summaryrefslogtreecommitdiff
path: root/toxcore/Lossless_UDP.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Lossless_UDP.h')
-rw-r--r--toxcore/Lossless_UDP.h124
1 files changed, 62 insertions, 62 deletions
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index fa74fab4..cd822e72 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -27,22 +27,22 @@
27#include "network.h" 27#include "network.h"
28 28
29 29
30/* maximum length of the data in the data packets */ 30/* Maximum length of the data in the data packets. */
31#define MAX_DATA_SIZE 1024 31#define MAX_DATA_SIZE 1024
32 32
33/* maximum data packets in sent and receive queues. */ 33/* Maximum data packets in sent and receive queues. */
34#define MAX_QUEUE_NUM 16 34#define MAX_QUEUE_NUM 16
35 35
36/* maximum number of data packets in the buffer */ 36/* Maximum number of data packets in the buffer. */
37#define BUFFER_PACKET_NUM (16-1) 37#define BUFFER_PACKET_NUM (16-1)
38 38
39/* timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */ 39/* Timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT. */
40#define CONNEXION_TIMEOUT 5 40#define CONNEXION_TIMEOUT 5
41 41
42/* initial amount of sync/hanshake packets to send per second. */ 42/* Initial amount of sync/hanshake packets to send per second. */
43#define SYNC_RATE 2 43#define SYNC_RATE 2
44 44
45/* initial send rate of data. */ 45/* Initial send rate of data. */
46#define DATA_SYNC_RATE 30 46#define DATA_SYNC_RATE 30
47 47
48typedef struct { 48typedef struct {
@@ -54,27 +54,29 @@ typedef struct {
54 IP_Port ip_port; 54 IP_Port ip_port;
55 55
56 /* 56 /*
57 * 0 if connection is dead, 1 if attempting handshake, 57 * return 0 if connection is dead.
58 * 2 if handshake is done (we start sending SYNC packets) 58 * return 1 if attempting handshake.
59 * 3 if we are sending SYNC packets and can send data 59 * return 2 if handshake is done (we start sending SYNC packets).
60 * 4 if the connection has timed out. 60 * return 3 if we are sending SYNC packets and can send data.
61 * return 4 if the connection has timed out.
61 */ 62 */
62 uint8_t status; 63 uint8_t status;
63 64
64 /* 65 /*
65 * 1 or 2 if connection was initiated by someone else, 0 if not. 66 * return 0 if connection was not initiated by someone else.
66 * 2 if incoming_connection() has not returned it yet, 1 if it has. 67 * return 1 if incoming_connection() has returned.
68 * return 2 if it has not.
67 */ 69 */
68 uint8_t inbound; 70 uint8_t inbound;
69 71
70 uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */ 72 uint16_t SYNC_rate; /* Current SYNC packet send rate packets per second. */
71 uint16_t data_rate; /* current data packet send rate packets per second. */ 73 uint16_t data_rate; /* Current data packet send rate packets per second. */
72 74
73 uint64_t last_SYNC; /* time our last SYNC packet was sent. */ 75 uint64_t last_SYNC; /* Time our last SYNC packet was sent. */
74 uint64_t last_sent; /* time our last data or handshake packet was sent. */ 76 uint64_t last_sent; /* Time our last data or handshake packet was sent. */
75 uint64_t last_recvSYNC; /* time we last received a SYNC packet from the other */ 77 uint64_t last_recvSYNC; /* Time we last received a SYNC packet from the other. */
76 uint64_t last_recvdata; /* time we last received a DATA packet from the other */ 78 uint64_t last_recvdata; /* Time we last received a DATA packet from the other. */
77 uint64_t killat; /* time to kill the connection */ 79 uint64_t killat; /* Time to kill the connection. */
78 80
79 Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */ 81 Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
80 Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */ 82 Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
@@ -82,31 +84,31 @@ typedef struct {
82 uint32_t handshake_id1; 84 uint32_t handshake_id1;
83 uint32_t handshake_id2; 85 uint32_t handshake_id2;
84 86
85 /* number of data packets received (also used as handshake_id1) */ 87 /* Number of data packets received (also used as handshake_id1). */
86 uint32_t recv_packetnum; 88 uint32_t recv_packetnum;
87 89
88 /* number of packets received by the other peer */ 90 /* Number of packets received by the other peer. */
89 uint32_t orecv_packetnum; 91 uint32_t orecv_packetnum;
90 92
91 /* number of data packets sent */ 93 /* Number of data packets sent. */
92 uint32_t sent_packetnum; 94 uint32_t sent_packetnum;
93 95
94 /* number of packets sent by the other peer. */ 96 /* Number of packets sent by the other peer. */
95 uint32_t osent_packetnum; 97 uint32_t osent_packetnum;
96 98
97 /* number of latest packet written onto the sendbuffer */ 99 /* Number of latest packet written onto the sendbuffer. */
98 uint32_t sendbuff_packetnum; 100 uint32_t sendbuff_packetnum;
99 101
100 /* we know all packets before that number were successfully sent */ 102 /* We know all packets before that number were successfully sent. */
101 uint32_t successful_sent; 103 uint32_t successful_sent;
102 104
103 /* packet number of last packet read with the read_packet function */ 105 /* Packet number of last packet read with the read_packet function. */
104 uint32_t successful_read; 106 uint32_t successful_read;
105 107
106 /* list of currently requested packet numbers(by the other person) */ 108 /* List of currently requested packet numbers(by the other person). */
107 uint32_t req_packets[BUFFER_PACKET_NUM]; 109 uint32_t req_packets[BUFFER_PACKET_NUM];
108 110
109 /* total number of currently requested packets(by the other person) */ 111 /* Total number of currently requested packets(by the other person). */
110 uint16_t num_req_paquets; 112 uint16_t num_req_paquets;
111 113
112 uint8_t recv_counter; 114 uint8_t recv_counter;
@@ -118,96 +120,94 @@ typedef struct {
118 Networking_Core *net; 120 Networking_Core *net;
119 Connection *connections; 121 Connection *connections;
120 122
121 uint32_t connections_length; /* Length of connections array */ 123 uint32_t connections_length; /* Length of connections array. */
122 uint32_t connections_number; /* Number of connections in connections array */ 124 uint32_t connections_number; /* Number of connections in connections array. */
123 125
124 /* table of random numbers used in handshake_id. */ 126 /* Table of random numbers used in handshake_id. */
125 uint32_t randtable[6][256]; 127 uint32_t randtable[6][256];
126 128
127} Lossless_UDP; 129} Lossless_UDP;
128 130
129/* 131/*
130 * Initialize a new connection to ip_port 132 * Initialize a new connection to ip_port.
131 * Returns an integer corresponding to the connection id. 133 * return an integer corresponding to the connection id.
132 * Return -1 if it could not initialize the connection. 134 * return -1 if it could not initialize the connection.
133 * Return number if there already was an existing connection to that ip_port. 135 * return number if there already was an existing connection to that ip_port.
134 */ 136 */
135int new_connection(Lossless_UDP *ludp, IP_Port ip_port); 137int new_connection(Lossless_UDP *ludp, IP_Port ip_port);
136 138
137/* 139/*
138 * Get connection id from IP_Port. 140 * Get connection id from IP_Port.
139 * Return -1 if there are no connections like we are looking for. 141 * return -1 if there are no connections like we are looking for.
140 * Return id if it found it . 142 * return id if it found it .
141 */ 143 */
142int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); 144int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);
143 145
144/* 146/*
145 * Returns an int corresponding to the next connection in our imcoming connection list 147 * return an integer corresponding to the next connection in our imcoming connection list.
146 * Return -1 if there are no new incoming connections in the list. 148 * return -1 if there are no new incoming connections in the list.
147 */ 149 */
148int incoming_connection(Lossless_UDP *ludp); 150int incoming_connection(Lossless_UDP *ludp);
149 151
150/* 152/*
151 * Return -1 if it could not kill the connection. 153 * return -1 if it could not kill the connection.
152 * Return 0 if killed successfully 154 * return 0 if killed successfully.
153 */ 155 */
154int kill_connection(Lossless_UDP *ludp, int connection_id); 156int kill_connection(Lossless_UDP *ludp, int connection_id);
155 157
156/* 158/*
157 * Kill connection in seconds seconds. 159 * Kill connection in seconds seconds.
158 * Return -1 if it can not kill the connection. 160 * return -1 if it can not kill the connection.
159 * Return 0 if it will kill it 161 * return 0 if it will kill it.
160 */ 162 */
161int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); 163int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);
162 164
163/* 165/*
164 * Returns the ip_port of the corresponding connection. 166 * returns the ip_port of the corresponding connection.
165 * Return 0 if there is no such connection. 167 * return 0 if there is no such connection.
166 */ 168 */
167IP_Port connection_ip(Lossless_UDP *ludp, int connection_id); 169IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);
168 170
169/* 171/*
170 * Returns the id of the next packet in the queue 172 * returns the id of the next packet in the queue.
171 * Return -1 if no packet in queue 173 * return -1 if no packet in queue.
172 */ 174 */
173char id_packet(Lossless_UDP *ludp, int connection_id); 175char id_packet(Lossless_UDP *ludp, int connection_id);
174 176
175/* 177/*
176 * Return 0 if there is no received data in the buffer. 178 * return 0 if there is no received data in the buffer.
177 * Return length of received packet if successful 179 * return length of received packet if successful.
178 */ 180 */
179int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); 181int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);
180 182
181/* 183/*
182 * Return 0 if data could not be put in packet queue 184 * return 0 if data could not be put in packet queue.
183 * Return 1 if data was put into the queue 185 * return 1 if data was put into the queue.
184 */ 186 */
185int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length); 187int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);
186 188
187/* Returns the number of packets in the queue waiting to be successfully sent. */ 189/* returns the number of packets in the queue waiting to be successfully sent. */
188uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); 190uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
189 191
190/* 192/*
191 * returns the number of packets in the queue waiting to be successfully 193 * return the number of packets in the queue waiting to be successfully
192 * read with read_packet(...) 194 * read with read_packet(...).
193 */ 195 */
194uint32_t recvqueue(Lossless_UDP *ludp, int connection_id); 196uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
195 197
196/* Check if connection is connected: 198/* Check if connection is connected:
197 * Return 0 no. 199 * return 0 not.
198 * Return 1 if attempting handshake. 200 * return 1 if attempting handshake.
199 * Return 2 if handshake is done. 201 * return 2 if handshake is done.
200 * Return 3 if fully connected. 202 * return 3 if fully connected.
201 * Return 4 if timed out and wating to be killed. 203 * return 4 if timed out and wating to be killed.
202 */ 204 */
203int is_connected(Lossless_UDP *ludp, int connection_id); 205int is_connected(Lossless_UDP *ludp, int connection_id);
204 206
205/* Call this function a couple times per second It's the main loop. */ 207/* Call this function a couple times per second It's the main loop. */
206void do_lossless_udp(Lossless_UDP *ludp); 208void do_lossless_udp(Lossless_UDP *ludp);
207 209
208/* 210/* This function sets up LosslessUDP packet handling. */
209 * This function sets up LosslessUDP packet handling.
210 */
211Lossless_UDP *new_lossless_udp(Networking_Core *net); 211Lossless_UDP *new_lossless_udp(Networking_Core *net);
212 212
213void kill_lossless_udp(Lossless_UDP *ludp); 213void kill_lossless_udp(Lossless_UDP *ludp);