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