diff options
Diffstat (limited to 'toxcore/Lossless_UDP.c')
-rw-r--r-- | toxcore/Lossless_UDP.c | 500 |
1 files changed, 240 insertions, 260 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index c30eb903..73df0e80 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -38,13 +38,13 @@ | |||
38 | */ | 38 | */ |
39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) | 39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) |
40 | { | 40 | { |
41 | uint32_t i; | 41 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
42 | 42 | if (tmp.ip_port.ip.i == ip_port.ip.i && | |
43 | for (i = 0; i < ludp->connections_length; ++i) { | 43 | tmp.ip_port.port == ip_port.port && |
44 | if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i && | 44 | tmp.status > 0) |
45 | ludp->connections[i].ip_port.port == ip_port.port && | 45 | { |
46 | ludp->connections[i].status > 0) | 46 | return tmp_i; |
47 | return i; | 47 | } |
48 | } | 48 | } |
49 | 49 | ||
50 | return -1; | 50 | return -1; |
@@ -98,49 +98,43 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
98 | if (connect != -1) | 98 | if (connect != -1) |
99 | return connect; | 99 | return connect; |
100 | 100 | ||
101 | if (ludp->connections_number == ludp->connections_length) { | 101 | int connection_id = -1; |
102 | Connection *temp; | 102 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
103 | temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1)); | 103 | if (tmp.status == 0) { |
104 | 104 | connection_id = tmp_i; | |
105 | if (temp == NULL) | 105 | break; |
106 | return -1; | 106 | } |
107 | } | ||
107 | 108 | ||
108 | memset(&temp[ludp->connections_length], 0, sizeof(Connection)); | 109 | if (connection_id == -1) { |
109 | ++ludp->connections_length; | 110 | tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */ |
110 | ludp->connections = temp; | 111 | connection_id = ludp->connections.len-1; |
111 | } | 112 | } |
113 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
112 | 114 | ||
113 | uint32_t i; | 115 | memset(connection, 0, sizeof(Connection)); |
114 | 116 | ||
115 | for (i = 0; i < ludp->connections_length; ++i) { | 117 | uint32_t handshake_id1 = handshake_id(ludp, ip_port); |
116 | if (ludp->connections[i].status == 0) { | ||
117 | memset(&ludp->connections[i], 0, sizeof(Connection)); | ||
118 | uint32_t handshake_id1 = handshake_id(ludp, ip_port); | ||
119 | |||
120 | ludp->connections[i] = (Connection) { | ||
121 | .ip_port = ip_port, | ||
122 | .status = 1, | ||
123 | .inbound = 0, | ||
124 | .handshake_id1 = handshake_id1, | ||
125 | .sent_packetnum = handshake_id1, | ||
126 | .sendbuff_packetnum = handshake_id1, | ||
127 | .successful_sent = handshake_id1, | ||
128 | .SYNC_rate = SYNC_RATE, | ||
129 | .data_rate = DATA_SYNC_RATE, | ||
130 | .last_recvSYNC = current_time(), | ||
131 | .last_sent = current_time(), | ||
132 | .killat = ~0, | ||
133 | .send_counter = 0, | ||
134 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | ||
135 | .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT | ||
136 | }; | ||
137 | ++ludp->connections_number; | ||
138 | |||
139 | return i; | ||
140 | } | ||
141 | } | ||
142 | 118 | ||
143 | return -1; | 119 | *connection = (Connection) { |
120 | .ip_port = ip_port, | ||
121 | .status = 1, | ||
122 | .inbound = 0, | ||
123 | .handshake_id1 = handshake_id1, | ||
124 | .sent_packetnum = handshake_id1, | ||
125 | .sendbuff_packetnum = handshake_id1, | ||
126 | .successful_sent = handshake_id1, | ||
127 | .SYNC_rate = SYNC_RATE, | ||
128 | .data_rate = DATA_SYNC_RATE, | ||
129 | .last_recvSYNC = current_time(), | ||
130 | .last_sent = current_time(), | ||
131 | .killat = ~0, | ||
132 | .send_counter = 0, | ||
133 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | ||
134 | .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT | ||
135 | }; | ||
136 | |||
137 | return connection_id; | ||
144 | } | 138 | } |
145 | 139 | ||
146 | /* | 140 | /* |
@@ -153,47 +147,41 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
153 | if (getconnection_id(ludp, ip_port) != -1) | 147 | if (getconnection_id(ludp, ip_port) != -1) |
154 | return -1; | 148 | return -1; |
155 | 149 | ||
156 | if (ludp->connections_number == ludp->connections_length) { | 150 | int connection_id = -1; |
157 | Connection *temp; | 151 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
158 | temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1)); | 152 | if (tmp.status == 0) { |
159 | 153 | connection_id = tmp_i; | |
160 | if (temp == NULL) | 154 | break; |
161 | return -1; | 155 | } |
156 | } | ||
162 | 157 | ||
163 | memset(&temp[ludp->connections_length], 0, sizeof(Connection)); | 158 | if (connection_id == -1) { |
164 | ++ludp->connections_length; | 159 | tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */ |
165 | ludp->connections = temp; | 160 | connection_id = ludp->connections.len-1; |
166 | } | 161 | } |
162 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
163 | memset(connection, 0, sizeof(Connection)); | ||
167 | 164 | ||
168 | uint32_t i; | 165 | uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; |
169 | 166 | ||
170 | for (i = 0; i < ludp->connections_length; ++i) { | 167 | *connection = (Connection) { |
171 | if (ludp->connections[i].status == 0) { | 168 | .ip_port = ip_port, |
172 | memset(&ludp->connections[i], 0, sizeof(Connection)); | 169 | .status = 2, |
173 | uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | 170 | .inbound = 2, |
174 | 171 | .SYNC_rate = SYNC_RATE, | |
175 | ludp->connections[i] = (Connection) { | 172 | .data_rate = DATA_SYNC_RATE, |
176 | .ip_port = ip_port, | 173 | .last_recvSYNC = current_time(), |
177 | .status = 2, | 174 | .last_sent = current_time(), |
178 | .inbound = 2, | 175 | .send_counter = 127, |
179 | .SYNC_rate = SYNC_RATE, | ||
180 | .data_rate = DATA_SYNC_RATE, | ||
181 | .last_recvSYNC = current_time(), | ||
182 | .last_sent = current_time(), | ||
183 | .send_counter = 127, | ||
184 | |||
185 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | ||
186 | .timeout = timeout, | ||
187 | |||
188 | /* if this connection isn't handled within the timeout kill it. */ | ||
189 | .killat = current_time() + 1000000UL * timeout | ||
190 | }; | ||
191 | ++ludp->connections_number; | ||
192 | return i; | ||
193 | } | ||
194 | } | ||
195 | 176 | ||
196 | return -1; | 177 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
178 | .timeout = timeout, | ||
179 | |||
180 | /* if this connection isn't handled within the timeout kill it. */ | ||
181 | .killat = current_time() + 1000000UL * timeout | ||
182 | }; | ||
183 | |||
184 | return connection_id; | ||
197 | } | 185 | } |
198 | 186 | ||
199 | /* | 187 | /* |
@@ -202,59 +190,29 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
202 | */ | 190 | */ |
203 | int incoming_connection(Lossless_UDP *ludp) | 191 | int incoming_connection(Lossless_UDP *ludp) |
204 | { | 192 | { |
205 | uint32_t i; | 193 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
206 | 194 | if (tmp.inbound == 2) { | |
207 | for (i = 0; i < ludp->connections_length; ++i) { | 195 | tmp.inbound = 1; |
208 | if (ludp->connections[i].inbound == 2) { | 196 | return tmp_i; |
209 | ludp->connections[i].inbound = 1; | ||
210 | return i; | ||
211 | } | 197 | } |
212 | } | 198 | } |
213 | 199 | ||
214 | return -1; | 200 | return -1; |
215 | } | 201 | } |
216 | 202 | ||
217 | /* Try to free some memory from the connections array. */ | ||
218 | static void free_connections(Lossless_UDP *ludp) | ||
219 | { | ||
220 | uint32_t i; | ||
221 | |||
222 | for (i = ludp->connections_length; i != 0; --i) | ||
223 | if (ludp->connections[i - 1].status != 0) | ||
224 | break; | ||
225 | |||
226 | if (ludp->connections_length == i) | ||
227 | return; | ||
228 | |||
229 | if (i == 0) { | ||
230 | free(ludp->connections); | ||
231 | ludp->connections = NULL; | ||
232 | ludp->connections_length = i; | ||
233 | return; | ||
234 | } | ||
235 | |||
236 | Connection *temp; | ||
237 | temp = realloc(ludp->connections, sizeof(Connection) * i); | ||
238 | |||
239 | if (temp == NULL && i != 0) | ||
240 | return; | ||
241 | |||
242 | ludp->connections = temp; | ||
243 | ludp->connections_length = i; | ||
244 | } | ||
245 | |||
246 | /* | 203 | /* |
247 | * Return -1 if it could not kill the connection. | 204 | * Return -1 if it could not kill the connection. |
248 | * Return 0 if killed successfully | 205 | * Return 0 if killed successfully |
249 | */ | 206 | */ |
250 | int kill_connection(Lossless_UDP *ludp, int connection_id) | 207 | int kill_connection(Lossless_UDP *ludp, int connection_id) |
251 | { | 208 | { |
252 | if (connection_id >= 0 && connection_id < ludp->connections_length) { | 209 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
253 | if (ludp->connections[connection_id].status > 0) { | 210 | |
254 | ludp->connections[connection_id].status = 0; | 211 | if (connection_id >= 0 && connection_id < ludp->connections.len) { |
255 | change_handshake(ludp, ludp->connections[connection_id].ip_port); | 212 | if (connection->status > 0) { |
256 | --ludp->connections_number; | 213 | connection->status = 0; |
257 | free_connections(ludp); | 214 | change_handshake(ludp, connection->ip_port); |
215 | memset(connection, 0, sizeof(Connection)); | ||
258 | return 0; | 216 | return 0; |
259 | } | 217 | } |
260 | } | 218 | } |
@@ -269,9 +227,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id) | |||
269 | */ | 227 | */ |
270 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) | 228 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) |
271 | { | 229 | { |
272 | if (connection_id >= 0 && connection_id < ludp->connections_length) { | 230 | if (connection_id >= 0 && connection_id < ludp->connections.len) { |
273 | if (ludp->connections[connection_id].status > 0) { | 231 | if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) { |
274 | ludp->connections[connection_id].killat = current_time() + 1000000UL * seconds; | 232 | tox_array_get(&ludp->connections, connection_id, Connection).killat = current_time() + 1000000UL * seconds; |
275 | return 0; | 233 | return 0; |
276 | } | 234 | } |
277 | } | 235 | } |
@@ -289,8 +247,8 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) | |||
289 | */ | 247 | */ |
290 | int is_connected(Lossless_UDP *ludp, int connection_id) | 248 | int is_connected(Lossless_UDP *ludp, int connection_id) |
291 | { | 249 | { |
292 | if (connection_id >= 0 && connection_id < ludp->connections_length) | 250 | if (connection_id >= 0 && connection_id < ludp->connections.len) |
293 | return ludp->connections[connection_id].status; | 251 | return tox_array_get(&ludp->connections, connection_id, Connection).status; |
294 | 252 | ||
295 | return 0; | 253 | return 0; |
296 | } | 254 | } |
@@ -298,8 +256,8 @@ int is_connected(Lossless_UDP *ludp, int connection_id) | |||
298 | /* returns the ip_port of the corresponding connection. */ | 256 | /* returns the ip_port of the corresponding connection. */ |
299 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) | 257 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) |
300 | { | 258 | { |
301 | if (connection_id >= 0 && connection_id < ludp->connections_length) | 259 | if (connection_id >= 0 && connection_id < ludp->connections.len) |
302 | return ludp->connections[connection_id].ip_port; | 260 | return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; |
303 | 261 | ||
304 | IP_Port zero = {{{0}}, 0}; | 262 | IP_Port zero = {{{0}}, 0}; |
305 | return zero; | 263 | return zero; |
@@ -308,30 +266,31 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) | |||
308 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 266 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
309 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) | 267 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) |
310 | { | 268 | { |
311 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 269 | if (connection_id < 0 || connection_id >= ludp->connections.len) |
312 | return 0; | 270 | return 0; |
313 | 271 | ||
314 | return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent; | 272 | return tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum - |
273 | tox_array_get(&ludp->connections, connection_id, Connection).successful_sent; | ||
315 | } | 274 | } |
316 | 275 | ||
317 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 276 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
318 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) | 277 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) |
319 | { | 278 | { |
320 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 279 | if (connection_id < 0 || connection_id >= ludp->connections.len) |
321 | return 0; | 280 | return 0; |
322 | 281 | ||
323 | return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read; | 282 | return tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum - tox_array_get(&ludp->connections, connection_id, Connection).successful_read; |
324 | } | 283 | } |
325 | 284 | ||
326 | /* returns the id of the next packet in the queue | 285 | /* returns the id of the next packet in the queue |
327 | return -1 if no packet in queue */ | 286 | return -1 if no packet in queue */ |
328 | char id_packet(Lossless_UDP *ludp, int connection_id) | 287 | char id_packet(Lossless_UDP *ludp, int connection_id) |
329 | { | 288 | { |
330 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 289 | if (connection_id < 0 || connection_id >= ludp->connections.len) |
331 | return -1; | 290 | return -1; |
332 | 291 | ||
333 | if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0) | 292 | if (recvqueue(ludp, connection_id) != 0 && tox_array_get(&ludp->connections, connection_id, Connection).status != 0) |
334 | return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % | 293 | return tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[tox_array_get(&ludp->connections, connection_id, Connection).successful_read % |
335 | MAX_QUEUE_NUM].data[0]; | 294 | MAX_QUEUE_NUM].data[0]; |
336 | 295 | ||
337 | return -1; | 296 | return -1; |
@@ -342,11 +301,12 @@ char id_packet(Lossless_UDP *ludp, int connection_id) | |||
342 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) | 301 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) |
343 | { | 302 | { |
344 | if (recvqueue(ludp, connection_id) != 0) { | 303 | if (recvqueue(ludp, connection_id) != 0) { |
345 | uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM; | 304 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
346 | uint16_t size = ludp->connections[connection_id].recvbuffer[index].size; | 305 | uint16_t index = connection->successful_read % MAX_QUEUE_NUM; |
347 | memcpy(data, ludp->connections[connection_id].recvbuffer[index].data, size); | 306 | uint16_t size = connection->recvbuffer[index].size; |
348 | ++ludp->connections[connection_id].successful_read; | 307 | memcpy(data, connection->recvbuffer[index].data, size); |
349 | ludp->connections[connection_id].recvbuffer[index].size = 0; | 308 | ++connection->successful_read; |
309 | connection->recvbuffer[index].size = 0; | ||
350 | return size; | 310 | return size; |
351 | } | 311 | } |
352 | 312 | ||
@@ -363,10 +323,11 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t | |||
363 | return 0; | 323 | return 0; |
364 | 324 | ||
365 | if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) { | 325 | if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) { |
366 | uint32_t index = ludp->connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; | 326 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
367 | memcpy(ludp->connections[connection_id].sendbuffer[index].data, data, length); | 327 | uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM; |
368 | ludp->connections[connection_id].sendbuffer[index].size = length; | 328 | memcpy(connection->sendbuffer[index].data, data, length); |
369 | ludp->connections[connection_id].sendbuff_packetnum++; | 329 | connection->sendbuffer[index].size = length; |
330 | connection->sendbuff_packetnum++; | ||
370 | return 1; | 331 | return 1; |
371 | } | 332 | } |
372 | 333 | ||
@@ -384,8 +345,13 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques | |||
384 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) | 345 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) |
385 | return 0; | 346 | return 0; |
386 | 347 | ||
387 | for (i = ludp->connections[connection_id].recv_packetnum; i != ludp->connections[connection_id].osent_packetnum; i++) { | 348 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
388 | if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { | 349 | |
350 | for (i = connection->recv_packetnum; | ||
351 | i != connection->osent_packetnum; | ||
352 | i++) | ||
353 | { | ||
354 | if (connection->recvbuffer[i % MAX_QUEUE_NUM].size == 0) { | ||
389 | temp = htonl(i); | 355 | temp = htonl(i); |
390 | memcpy(requested + number, &temp, 4); | 356 | memcpy(requested + number, &temp, 4); |
391 | ++number; | 357 | ++number; |
@@ -393,7 +359,7 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques | |||
393 | } | 359 | } |
394 | 360 | ||
395 | if (number == 0) | 361 | if (number == 0) |
396 | ludp->connections[connection_id].recv_packetnum = ludp->connections[connection_id].osent_packetnum; | 362 | connection->recv_packetnum = connection->osent_packetnum; |
397 | 363 | ||
398 | return number; | 364 | return number; |
399 | } | 365 | } |
@@ -418,15 +384,17 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak | |||
418 | return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); | 384 | return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); |
419 | } | 385 | } |
420 | 386 | ||
421 | static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) | 387 | static int send_SYNC(Lossless_UDP *ludp, int connection_id) |
422 | { | 388 | { |
389 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
390 | |||
423 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; | 391 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; |
424 | uint16_t index = 0; | 392 | uint16_t index = 0; |
425 | 393 | ||
426 | IP_Port ip_port = ludp->connections[connection_id].ip_port; | 394 | IP_Port ip_port = connection->ip_port; |
427 | uint8_t counter = ludp->connections[connection_id].send_counter; | 395 | uint8_t counter = connection->send_counter; |
428 | uint32_t recv_packetnum = htonl(ludp->connections[connection_id].recv_packetnum); | 396 | uint32_t recv_packetnum = htonl(connection->recv_packetnum); |
429 | uint32_t sent_packetnum = htonl(ludp->connections[connection_id].sent_packetnum); | 397 | uint32_t sent_packetnum = htonl(connection->sent_packetnum); |
430 | 398 | ||
431 | uint32_t requested[BUFFER_PACKET_NUM]; | 399 | uint32_t requested[BUFFER_PACKET_NUM]; |
432 | uint32_t number = missing_packets(ludp, connection_id, requested); | 400 | uint32_t number = missing_packets(ludp, connection_id, requested); |
@@ -445,37 +413,38 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) | |||
445 | 413 | ||
446 | } | 414 | } |
447 | 415 | ||
448 | static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num) | 416 | static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t packet_num) |
449 | { | 417 | { |
418 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
419 | |||
450 | uint32_t index = packet_num % MAX_QUEUE_NUM; | 420 | uint32_t index = packet_num % MAX_QUEUE_NUM; |
451 | uint32_t temp; | 421 | uint32_t temp; |
452 | uint8_t packet[1 + 4 + MAX_DATA_SIZE]; | 422 | uint8_t packet[1 + 4 + MAX_DATA_SIZE]; |
453 | packet[0] = NET_PACKET_DATA; | 423 | packet[0] = NET_PACKET_DATA; |
454 | temp = htonl(packet_num); | 424 | temp = htonl(packet_num); |
455 | memcpy(packet + 1, &temp, 4); | 425 | memcpy(packet + 1, &temp, 4); |
456 | memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data, | 426 | memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); |
457 | ludp->connections[connection_id].sendbuffer[index].size); | 427 | return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); |
458 | return sendpacket(ludp->net->sock, ludp->connections[connection_id].ip_port, packet, | ||
459 | 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size); | ||
460 | } | 428 | } |
461 | 429 | ||
462 | /* sends 1 data packet */ | 430 | /* sends 1 data packet */ |
463 | static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) | 431 | static int send_DATA(Lossless_UDP *ludp, int connection_id) |
464 | { | 432 | { |
433 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
465 | int ret; | 434 | int ret; |
466 | uint32_t buffer[BUFFER_PACKET_NUM]; | 435 | uint32_t buffer[BUFFER_PACKET_NUM]; |
467 | 436 | ||
468 | if (ludp->connections[connection_id].num_req_paquets > 0) { | 437 | if (connection->num_req_paquets > 0) { |
469 | ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].req_packets[0]); | 438 | ret = send_data_packet(ludp, connection_id, connection->req_packets[0]); |
470 | ludp->connections[connection_id].num_req_paquets--; | 439 | connection->num_req_paquets--; |
471 | memcpy(buffer, ludp->connections[connection_id].req_packets + 1, ludp->connections[connection_id].num_req_paquets * 4); | 440 | memcpy(buffer, connection->req_packets + 1, connection->num_req_paquets * 4); |
472 | memcpy(ludp->connections[connection_id].req_packets, buffer, ludp->connections[connection_id].num_req_paquets * 4); | 441 | memcpy(connection->req_packets, buffer, connection->num_req_paquets * 4); |
473 | return ret; | 442 | return ret; |
474 | } | 443 | } |
475 | 444 | ||
476 | if (ludp->connections[connection_id].sendbuff_packetnum != ludp->connections[connection_id].sent_packetnum) { | 445 | if (connection->sendbuff_packetnum != connection->sent_packetnum) { |
477 | ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].sent_packetnum); | 446 | ret = send_data_packet(ludp, connection_id, connection->sent_packetnum); |
478 | ludp->connections[connection_id].sent_packetnum++; | 447 | connection->sent_packetnum++; |
479 | return ret; | 448 | return ret; |
480 | } | 449 | } |
481 | 450 | ||
@@ -502,29 +471,31 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3 | |||
502 | uint32_t temp; | 471 | uint32_t temp; |
503 | uint32_t handshake_id1, handshake_id2; | 472 | uint32_t handshake_id1, handshake_id2; |
504 | 473 | ||
505 | int connection = getconnection_id(ludp, source); | 474 | int connection_id = getconnection_id(ludp, source); |
475 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
476 | |||
506 | memcpy(&temp, packet + 1, 4); | 477 | memcpy(&temp, packet + 1, 4); |
507 | handshake_id1 = ntohl(temp); | 478 | handshake_id1 = ntohl(temp); |
508 | memcpy(&temp, packet + 5, 4); | 479 | memcpy(&temp, packet + 5, 4); |
509 | handshake_id2 = ntohl(temp); | 480 | handshake_id2 = ntohl(temp); |
510 | 481 | ||
511 | if (handshake_id2 == 0 && is_connected(ludp, connection) < 3) { | 482 | if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) { |
512 | send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1); | 483 | send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1); |
513 | return 0; | 484 | return 0; |
514 | } | 485 | } |
515 | 486 | ||
516 | if (is_connected(ludp, connection) != 1) | 487 | if (is_connected(ludp, connection_id) != 1) |
517 | return 1; | 488 | return 1; |
518 | 489 | ||
519 | /* if handshake_id2 is what we sent previously as handshake_id1 */ | 490 | /* if handshake_id2 is what we sent previously as handshake_id1 */ |
520 | if (handshake_id2 == ludp->connections[connection].handshake_id1) { | 491 | if (handshake_id2 == connection->handshake_id1) { |
521 | ludp->connections[connection].status = 2; | 492 | connection->status = 2; |
522 | /* NOTE: is this necessary? | 493 | /* NOTE: is this necessary? |
523 | ludp->connections[connection].handshake_id2 = handshake_id1; */ | 494 | connection->handshake_id2 = handshake_id1; */ |
524 | ludp->connections[connection].orecv_packetnum = handshake_id2; | 495 | connection->orecv_packetnum = handshake_id2; |
525 | ludp->connections[connection].osent_packetnum = handshake_id1; | 496 | connection->osent_packetnum = handshake_id1; |
526 | ludp->connections[connection].recv_packetnum = handshake_id1; | 497 | connection->recv_packetnum = handshake_id1; |
527 | ludp->connections[connection].successful_read = handshake_id1; | 498 | connection->successful_read = handshake_id1; |
528 | } | 499 | } |
529 | 500 | ||
530 | return 0; | 501 | return 0; |
@@ -547,18 +518,19 @@ static int SYNC_valid(uint32_t length) | |||
547 | static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 518 | static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
548 | { | 519 | { |
549 | if (handshake_id(ludp, source) == recv_packetnum) { | 520 | if (handshake_id(ludp, source) == recv_packetnum) { |
550 | int x = new_inconnection(ludp, source); | 521 | int connection_id = new_inconnection(ludp, source); |
551 | 522 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | |
552 | if (x != -1) { | 523 | |
553 | ludp->connections[x].orecv_packetnum = recv_packetnum; | 524 | if (connection_id != -1) { |
554 | ludp->connections[x].sent_packetnum = recv_packetnum; | 525 | connection->orecv_packetnum = recv_packetnum; |
555 | ludp->connections[x].sendbuff_packetnum = recv_packetnum; | 526 | connection->sent_packetnum = recv_packetnum; |
556 | ludp->connections[x].successful_sent = recv_packetnum; | 527 | connection->sendbuff_packetnum = recv_packetnum; |
557 | ludp->connections[x].osent_packetnum = sent_packetnum; | 528 | connection->successful_sent = recv_packetnum; |
558 | ludp->connections[x].recv_packetnum = sent_packetnum; | 529 | connection->osent_packetnum = sent_packetnum; |
559 | ludp->connections[x].successful_read = sent_packetnum; | 530 | connection->recv_packetnum = sent_packetnum; |
560 | 531 | connection->successful_read = sent_packetnum; | |
561 | return x; | 532 | |
533 | return connection_id; | ||
562 | } | 534 | } |
563 | } | 535 | } |
564 | 536 | ||
@@ -569,11 +541,13 @@ static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packet | |||
569 | static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, | 541 | static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, |
570 | uint32_t sent_packetnum) | 542 | uint32_t sent_packetnum) |
571 | { | 543 | { |
572 | if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) { | 544 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
573 | /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */ | 545 | |
574 | ludp->connections[connection_id].status = 3; | 546 | if (recv_packetnum == connection->orecv_packetnum) { |
575 | ludp->connections[connection_id].recv_counter = counter; | 547 | /* && sent_packetnum == connection->osent_packetnum) */ |
576 | ++ludp->connections[connection_id].send_counter; | 548 | connection->status = 3; |
549 | connection->recv_counter = counter; | ||
550 | ++connection->send_counter; | ||
577 | send_SYNC(ludp, connection_id); | 551 | send_SYNC(ludp, connection_id); |
578 | return 0; | 552 | return 0; |
579 | } | 553 | } |
@@ -583,35 +557,37 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
583 | /* case 3 in handle_SYNC: */ | 557 | /* case 3 in handle_SYNC: */ |
584 | static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, | 558 | static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, |
585 | uint32_t sent_packetnum, | 559 | uint32_t sent_packetnum, |
586 | uint32_t *req_packets, | 560 | uint32_t *req_packets, |
587 | uint16_t number) | 561 | uint16_t number) |
588 | { | 562 | { |
589 | uint8_t comp_counter = (counter - ludp->connections[connection_id].recv_counter ); | 563 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
564 | |||
565 | uint8_t comp_counter = (counter - connection->recv_counter); | ||
590 | uint32_t i, temp; | 566 | uint32_t i, temp; |
591 | /* uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].successful_sent); | 567 | /* uint32_t comp_1 = (recv_packetnum - connection->successful_sent); |
592 | uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].successful_read); */ | 568 | uint32_t comp_2 = (sent_packetnum - connection->successful_read); */ |
593 | uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum); | 569 | uint32_t comp_1 = (recv_packetnum - connection->orecv_packetnum); |
594 | uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum); | 570 | uint32_t comp_2 = (sent_packetnum - connection->osent_packetnum); |
595 | 571 | ||
596 | /* packet valid */ | 572 | /* packet valid */ |
597 | if (comp_1 <= BUFFER_PACKET_NUM && | 573 | if (comp_1 <= BUFFER_PACKET_NUM && |
598 | comp_2 <= BUFFER_PACKET_NUM && | 574 | comp_2 <= BUFFER_PACKET_NUM && |
599 | comp_counter < 10 && comp_counter != 0) { | 575 | comp_counter < 10 && comp_counter != 0) { |
600 | 576 | ||
601 | ludp->connections[connection_id].orecv_packetnum = recv_packetnum; | 577 | connection->orecv_packetnum = recv_packetnum; |
602 | ludp->connections[connection_id].osent_packetnum = sent_packetnum; | 578 | connection->osent_packetnum = sent_packetnum; |
603 | ludp->connections[connection_id].successful_sent = recv_packetnum; | 579 | connection->successful_sent = recv_packetnum; |
604 | ludp->connections[connection_id].last_recvSYNC = current_time(); | 580 | connection->last_recvSYNC = current_time(); |
605 | ludp->connections[connection_id].recv_counter = counter; | 581 | connection->recv_counter = counter; |
606 | 582 | ||
607 | ++ludp->connections[connection_id].send_counter; | 583 | ++connection->send_counter; |
608 | 584 | ||
609 | for (i = 0; i < number; ++i) { | 585 | for (i = 0; i < number; ++i) { |
610 | temp = ntohl(req_packets[i]); | 586 | temp = ntohl(req_packets[i]); |
611 | memcpy(ludp->connections[connection_id].req_packets + i, &temp, 4 * number); | 587 | memcpy(connection->req_packets + i, &temp, 4 * number); |
612 | } | 588 | } |
613 | 589 | ||
614 | ludp->connections[connection_id].num_req_paquets = number; | 590 | connection->num_req_paquets = number; |
615 | return 0; | 591 | return 0; |
616 | } | 592 | } |
617 | 593 | ||
@@ -625,7 +601,6 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
625 | if (!SYNC_valid(length)) | 601 | if (!SYNC_valid(length)) |
626 | return 1; | 602 | return 1; |
627 | 603 | ||
628 | int connection = getconnection_id(ludp, source); | ||
629 | uint8_t counter; | 604 | uint8_t counter; |
630 | uint32_t temp; | 605 | uint32_t temp; |
631 | uint32_t recv_packetnum, sent_packetnum; | 606 | uint32_t recv_packetnum, sent_packetnum; |
@@ -641,15 +616,17 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
641 | if (number != 0) | 616 | if (number != 0) |
642 | memcpy(req_packets, packet + 10, 4 * number); | 617 | memcpy(req_packets, packet + 10, 4 * number); |
643 | 618 | ||
644 | if (connection == -1) | 619 | int connection_id = getconnection_id(ludp, source); |
620 | if (connection_id == -1) | ||
645 | return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum); | 621 | return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum); |
622 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
646 | 623 | ||
647 | if (ludp->connections[connection].status == 2) | 624 | if (connection->status == 2) |
648 | return handle_SYNC2(ludp, connection, counter, | 625 | return handle_SYNC2(ludp, connection_id, counter, |
649 | recv_packetnum, sent_packetnum); | 626 | recv_packetnum, sent_packetnum); |
650 | 627 | ||
651 | if (ludp->connections[connection].status == 3) | 628 | if (connection->status == 3) |
652 | return handle_SYNC3(ludp, connection, counter, recv_packetnum, | 629 | return handle_SYNC3(ludp, connection_id, counter, recv_packetnum, |
653 | sent_packetnum, req_packets, number); | 630 | sent_packetnum, req_packets, number); |
654 | 631 | ||
655 | return 0; | 632 | return 0; |
@@ -661,31 +638,33 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
661 | */ | 638 | */ |
662 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) | 639 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) |
663 | { | 640 | { |
641 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
642 | |||
664 | if (size > MAX_DATA_SIZE) | 643 | if (size > MAX_DATA_SIZE) |
665 | return 1; | 644 | return 1; |
666 | 645 | ||
667 | uint32_t i; | 646 | uint32_t i; |
668 | uint32_t maxnum = ludp->connections[connection_id].successful_read + BUFFER_PACKET_NUM; | 647 | uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM; |
669 | uint32_t sent_packet = data_num - ludp->connections[connection_id].osent_packetnum; | 648 | uint32_t sent_packet = data_num - connection->osent_packetnum; |
670 | 649 | ||
671 | for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) { | 650 | for (i = connection->recv_packetnum; i != maxnum; ++i) { |
672 | if (i == data_num) { | 651 | if (i == data_num) { |
673 | memcpy(ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); | 652 | memcpy(connection->recvbuffer[i % MAX_QUEUE_NUM].data, data, size); |
674 | 653 | ||
675 | ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; | 654 | connection->recvbuffer[i % MAX_QUEUE_NUM].size = size; |
676 | ludp->connections[connection_id].last_recvdata = current_time(); | 655 | connection->last_recvdata = current_time(); |
677 | 656 | ||
678 | if (sent_packet < BUFFER_PACKET_NUM) { | 657 | if (sent_packet < BUFFER_PACKET_NUM) { |
679 | ludp->connections[connection_id].osent_packetnum = data_num; | 658 | connection->osent_packetnum = data_num; |
680 | } | 659 | } |
681 | 660 | ||
682 | break; | 661 | break; |
683 | } | 662 | } |
684 | } | 663 | } |
685 | 664 | ||
686 | for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) { | 665 | for (i = connection->recv_packetnum; i != maxnum; ++i) { |
687 | if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) | 666 | if (connection->recvbuffer[i % MAX_QUEUE_NUM].size != 0) |
688 | ludp->connections[connection_id].recv_packetnum = i; | 667 | connection->recv_packetnum = i; |
689 | else | 668 | else |
690 | break; | 669 | break; |
691 | } | 670 | } |
@@ -696,13 +675,13 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui | |||
696 | static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 675 | static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
697 | { | 676 | { |
698 | Lossless_UDP *ludp = object; | 677 | Lossless_UDP *ludp = object; |
699 | int connection = getconnection_id(ludp, source); | 678 | int connection_id = getconnection_id(ludp, source); |
700 | 679 | if (connection_id == -1) | |
701 | if (connection == -1) | ||
702 | return 1; | 680 | return 1; |
681 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
703 | 682 | ||
704 | /* Drop the data packet if connection is not connected. */ | 683 | /* Drop the data packet if connection is not connected. */ |
705 | if (ludp->connections[connection].status != 3) | 684 | if (connection->status != 3) |
706 | return 1; | 685 | return 1; |
707 | 686 | ||
708 | if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) | 687 | if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) |
@@ -715,7 +694,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
715 | memcpy(&temp, packet + 1, 4); | 694 | memcpy(&temp, packet + 1, 4); |
716 | number = ntohl(temp); | 695 | number = ntohl(temp); |
717 | 696 | ||
718 | return add_recv(ludp, connection, number, packet + 5, size); | 697 | return add_recv(ludp, connection_id, number, packet + 5, size); |
719 | } | 698 | } |
720 | 699 | ||
721 | /* | 700 | /* |
@@ -748,22 +727,20 @@ static void do_new(Lossless_UDP *ludp) | |||
748 | uint32_t i; | 727 | uint32_t i; |
749 | uint64_t temp_time = current_time(); | 728 | uint64_t temp_time = current_time(); |
750 | 729 | ||
751 | for (i = 0; i < ludp->connections_length; ++i) { | 730 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
752 | if (ludp->connections[i].status == 1) | 731 | if (tmp.status == 1) |
753 | if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) { | 732 | if ((tmp.last_sent + (1000000UL / tmp.SYNC_rate)) <= temp_time) { |
754 | send_handshake(ludp, ludp->connections[i].ip_port, ludp->connections[i].handshake_id1, 0); | 733 | send_handshake(ludp, tmp.ip_port, tmp.handshake_id1, 0); |
755 | ludp->connections[i].last_sent = temp_time; | 734 | tmp.last_sent = temp_time; |
756 | } | 735 | } |
757 | 736 | ||
758 | /* kill all timed out connections */ | 737 | /* kill all timed out connections */ |
759 | if (ludp->connections[i].status > 0 && | 738 | if (tmp.status > 0 && (tmp.last_recvSYNC + tmp.timeout * 1000000UL) < temp_time && tmp.status != 4) { |
760 | (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time && | 739 | tmp.status = 4; |
761 | ludp->connections[i].status != 4) { | ||
762 | ludp->connections[i].status = 4; | ||
763 | /* kill_connection(i); */ | 740 | /* kill_connection(i); */ |
764 | } | 741 | } |
765 | 742 | ||
766 | if (ludp->connections[i].status > 0 && ludp->connections[i].killat < temp_time) | 743 | if (tmp.status > 0 && tmp.killat < temp_time) |
767 | kill_connection(ludp, i); | 744 | kill_connection(ludp, i); |
768 | } | 745 | } |
769 | } | 746 | } |
@@ -773,11 +750,11 @@ static void do_SYNC(Lossless_UDP *ludp) | |||
773 | uint32_t i; | 750 | uint32_t i; |
774 | uint64_t temp_time = current_time(); | 751 | uint64_t temp_time = current_time(); |
775 | 752 | ||
776 | for (i = 0; i < ludp->connections_length; ++i) { | 753 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
777 | if (ludp->connections[i].status == 2 || ludp->connections[i].status == 3) | 754 | if (tmp.status == 2 || tmp.status == 3) |
778 | if ((ludp->connections[i].last_SYNC + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) { | 755 | if ((tmp.last_SYNC + (1000000UL / tmp.SYNC_rate)) <= temp_time) { |
779 | send_SYNC(ludp, i); | 756 | send_SYNC(ludp, i); |
780 | ludp->connections[i].last_SYNC = temp_time; | 757 | tmp.last_SYNC = temp_time; |
781 | } | 758 | } |
782 | } | 759 | } |
783 | } | 760 | } |
@@ -788,14 +765,17 @@ static void do_data(Lossless_UDP *ludp) | |||
788 | uint64_t j; | 765 | uint64_t j; |
789 | uint64_t temp_time = current_time(); | 766 | uint64_t temp_time = current_time(); |
790 | 767 | ||
791 | for (i = 0; i < ludp->connections_length; ++i) | 768 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
792 | if (ludp->connections[i].status == 3 && sendqueue(ludp, i) != 0) | 769 | if (tmp.status == 3 && sendqueue(ludp, i) != 0 && |
793 | if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].data_rate)) <= temp_time) { | 770 | (tmp.last_sent + (1000000UL / tmp.data_rate)) <= temp_time) |
794 | for (j = ludp->connections[i].last_sent; j < temp_time; j += (1000000UL / ludp->connections[i].data_rate)) | 771 | { |
795 | send_DATA(ludp, i); | 772 | for (j = tmp.last_sent; j < temp_time; j += (1000000UL / tmp.data_rate)) |
773 | send_DATA(ludp, i); | ||
796 | 774 | ||
797 | ludp->connections[i].last_sent = temp_time; | 775 | tmp.last_sent = temp_time; |
798 | } | 776 | |
777 | } | ||
778 | } | ||
799 | } | 779 | } |
800 | 780 | ||
801 | #define MAX_SYNC_RATE 10 | 781 | #define MAX_SYNC_RATE 10 |
@@ -810,18 +790,18 @@ static void adjust_rates(Lossless_UDP *ludp) | |||
810 | uint32_t i; | 790 | uint32_t i; |
811 | uint64_t temp_time = current_time(); | 791 | uint64_t temp_time = current_time(); |
812 | 792 | ||
813 | for (i = 0; i < ludp->connections_length; ++i) { | 793 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
814 | if (ludp->connections[i].status == 1 || ludp->connections[i].status == 2) | 794 | if (tmp.status == 1 || tmp.status == 2) |
815 | ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; | 795 | tmp.SYNC_rate = MAX_SYNC_RATE; |
816 | 796 | ||
817 | if (ludp->connections[i].status == 3) { | 797 | if (tmp.status == 3) { |
818 | if (sendqueue(ludp, i) != 0) { | 798 | if (sendqueue(ludp, i) != 0) { |
819 | ludp->connections[i].data_rate = (BUFFER_PACKET_NUM - ludp->connections[i].num_req_paquets) * MAX_SYNC_RATE; | 799 | tmp.data_rate = (BUFFER_PACKET_NUM - tmp.num_req_paquets) * MAX_SYNC_RATE; |
820 | ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; | 800 | tmp.SYNC_rate = MAX_SYNC_RATE; |
821 | } else if (ludp->connections[i].last_recvdata + 1000000UL > temp_time) | 801 | } else if (tmp.last_recvdata + 1000000UL > temp_time) |
822 | ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; | 802 | tmp.SYNC_rate = MAX_SYNC_RATE; |
823 | else | 803 | else |
824 | ludp->connections[i].SYNC_rate = SYNC_RATE; | 804 | tmp.SYNC_rate = SYNC_RATE; |
825 | } | 805 | } |
826 | } | 806 | } |
827 | } | 807 | } |
@@ -837,6 +817,6 @@ void do_lossless_udp(Lossless_UDP *ludp) | |||
837 | 817 | ||
838 | void kill_lossless_udp(Lossless_UDP *ludp) | 818 | void kill_lossless_udp(Lossless_UDP *ludp) |
839 | { | 819 | { |
840 | free(ludp->connections); | 820 | tox_array_delete(&ludp->connections); |
841 | free(ludp); | 821 | free(ludp); |
842 | } | 822 | } |