diff options
author | irungentoo <irungentoo@gmail.com> | 2015-03-11 15:37:25 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-03-11 15:37:25 -0400 |
commit | 43fe6e71bd06dd4fcc2bb49662f56b493ed9b6fc (patch) | |
tree | e7b36ae30312bbe11e2e74f7a40538ac84056f42 | |
parent | 572484f06ec75df61960077b033a9c7e4d28d283 (diff) |
tox_callback_connection_status() implemented.
Attempted fix of connection checking to make it more stable.
-rw-r--r-- | auto_tests/tox_test.c | 34 | ||||
-rw-r--r-- | toxcore/Messenger.c | 17 | ||||
-rw-r--r-- | toxcore/Messenger.h | 9 | ||||
-rw-r--r-- | toxcore/onion_client.c | 113 | ||||
-rw-r--r-- | toxcore/onion_client.h | 8 | ||||
-rw-r--r-- | toxcore/tox.c | 15 |
6 files changed, 137 insertions, 59 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index d3f9472a..7cae9da6 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c | |||
@@ -202,6 +202,18 @@ void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t p | |||
202 | } | 202 | } |
203 | } | 203 | } |
204 | 204 | ||
205 | unsigned int connected_t1; | ||
206 | void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | ||
207 | { | ||
208 | if (*((uint32_t *)user_data) != 974536) | ||
209 | return; | ||
210 | |||
211 | if (connected_t1 && !connection_status) | ||
212 | ck_abort_msg("Tox went offline"); | ||
213 | |||
214 | connected_t1 = connection_status; | ||
215 | } | ||
216 | |||
205 | START_TEST(test_one) | 217 | START_TEST(test_one) |
206 | { | 218 | { |
207 | Tox *tox1 = tox_new(0, 0, 0, 0); | 219 | Tox *tox1 = tox_new(0, 0, 0, 0); |
@@ -286,6 +298,8 @@ START_TEST(test_few_clients) | |||
286 | } | 298 | } |
287 | 299 | ||
288 | uint32_t to_compare = 974536; | 300 | uint32_t to_compare = 974536; |
301 | connected_t1 = 0; | ||
302 | tox_callback_connection_status(tox1, tox_connection_status, &to_compare); | ||
289 | tox_callback_friend_request(tox2, accept_friend_request, &to_compare); | 303 | tox_callback_friend_request(tox2, accept_friend_request, &to_compare); |
290 | uint8_t address[TOX_ADDRESS_SIZE]; | 304 | uint8_t address[TOX_ADDRESS_SIZE]; |
291 | tox_self_get_address(tox2, address); | 305 | tox_self_get_address(tox2, address); |
@@ -299,20 +313,22 @@ START_TEST(test_few_clients) | |||
299 | tox_iteration(tox2); | 313 | tox_iteration(tox2); |
300 | tox_iteration(tox3); | 314 | tox_iteration(tox3); |
301 | 315 | ||
302 | if (tox_get_connection_status(tox1) && tox_get_connection_status(tox2) && tox_get_connection_status(tox3) && off) { | 316 | if (tox_get_connection_status(tox1) && tox_get_connection_status(tox2) && tox_get_connection_status(tox3)) { |
303 | printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); | 317 | if (off) { |
304 | con_time = time(NULL); | 318 | printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); |
305 | off = 0; | 319 | con_time = time(NULL); |
306 | } | 320 | off = 0; |
307 | 321 | } | |
308 | 322 | ||
309 | if (tox_friend_get_connection_status(tox2, 0, 0) == TOX_CONNECTION_UDP | 323 | if (tox_friend_get_connection_status(tox2, 0, 0) == TOX_CONNECTION_UDP |
310 | && tox_friend_get_connection_status(tox3, 0, 0) == TOX_CONNECTION_UDP) | 324 | && tox_friend_get_connection_status(tox3, 0, 0) == TOX_CONNECTION_UDP) |
311 | break; | 325 | break; |
326 | } | ||
312 | 327 | ||
313 | c_sleep(50); | 328 | c_sleep(50); |
314 | } | 329 | } |
315 | 330 | ||
331 | ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); | ||
316 | printf("tox clients connected took %llu seconds\n", time(NULL) - con_time); | 332 | printf("tox clients connected took %llu seconds\n", time(NULL) - con_time); |
317 | to_compare = 974536; | 333 | to_compare = 974536; |
318 | tox_callback_friend_message(tox3, print_message, &to_compare); | 334 | tox_callback_friend_message(tox3, print_message, &to_compare); |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 08b62660..546fe4a8 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -865,6 +865,12 @@ void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, ui | |||
865 | m->friend_connectionstatuschange_userdata = userdata; | 865 | m->friend_connectionstatuschange_userdata = userdata; |
866 | } | 866 | } |
867 | 867 | ||
868 | void m_callback_core_connection(Messenger *m, void (*function)(Messenger *m, unsigned int, void *), void *userdata) | ||
869 | { | ||
870 | m->core_connection_change = function; | ||
871 | m->core_connection_change_userdata = userdata; | ||
872 | } | ||
873 | |||
868 | void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, uint32_t, uint8_t, void *), | 874 | void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, uint32_t, uint8_t, void *), |
869 | void *userdata) | 875 | void *userdata) |
870 | { | 876 | { |
@@ -2047,7 +2053,17 @@ void do_friends(Messenger *m) | |||
2047 | } | 2053 | } |
2048 | } | 2054 | } |
2049 | 2055 | ||
2056 | static void connection_status_cb(Messenger *m) | ||
2057 | { | ||
2058 | unsigned int conn_status = onion_connection_status(m->onion_c); | ||
2050 | 2059 | ||
2060 | if (conn_status != m->last_connection_status) { | ||
2061 | if (m->core_connection_change) | ||
2062 | (*m->core_connection_change)(m, conn_status, m->core_connection_change_userdata); | ||
2063 | |||
2064 | m->last_connection_status = conn_status; | ||
2065 | } | ||
2066 | } | ||
2051 | 2067 | ||
2052 | 2068 | ||
2053 | #ifdef LOGGING | 2069 | #ifdef LOGGING |
@@ -2112,6 +2128,7 @@ void do_messenger(Messenger *m) | |||
2112 | do_friend_connections(m->fr_c); | 2128 | do_friend_connections(m->fr_c); |
2113 | do_friends(m); | 2129 | do_friends(m); |
2114 | LANdiscovery(m); | 2130 | LANdiscovery(m); |
2131 | connection_status_cb(m); | ||
2115 | 2132 | ||
2116 | #ifdef LOGGING | 2133 | #ifdef LOGGING |
2117 | 2134 | ||
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 5215f989..ae80760f 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -271,6 +271,10 @@ struct Messenger { | |||
271 | void (*lossless_packethandler)(struct Messenger *m, uint32_t, const uint8_t *, size_t, void *); | 271 | void (*lossless_packethandler)(struct Messenger *m, uint32_t, const uint8_t *, size_t, void *); |
272 | void *lossless_packethandler_userdata; | 272 | void *lossless_packethandler_userdata; |
273 | 273 | ||
274 | void (*core_connection_change)(struct Messenger *m, unsigned int, void *); | ||
275 | void *core_connection_change_userdata; | ||
276 | unsigned int last_connection_status; | ||
277 | |||
274 | Messenger_Options options; | 278 | Messenger_Options options; |
275 | }; | 279 | }; |
276 | 280 | ||
@@ -543,6 +547,11 @@ void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Mess | |||
543 | void *userdata); | 547 | void *userdata); |
544 | 548 | ||
545 | 549 | ||
550 | /* Set the callback for typing changes. | ||
551 | * Function(unsigned int connection_status (0 = not connected, 1 = TCP only, 2 = UDP + TCP)) | ||
552 | */ | ||
553 | void m_callback_core_connection(Messenger *m, void (*function)(Messenger *m, unsigned int, void *), void *userdata); | ||
554 | |||
546 | /**********GROUP CHATS************/ | 555 | /**********GROUP CHATS************/ |
547 | 556 | ||
548 | /* Set the callback for group invites. | 557 | /* Set the callback for group invites. |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 3643cb2f..a9fc1643 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -1351,6 +1351,64 @@ static void do_announce(Onion_Client *onion_c) | |||
1351 | } | 1351 | } |
1352 | } | 1352 | } |
1353 | 1353 | ||
1354 | /* return 0 if we are not connected to the network. | ||
1355 | * return 1 if we are. | ||
1356 | */ | ||
1357 | static int onion_isconnected(const Onion_Client *onion_c) | ||
1358 | { | ||
1359 | unsigned int i, num = 0, announced = 0; | ||
1360 | |||
1361 | if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) | ||
1362 | return 0; | ||
1363 | |||
1364 | if (onion_c->path_nodes_index == 0) | ||
1365 | return 0; | ||
1366 | |||
1367 | for (i = 0; i < MAX_ONION_CLIENTS; ++i) { | ||
1368 | if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) { | ||
1369 | ++num; | ||
1370 | |||
1371 | if (onion_c->clients_announce_list[i].is_stored) { | ||
1372 | ++announced; | ||
1373 | } | ||
1374 | } | ||
1375 | } | ||
1376 | |||
1377 | unsigned int pnodes = onion_c->path_nodes_index; | ||
1378 | |||
1379 | if (pnodes > MAX_ONION_CLIENTS) { | ||
1380 | pnodes = MAX_ONION_CLIENTS; | ||
1381 | } | ||
1382 | |||
1383 | /* Consider ourselves online if we are announced to half or more nodes | ||
1384 | we are connected to */ | ||
1385 | if (num && announced) { | ||
1386 | if ((num / 2) <= announced && (pnodes / 2) <= num) | ||
1387 | return 1; | ||
1388 | } | ||
1389 | |||
1390 | return 0; | ||
1391 | } | ||
1392 | |||
1393 | #define ONION_CONNECTION_SECONDS 2 | ||
1394 | |||
1395 | /* return 0 if we are not connected to the network. | ||
1396 | * return 1 if we are connected with TCP only. | ||
1397 | * return 2 if we are also connected with UDP. | ||
1398 | */ | ||
1399 | unsigned int onion_connection_status(const Onion_Client *onion_c) | ||
1400 | { | ||
1401 | if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) { | ||
1402 | if (onion_c->UDP_connected) { | ||
1403 | return 2; | ||
1404 | } else { | ||
1405 | return 1; | ||
1406 | } | ||
1407 | } | ||
1408 | |||
1409 | return 0; | ||
1410 | } | ||
1411 | |||
1354 | void do_onion_client(Onion_Client *onion_c) | 1412 | void do_onion_client(Onion_Client *onion_c) |
1355 | { | 1413 | { |
1356 | unsigned int i; | 1414 | unsigned int i; |
@@ -1363,11 +1421,23 @@ void do_onion_client(Onion_Client *onion_c) | |||
1363 | do_announce(onion_c); | 1421 | do_announce(onion_c); |
1364 | 1422 | ||
1365 | if (onion_isconnected(onion_c)) { | 1423 | if (onion_isconnected(onion_c)) { |
1366 | for (i = 0; i < onion_c->num_friends; ++i) { | 1424 | if (onion_c->onion_connected < ONION_CONNECTION_SECONDS * 2) { |
1367 | do_friend(onion_c, i); | 1425 | ++onion_c->onion_connected; |
1368 | } | 1426 | } |
1427 | |||
1428 | onion_c->UDP_connected = DHT_non_lan_connected(onion_c->dht); | ||
1369 | } else { | 1429 | } else { |
1370 | populate_path_nodes_tcp(onion_c); | 1430 | populate_path_nodes_tcp(onion_c); |
1431 | |||
1432 | if (onion_c->onion_connected != 0) { | ||
1433 | --onion_c->onion_connected; | ||
1434 | } | ||
1435 | } | ||
1436 | |||
1437 | if (onion_connection_status(onion_c)) { | ||
1438 | for (i = 0; i < onion_c->num_friends; ++i) { | ||
1439 | do_friend(onion_c, i); | ||
1440 | } | ||
1371 | } | 1441 | } |
1372 | 1442 | ||
1373 | onion_c->last_run = unix_time(); | 1443 | onion_c->last_run = unix_time(); |
@@ -1418,42 +1488,3 @@ void kill_onion_client(Onion_Client *onion_c) | |||
1418 | free(onion_c); | 1488 | free(onion_c); |
1419 | } | 1489 | } |
1420 | 1490 | ||
1421 | |||
1422 | /* return 0 if we are not connected to the network. | ||
1423 | * return 1 if we are. | ||
1424 | */ | ||
1425 | int onion_isconnected(const Onion_Client *onion_c) | ||
1426 | { | ||
1427 | unsigned int i, num = 0, announced = 0; | ||
1428 | |||
1429 | if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) | ||
1430 | return 0; | ||
1431 | |||
1432 | if (onion_c->path_nodes_index == 0) | ||
1433 | return 0; | ||
1434 | |||
1435 | for (i = 0; i < MAX_ONION_CLIENTS; ++i) { | ||
1436 | if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) { | ||
1437 | ++num; | ||
1438 | |||
1439 | if (onion_c->clients_announce_list[i].is_stored) { | ||
1440 | ++announced; | ||
1441 | } | ||
1442 | } | ||
1443 | } | ||
1444 | |||
1445 | unsigned int pnodes = onion_c->path_nodes_index; | ||
1446 | |||
1447 | if (pnodes > MAX_ONION_CLIENTS) { | ||
1448 | pnodes = MAX_ONION_CLIENTS; | ||
1449 | } | ||
1450 | |||
1451 | /* Consider ourselves online if we are announced to half or more nodes | ||
1452 | we are connected to */ | ||
1453 | if (num && announced) { | ||
1454 | if ((num / 2) <= announced && (pnodes / 2) <= num) | ||
1455 | return 1; | ||
1456 | } | ||
1457 | |||
1458 | return 0; | ||
1459 | } | ||
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 6851d929..e10a86c5 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h | |||
@@ -157,6 +157,9 @@ typedef struct { | |||
157 | } Onion_Data_Handlers[256]; | 157 | } Onion_Data_Handlers[256]; |
158 | 158 | ||
159 | uint64_t last_packet_recv; | 159 | uint64_t last_packet_recv; |
160 | |||
161 | unsigned int onion_connected; | ||
162 | _Bool UDP_connected; | ||
160 | } Onion_Client; | 163 | } Onion_Client; |
161 | 164 | ||
162 | 165 | ||
@@ -278,8 +281,9 @@ void kill_onion_client(Onion_Client *onion_c); | |||
278 | 281 | ||
279 | 282 | ||
280 | /* return 0 if we are not connected to the network. | 283 | /* return 0 if we are not connected to the network. |
281 | * return 1 if we are. | 284 | * return 1 if we are connected with TCP only. |
285 | * return 2 if we are also connected with UDP. | ||
282 | */ | 286 | */ |
283 | int onion_isconnected(const Onion_Client *onion_c); | 287 | unsigned int onion_connection_status(const Onion_Client *onion_c); |
284 | 288 | ||
285 | #endif | 289 | #endif |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 6bb20c81..554414b1 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -278,21 +278,22 @@ TOX_CONNECTION tox_get_connection_status(const Tox *tox) | |||
278 | { | 278 | { |
279 | const Messenger *m = tox; | 279 | const Messenger *m = tox; |
280 | 280 | ||
281 | if (onion_isconnected(m->onion_c)) { | 281 | unsigned int ret = onion_connection_status(m->onion_c); |
282 | if (DHT_non_lan_connected(m->dht)) { | ||
283 | return TOX_CONNECTION_UDP; | ||
284 | } | ||
285 | 282 | ||
283 | if (ret == 2) { | ||
284 | return TOX_CONNECTION_UDP; | ||
285 | } else if (ret == 1) { | ||
286 | return TOX_CONNECTION_TCP; | 286 | return TOX_CONNECTION_TCP; |
287 | } else { | ||
288 | return TOX_CONNECTION_NONE; | ||
287 | } | 289 | } |
288 | |||
289 | return TOX_CONNECTION_NONE; | ||
290 | } | 290 | } |
291 | 291 | ||
292 | 292 | ||
293 | void tox_callback_connection_status(Tox *tox, tox_connection_status_cb *function, void *user_data) | 293 | void tox_callback_connection_status(Tox *tox, tox_connection_status_cb *function, void *user_data) |
294 | { | 294 | { |
295 | //TODO | 295 | Messenger *m = tox; |
296 | m_callback_core_connection(m, function, user_data); | ||
296 | } | 297 | } |
297 | 298 | ||
298 | uint32_t tox_iteration_interval(const Tox *tox) | 299 | uint32_t tox_iteration_interval(const Tox *tox) |