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