-
Notifications
You must be signed in to change notification settings - Fork 6
/
nctx.cpp
352 lines (332 loc) · 12.9 KB
/
nctx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "nctx.h"
#include <time.h>
extern sockaddr_in addr(unsigned int ip, unsigned short int port);
nctx::nctx(int socket): _SOCKET(socket){}
nctx::~nctx()
{
_tx_session_info.perform_for_all_data([](tx_session_info* session){
delete session;
session = nullptr;
});
}
tx_session_info::tx_session_info(unsigned int client_ip, unsigned short int cport, BLOCK_SIZE block_size, unsigned int retransmission_interval, unsigned char redundancy):\
_DATA_ADDR(addr(client_ip, cport)), _max_block_size(block_size), _retransmission_interval(retransmission_interval) {
_state = tx_session_info::STATE::INIT_FAILURE;
try{
_buffer = new NetworkCodingPktBuffer [_max_block_size];
for(unsigned char i = 0 ; i < _max_block_size ; i++)
{
memset(_buffer[i].buffer, 0x0, sizeof(NetworkCodingPktBuffer));
}
}catch(std::exception ex){
_buffer = nullptr;
return;
}
try{
_rand_coef = new unsigned char [_max_block_size];
memset(_rand_coef, 0x0, _max_block_size);
}catch(std::exception ex){
delete [] _buffer;
return;
}
_tx_cnt = 0;
_largest_pkt_size = 0;
_blk_seq = 0;
_loss_rate = 0.;
_redundancy = redundancy;
_retransmission_in_progress = false;
_state = tx_session_info::STATE::INIT_SUCCESS;
}
tx_session_info::~tx_session_info()
{
if(_state == tx_session_info::STATE::INIT_FAILURE)
{
return;
}
_retransmission_in_progress = false;
delete [] _buffer;
delete [] _rand_coef;
}
/*
* Send remedy packets.
*/
bool nctx::_send_remedy_pkt(tx_session_info * const info)
{
if(info->_retransmission_in_progress == false)
{
return false;
}
if(info->_tx_cnt == 0)
{
return (int)GET_OUTER_SIZE(info->_buffer[0].buffer) == sendto(_SOCKET, info->_buffer[0].buffer, GET_OUTER_SIZE(info->_buffer[0].buffer), 0, (sockaddr*)&info->_DATA_ADDR, sizeof(info->_DATA_ADDR));
}
memset(info->_remedy_pkt.buffer, 0x0, TOTAL_HEADER_SIZE(info->_max_block_size)+MAX_PAYLOAD_SIZE(info->_max_block_size));
// Generate random cofficient.
for(unsigned int code_id = 0 ; code_id < info->_max_block_size ; code_id++)
{
// I want all packets are encoded for remedy pkts, i.e. coefficients are greater than 0.
info->_rand_coef[code_id] = code_id <= info->_tx_cnt?1+rand()%255:0;
}
// Fill-in header information
GET_OUTER_TYPE(info->_remedy_pkt.buffer) = NC_PKT_TYPE::DATA_TYPE;
GET_OUTER_SIZE(info->_remedy_pkt.buffer) = TOTAL_HEADER_SIZE(info->_max_block_size)+info->_largest_pkt_size;
GET_OUTER_BLK_SEQ(info->_remedy_pkt.buffer) = info->_blk_seq;
GET_OUTER_BLK_SIZE(info->_remedy_pkt.buffer) = info->_tx_cnt;
GET_OUTER_MAX_BLK_SIZE(info->_remedy_pkt.buffer) = info->_max_block_size;
GET_OUTER_FLAGS(info->_remedy_pkt.buffer) = OuterHeader::FLAGS_END_OF_BLK;
if(info->_retransmission_in_progress == false)
{
return false;
}
// Encode packets.
for(unsigned int packet_id = 0 ; packet_id <= info->_tx_cnt ; packet_id++)
{
for(unsigned int coding_position = OUTER_HEADER_SIZE ;
coding_position < GET_OUTER_SIZE(info->_buffer[packet_id].buffer)/*NOTE: 0^x = 0*/ ;
coding_position++)
{
info->_remedy_pkt.buffer[coding_position] ^= FiniteField::instance()->mul(info->_buffer[packet_id].buffer[coding_position], info->_rand_coef[packet_id]);
}
}
if(info->_retransmission_in_progress == false)
{
return false;
}
const bool ret = (int)GET_OUTER_SIZE(info->_remedy_pkt.buffer) == sendto(_SOCKET, info->_remedy_pkt.buffer, GET_OUTER_SIZE(info->_remedy_pkt.buffer), 0, (sockaddr*)&info->_DATA_ADDR, sizeof(info->_DATA_ADDR));
return ret;
}
bool nctx::open_session(unsigned int client_ip, unsigned short int cport, BLOCK_SIZE block_size, unsigned int retransmission_interval, unsigned char redundancy)
{
tx_session_info* new_tx_session_info = nullptr;
const ip_port_key key = {client_ip, cport};
if(_tx_session_info.find(key) == nullptr)
{
try
{
new_tx_session_info = new tx_session_info(client_ip, cport, block_size, retransmission_interval, redundancy);
}
catch(std::exception ex)
{
return false;
}
if(new_tx_session_info->_state == tx_session_info::STATE::INIT_FAILURE)
{
delete new_tx_session_info;
return false;
}
if(_tx_session_info.insert(key, new_tx_session_info) == false)
{
delete new_tx_session_info;
return false;
}
}
return true;
}
bool nctx::connect_session(unsigned int client_ip, unsigned short int cport, unsigned char probes, unsigned int to)
{
const ip_port_key key = {client_ip, cport};
const clock_t timeout = clock() + to*(CLOCKS_PER_SEC/1000);
tx_session_info** const info = _tx_session_info.find(key);
if(info == nullptr)
{
return false;
}
(*info)->_is_connected = false;
for(unsigned char req = 0 ; req < probes ; req++)
{
Connect connect;
connect.type = NC_PKT_TYPE::REQ_CONNECT_TYPE;
if(sizeof(Connect) != sendto(_SOCKET, &connect, sizeof(Connect), 0, (sockaddr*)&(*info)->_DATA_ADDR, sizeof((*info)->_DATA_ADDR)))
{
std::cout<<"Could not send connection request\n";
}
}
while((*info)->_is_connected == false && clock() < timeout);
return (*info)->_is_connected;
}
void nctx::close_session(unsigned int client_ip, unsigned short int cport)
{
const ip_port_key key = {client_ip, cport};
tx_session_info** session = nullptr;
if((session = _tx_session_info.find(key)) != nullptr)
{
delete (*session);
_tx_session_info.remove(key);
}
}
/*
* Send data to the client.
* pkt: Pointer for data.
* pkt_size: Size of data.
*/
unsigned short int nctx::send(unsigned int client_ip, unsigned short int cport, unsigned char* pkt, unsigned short int pkt_size, const bool complete_block, const unsigned int ack_timeout, tx_session_param* new_param)
{
const ip_port_key key = {client_ip, cport};
tx_session_info** const session = _tx_session_info.find(key);
if(session == nullptr)
{
return 0;
}
(*session)->_lock.lock();
if((*session)->_state == tx_session_info::STATE::INIT_FAILURE)
{
(*session)->_lock.unlock();
return 0;
}
if(pkt_size > MAX_PAYLOAD_SIZE((*session)->_max_block_size))
{
// A single packet size is limitted to MAX_PAYLOAD_SIZE for the simplicity.
// Support of large data packet is my future work.
(*session)->_lock.unlock();
return 0;
}
if((*session)->_largest_pkt_size < pkt_size)
{
// _largest_pkt_size maintains the largest packet size of data packets with the same block ID.
// _largest_pkt_size is used to efficiently encode data packets.
// Note: Packets with the same block ID will be encoded together to repair random packet losses.
(*session)->_largest_pkt_size = pkt_size;
}
// Fill outer header
const unsigned char* pkt_buffer = (*session)->_buffer[(*session)->_tx_cnt].buffer;
const bool invoke_retransmission = \
((*session)->_tx_cnt == (*session)->_max_block_size - 1) || \
(complete_block == true) || \
(new_param!=nullptr);
GET_OUTER_TYPE(pkt_buffer) = NC_PKT_TYPE::DATA_TYPE;
GET_OUTER_SIZE(pkt_buffer) = TOTAL_HEADER_SIZE((*session)->_max_block_size)+pkt_size;
GET_OUTER_BLK_SEQ(pkt_buffer) = (*session)->_blk_seq;
GET_OUTER_BLK_SIZE(pkt_buffer) = (*session)->_tx_cnt;
GET_OUTER_MAX_BLK_SIZE(pkt_buffer) = (*session)->_max_block_size;
GET_OUTER_FLAGS(pkt_buffer) = OuterHeader::FLAGS_ORIGINAL;
GET_OUTER_FLAGS(pkt_buffer) = (invoke_retransmission?GET_OUTER_FLAGS(pkt_buffer) | OuterHeader::FLAGS_END_OF_BLK:GET_OUTER_FLAGS(pkt_buffer));
GET_INNER_SIZE(pkt_buffer) = pkt_size;
GET_INNER_LAST_INDICATOR(pkt_buffer) = 1; // I will support pkt_size larger than MAX_PAYLOAD_SIZE in the next phase.
memset(GET_INNER_CODE(pkt_buffer), 0x0, (*session)->_max_block_size);
GET_INNER_CODE(pkt_buffer)[(*session)->_tx_cnt] = 1; // Array index is 0 base.
memcpy(GET_INNER_PAYLOAD(pkt_buffer, (*session)->_max_block_size), pkt, pkt_size);
(*session)->_retransmission_in_progress = invoke_retransmission;
const unsigned char loss_rate = ((*session)->_redundancy.load()==0xff?(invoke_retransmission?(*session)->_loss_rate.load()+1:(unsigned char)0)
:
(*session)->_redundancy.load());
// Send data packet
sendto(_SOCKET, pkt_buffer, GET_OUTER_SIZE(pkt_buffer), 0, (sockaddr*)&(*session)->_DATA_ADDR, sizeof((*session)->_DATA_ADDR));
if(invoke_retransmission)
{
clock_t sent_time = 0;
clock_t current_time = 0;
for(unsigned char i = 0 ; i < loss_rate ; i++)
{
_send_remedy_pkt((*session));
}
const clock_t retransmission_timeout = clock() + (CLOCKS_PER_SEC/1000)*ack_timeout;
/**
* _redundancy: =0xff: Reliable transmission
* <0xff: Best effort transmission.
*/
while((*session)->_retransmission_in_progress && (*session)->_redundancy == 0xff && clock() < retransmission_timeout)
{
current_time = clock();
if((current_time - sent_time)/(CLOCKS_PER_SEC/1000) > (*session)->_retransmission_interval)
{
_send_remedy_pkt((*session));
sent_time = current_time;
}
}
if((*session)->_redundancy == 0xff && (*session)->_retransmission_in_progress)
{
(*session)->_lock.unlock();
return 0;
}
unsigned short next_blk_seq = 0;
do
{
next_blk_seq = rand();
}while(next_blk_seq == (*session)->_blk_seq);
(*session)->_blk_seq = next_blk_seq;
(*session)->_largest_pkt_size = 0;
(*session)->_tx_cnt = 0;
if(new_param)
{
if(new_param->block_size != (*session)->_max_block_size)
{
NetworkCodingPktBuffer* new_buffer = nullptr;
unsigned char* new_rand_coef = nullptr;
try{
new_buffer = new NetworkCodingPktBuffer [new_param->block_size];
}
catch(std::exception ex)
{
// Packet is successfully delivered regardless of the result of updating nc parameters
return pkt_size;
}
try{
new_rand_coef = new unsigned char [new_param->block_size];
}
catch(std::exception ex)
{
delete [] new_buffer;
// Packet is successfully delivered regardless of the result of updating nc parameters
return pkt_size;
}
delete [] ((*session)->_buffer);
delete [] ((*session)->_rand_coef);
(*session)->_buffer = new_buffer;
(*session)->_rand_coef = new_rand_coef;
}
if(new_param->retransmission_interval != (*session)->_retransmission_interval)
{
(*session)->_retransmission_interval = new_param->retransmission_interval;
}
if(new_param->redundancy != (*session)->_redundancy)
{
(*session)->_redundancy = new_param->redundancy;
}
// Parameter is changed.
}
}
else
{
(*session)->_tx_cnt++;
}
(*session)->_lock.unlock();
return pkt_size;
}
void nctx::_rx_handler(unsigned char* buffer, unsigned int size, sockaddr_in* sender_addr, unsigned int sender_addr_len)
{
if(buffer[0] == NC_PKT_TYPE::ACK_TYPE)
{
Ack* const ack = (Ack*)buffer;
if(size != sizeof(Ack))
{
return;
}
const ip_port_key key = {sender_addr->sin_addr.s_addr, sender_addr->sin_port};
tx_session_info** const session = _tx_session_info.find(key);
if(session != nullptr)
{
if(ack->blk_seq == (*session)->_blk_seq)
{
(*session)->_retransmission_in_progress = false;
(*session)->_loss_rate = ((*session)->_loss_rate + ack->losses)/2;
}
else
{
// Packets of new block sequence may be lost. We can ignore this packet.
}
}
else
{
// To Do: Deal with zombie client
}
}
else if(buffer[0] == NC_PKT_TYPE::REP_CONNECT_TYPE)
{
const ip_port_key key = {sender_addr->sin_addr.s_addr, sender_addr->sin_port};
tx_session_info** const session = _tx_session_info.find(key);
if(session)
{
(*session)->_is_connected = true;
}
}
}