summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-02-19 20:51:19 -0500
committerirungentoo <irungentoo@gmail.com>2015-02-19 20:51:19 -0500
commitc87da9203e09cf1c7e0f19fc397808424fdaeea2 (patch)
tree6b87ef8608ccfcf77e6fd5eaf6e8390b8ea51675
parent53d0eb8937b32fa638bf2f5d36d0da85975ebde7 (diff)
Tox connection function and callback should now work as expected.
Changes in Messenger to call the callback every time the connection type changes from TCP to UDP.
-rw-r--r--toxcore/Messenger.c36
-rw-r--r--toxcore/Messenger.h12
-rw-r--r--toxcore/tox.c26
3 files changed, 67 insertions, 7 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index ced02a12..11da5951 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -358,7 +358,19 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
358 if (friend_not_valid(m, friendnumber)) 358 if (friend_not_valid(m, friendnumber))
359 return -1; 359 return -1;
360 360
361 return m->friendlist[friendnumber].status == FRIEND_ONLINE; 361 if (m->friendlist[friendnumber].status == FRIEND_ONLINE) {
362 uint8_t direct_connected = 0;
363 crypto_connection_status(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
364 m->friendlist[friendnumber].friendcon_id), &direct_connected);
365
366 if (direct_connected) {
367 return CONNECTION_UDP;
368 } else {
369 return CONNECTION_TCP;
370 }
371 } else {
372 return CONNECTION_NONE;
373 }
362} 374}
363 375
364int m_friend_exists(const Messenger *m, int32_t friendnumber) 376int m_friend_exists(const Messenger *m, int32_t friendnumber)
@@ -969,6 +981,23 @@ void m_callback_avatar_data(Messenger *m, void (*function)(Messenger *m, uint32_
969 m->avatar_data_recv_userdata = userdata; 981 m->avatar_data_recv_userdata = userdata;
970} 982}
971 983
984static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber)
985{
986 int last_connection_udp_tcp = m->friendlist[friendnumber].last_connection_udp_tcp;
987
988 int ret = m_get_friend_connectionstatus(m, friendnumber);
989
990 if (ret == -1)
991 return;
992
993 if (last_connection_udp_tcp != ret) {
994 if (m->friend_connectionstatuschange)
995 m->friend_connectionstatuschange(m, friendnumber, ret, m->friend_connectionstatuschange_userdata);
996 }
997
998 m->friendlist[friendnumber].last_connection_udp_tcp = ret;
999}
1000
972static void break_files(const Messenger *m, int32_t friendnumber); 1001static void break_files(const Messenger *m, int32_t friendnumber);
973static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status) 1002static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status)
974{ 1003{
@@ -988,8 +1017,7 @@ static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, ui
988 1017
989 m->friendlist[friendnumber].status = status; 1018 m->friendlist[friendnumber].status = status;
990 1019
991 if (m->friend_connectionstatuschange) 1020 check_friend_tcp_udp(m, friendnumber);
992 m->friend_connectionstatuschange(m, friendnumber, is_online, m->friend_connectionstatuschange_userdata);
993 1021
994 if (m->friend_connectionstatuschange_internal) 1022 if (m->friend_connectionstatuschange_internal)
995 m->friend_connectionstatuschange_internal(m, friendnumber, is_online, 1023 m->friend_connectionstatuschange_internal(m, friendnumber, is_online,
@@ -2316,6 +2344,8 @@ void do_friends(Messenger *m)
2316 if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) { 2344 if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) {
2317 send_relays(m, i); 2345 send_relays(m, i);
2318 } 2346 }
2347
2348 check_friend_tcp_udp(m, i);
2319 } 2349 }
2320 } 2350 }
2321} 2351}
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 4e709f64..da1bbe9f 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -114,6 +114,12 @@ enum {
114#define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */ 114#define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */
115 115
116 116
117enum {
118 CONNECTION_NONE,
119 CONNECTION_TCP,
120 CONNECTION_UDP
121};
122
117/* USERSTATUS - 123/* USERSTATUS -
118 * Represents userstatuses someone can have. 124 * Represents userstatuses someone can have.
119 */ 125 */
@@ -215,6 +221,7 @@ typedef struct {
215 uint32_t friendrequest_nospam; // The nospam number used in the friend request. 221 uint32_t friendrequest_nospam; // The nospam number used in the friend request.
216 uint64_t ping_lastrecv;//TODO remove 222 uint64_t ping_lastrecv;//TODO remove
217 uint64_t share_relays_lastsent; 223 uint64_t share_relays_lastsent;
224 uint8_t last_connection_udp_tcp;
218 struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; 225 struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES];
219 struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; 226 struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES];
220 227
@@ -372,8 +379,9 @@ int m_delfriend(Messenger *m, int32_t friendnumber);
372 379
373/* Checks friend's connecting status. 380/* Checks friend's connecting status.
374 * 381 *
375 * return 1 if friend is connected to us (Online). 382 * return CONNECTION_UDP (2) if friend is directly connected to us (Online UDP).
376 * return 0 if friend is not connected to us (Offline). 383 * return CONNECTION_TCP (1) if friend is connected to us (Online TCP).
384 * return CONNECTION_NONE (0) if friend is not connected to us (Offline).
377 * return -1 on failure. 385 * return -1 on failure.
378 */ 386 */
379int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber); 387int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber);
diff --git a/toxcore/tox.c b/toxcore/tox.c
index b166d151..db100459 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -647,7 +647,7 @@ void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb *
647 m_callback_statusmessage(m, function, user_data); 647 m_callback_statusmessage(m, function, user_data);
648} 648}
649 649
650TOX_STATUS tox_friend_get_status(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error) 650TOX_STATUS tox_friend_get_status(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error)
651{ 651{
652 const Messenger *m = tox; 652 const Messenger *m = tox;
653 653
@@ -668,10 +668,32 @@ void tox_callback_friend_status(Tox *tox, tox_friend_status_cb *function, void *
668 m_callback_userstatus(m, function, user_data); 668 m_callback_userstatus(m, function, user_data);
669} 669}
670 670
671bool tox_friend_get_typing(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error) 671TOX_CONNECTION tox_friend_get_connection_status(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error)
672{
673 const Messenger *m = tox;
674
675 int ret = m_get_friend_connectionstatus(m, friend_number);
676
677 if (ret == -1) {
678 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
679 return TOX_CONNECTION_NONE;
680 }
681
682 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
683 return ret;
684}
685
686void tox_callback_friend_connection_status(Tox *tox, tox_friend_connection_status_cb *function, void *user_data)
687{
688 Messenger *m = tox;
689 m_callback_connectionstatus(m, function, user_data);
690}
691
692bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error)
672{ 693{
673 const Messenger *m = tox; 694 const Messenger *m = tox;
674 int ret = m_get_istyping(m, friend_number); 695 int ret = m_get_istyping(m, friend_number);
696
675 if (ret == -1) { 697 if (ret == -1) {
676 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND); 698 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
677 return 0; 699 return 0;