summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-08 15:41:13 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-08 18:06:39 +0000
commit819fe534ea62b07340b20f18841d1ed93cf5caff (patch)
treeab2dd90a2fb8df0d3de8f1ff4ebff5726728fce7
parentb3889f0f052b450225eeb02826efcfea82ae9590 (diff)
Use named function types for friend_connection callbacks.
Also: * No inner structs. * One declarator per member declaration. * Function names are snake_case. * Names ending in `_cb` are function types. * `++i` is preferred over `i++`.
-rw-r--r--toxcore/friend_connection.c68
-rw-r--r--toxcore/friend_connection.h16
2 files changed, 46 insertions, 38 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 62f76b22..64a04773 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -35,29 +35,33 @@
35 35
36#define PORTS_PER_DISCOVERY 10 36#define PORTS_PER_DISCOVERY 10
37 37
38typedef struct { 38typedef struct Friend_Conn_Callbacks {
39 fc_status_cb *status_callback;
40 fc_data_cb *data_callback;
41 fc_lossy_data_cb *lossy_data_callback;
42
43 void *callback_object;
44 int callback_id;
45} Friend_Conn_Callbacks;
46
47typedef struct Friend_Conn {
39 uint8_t status; 48 uint8_t status;
40 49
41 uint8_t real_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 50 uint8_t real_public_key[CRYPTO_PUBLIC_KEY_SIZE];
42 uint8_t dht_temp_pk[CRYPTO_PUBLIC_KEY_SIZE]; 51 uint8_t dht_temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
43 uint16_t dht_lock; 52 uint16_t dht_lock;
44 IP_Port dht_ip_port; 53 IP_Port dht_ip_port;
45 uint64_t dht_pk_lastrecv, dht_ip_port_lastrecv; 54 uint64_t dht_pk_lastrecv;
55 uint64_t dht_ip_port_lastrecv;
46 56
47 int onion_friendnum; 57 int onion_friendnum;
48 int crypt_connection_id; 58 int crypt_connection_id;
49 59
50 uint64_t ping_lastrecv, ping_lastsent; 60 uint64_t ping_lastrecv;
61 uint64_t ping_lastsent;
51 uint64_t share_relays_lastsent; 62 uint64_t share_relays_lastsent;
52 63
53 struct { 64 Friend_Conn_Callbacks callbacks[MAX_FRIEND_CONNECTION_CALLBACKS];
54 int (*status_callback)(void *object, int id, uint8_t status, void *userdata);
55 int (*data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
56 int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
57
58 void *callback_object;
59 int callback_id;
60 } callbacks[MAX_FRIEND_CONNECTION_CALLBACKS];
61 65
62 uint16_t lock_count; 66 uint16_t lock_count;
63 67
@@ -76,12 +80,11 @@ struct Friend_Connections {
76 Friend_Conn *conns; 80 Friend_Conn *conns;
77 uint32_t num_cons; 81 uint32_t num_cons;
78 82
79 int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len, 83 fr_request_cb *fr_request_callback;
80 void *userdata);
81 void *fr_request_object; 84 void *fr_request_object;
82 85
83 uint64_t last_LANdiscovery; 86 uint64_t last_lan_discovery;
84 uint16_t next_LANport; 87 uint16_t next_lan_port;
85 88
86 bool local_discovery_enabled; 89 bool local_discovery_enabled;
87}; 90};
@@ -375,17 +378,17 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat
375 return -1; 378 return -1;
376 } 379 }
377 380
378 bool call_cb = 0; 381 bool status_changed = 0;
379 382
380 if (status) { /* Went online. */ 383 if (status) { /* Went online. */
381 call_cb = 1; 384 status_changed = 1;
382 friend_con->status = FRIENDCONN_STATUS_CONNECTED; 385 friend_con->status = FRIENDCONN_STATUS_CONNECTED;
383 friend_con->ping_lastrecv = unix_time(); 386 friend_con->ping_lastrecv = unix_time();
384 friend_con->share_relays_lastsent = 0; 387 friend_con->share_relays_lastsent = 0;
385 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); 388 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
386 } else { /* Went offline. */ 389 } else { /* Went offline. */
387 if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { 390 if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) {
388 call_cb = 1; 391 status_changed = 1;
389 friend_con->dht_pk_lastrecv = unix_time(); 392 friend_con->dht_pk_lastrecv = unix_time();
390 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); 393 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
391 } 394 }
@@ -395,7 +398,7 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat
395 friend_con->hosting_tcp_relay = 0; 398 friend_con->hosting_tcp_relay = 0;
396 } 399 }
397 400
398 if (call_cb) { 401 if (status_changed) {
399 unsigned int i; 402 unsigned int i;
400 403
401 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { 404 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
@@ -471,7 +474,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
471 return -1; 474 return -1;
472 } 475 }
473 476
474 for (int j = 0; j < n; j++) { 477 for (int j = 0; j < n; ++j) {
475 friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); 478 friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
476 } 479 }
477 480
@@ -686,9 +689,9 @@ void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *
686 * return -1 on failure 689 * return -1 on failure
687 */ 690 */
688int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index, 691int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index,
689 int (*status_callback)(void *object, int id, uint8_t status, void *userdata), 692 fc_status_cb *status_callback,
690 int (*data_callback)(void *object, int id, const uint8_t *data, uint16_t len, void *userdata), 693 fc_data_cb *data_callback,
691 int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), 694 fc_lossy_data_cb *lossy_data_callback,
692 void *object, int number) 695 void *object, int number)
693{ 696{
694 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); 697 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
@@ -800,8 +803,7 @@ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id)
800 * 803 *
801 * This function will be called every time a friend request packet is received. 804 * This function will be called every time a friend request packet is received.
802 */ 805 */
803void set_friend_request_callback(Friend_Connections *fr_c, int (*fr_request_callback)(void *, const uint8_t *, 806void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object)
804 const uint8_t *, uint16_t, void *), void *object)
805{ 807{
806 fr_c->fr_request_callback = fr_request_callback; 808 fr_c->fr_request_callback = fr_request_callback;
807 fr_c->fr_request_object = object; 809 fr_c->fr_request_object = object;
@@ -864,7 +866,7 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
864 temp->onion_c = onion_c; 866 temp->onion_c = onion_c;
865 temp->local_discovery_enabled = local_discovery_enabled; 867 temp->local_discovery_enabled = local_discovery_enabled;
866 // Don't include default port in port range 868 // Don't include default port in port range
867 temp->next_LANport = TOX_PORTRANGE_FROM + 1; 869 temp->next_lan_port = TOX_PORTRANGE_FROM + 1;
868 870
869 new_connection_handler(temp->net_crypto, &handle_new_connections, temp); 871 new_connection_handler(temp->net_crypto, &handle_new_connections, temp);
870 872
@@ -876,10 +878,10 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
876} 878}
877 879
878/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 880/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
879static void LANdiscovery(Friend_Connections *fr_c) 881static void lan_discovery(Friend_Connections *fr_c)
880{ 882{
881 if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 883 if (fr_c->last_lan_discovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
882 const uint16_t first = fr_c->next_LANport; 884 const uint16_t first = fr_c->next_lan_port;
883 uint16_t last = first + PORTS_PER_DISCOVERY; 885 uint16_t last = first + PORTS_PER_DISCOVERY;
884 last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; 886 last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;
885 887
@@ -887,13 +889,13 @@ static void LANdiscovery(Friend_Connections *fr_c)
887 lan_discovery_send(net_htons(TOX_PORT_DEFAULT), fr_c->dht); 889 lan_discovery_send(net_htons(TOX_PORT_DEFAULT), fr_c->dht);
888 890
889 // And check some extra ports 891 // And check some extra ports
890 for (uint16_t port = first; port < last; port++) { 892 for (uint16_t port = first; port < last; ++port) {
891 lan_discovery_send(net_htons(port), fr_c->dht); 893 lan_discovery_send(net_htons(port), fr_c->dht);
892 } 894 }
893 895
894 // Don't include default port in port range 896 // Don't include default port in port range
895 fr_c->next_LANport = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1; 897 fr_c->next_lan_port = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1;
896 fr_c->last_LANdiscovery = unix_time(); 898 fr_c->last_lan_discovery = unix_time();
897 } 899 }
898} 900}
899 901
@@ -945,7 +947,7 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata)
945 } 947 }
946 948
947 if (fr_c->local_discovery_enabled) { 949 if (fr_c->local_discovery_enabled) {
948 LANdiscovery(fr_c); 950 lan_discovery(fr_c);
949 } 951 }
950} 952}
951 953
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h
index f4d53c7a..a29cc7be 100644
--- a/toxcore/friend_connection.h
+++ b/toxcore/friend_connection.h
@@ -101,6 +101,10 @@ void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *
101 */ 101 */
102int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key); 102int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key);
103 103
104typedef int fc_status_cb(void *object, int id, uint8_t status, void *userdata);
105typedef int fc_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
106typedef int fc_lossy_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
107
104/* Set the callbacks for the friend connection. 108/* Set the callbacks for the friend connection.
105 * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array. 109 * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array.
106 * 110 *
@@ -108,9 +112,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
108 * return -1 on failure 112 * return -1 on failure
109 */ 113 */
110int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index, 114int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index,
111 int (*status_callback)(void *object, int id, uint8_t status, void *userdata), 115 fc_status_cb *status_callback,
112 int (*data_callback)(void *object, int id, const uint8_t *data, uint16_t len, void *userdata), 116 fc_data_cb *data_callback,
113 int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), 117 fc_lossy_data_cb *lossy_data_callback,
114 void *object, int number); 118 void *object, int number);
115 119
116/* return the crypt_connection_id for the connection. 120/* return the crypt_connection_id for the connection.
@@ -144,12 +148,14 @@ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id);
144int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data, 148int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data,
145 uint16_t length); 149 uint16_t length);
146 150
151typedef int fr_request_cb(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len,
152 void *userdata);
153
147/* Set friend request callback. 154/* Set friend request callback.
148 * 155 *
149 * This function will be called every time a friend request is received. 156 * This function will be called every time a friend request is received.
150 */ 157 */
151void set_friend_request_callback(Friend_Connections *fr_c, int (*fr_request_callback)(void *, const uint8_t *, 158void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object);
152 const uint8_t *, uint16_t, void *), void *object);
153 159
154/* Create new friend_connections instance. */ 160/* Create new friend_connections instance. */
155Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled); 161Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled);