[go: up one dir, main page]

Menu

[r76]: / udt / queue.h  Maximize  Restore  History

Download this file

505 lines (385 with data), 12.8 kB

  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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*****************************************************************************
Copyright © 2001 - 2007, The Board of Trustees of the University of Illinois.
All Rights Reserved.
UDP-based Data Transfer Library (UDT) version 4
National Center for Data Mining (NCDM)
University of Illinois at Chicago
http://www.ncdm.uic.edu/
UDT is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
UDT is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
/*****************************************************************************
This header file contains the definition of UDT multiplexer.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu [gu@lac.uic.edu], last updated 07/16/2007
*****************************************************************************/
#ifndef __UDT_QUEUE_H__
#define __UDT_QUEUE_H__
#include "common.h"
#include "packet.h"
#include "channel.h"
#include <vector>
class CUDT;
struct CUnit
{
CPacket m_Packet; // packet
int m_iFlag; // 0: free, 1: occupied, 2: msg read but not freed (out-of-order), 3: msg dropped
};
class CUnitQueue
{
friend class CRcvQueue;
friend class CRcvBuffer;
public:
CUnitQueue();
~CUnitQueue();
public:
// Functionality:
// Initialize the unit queue.
// Parameters:
// 1) [in] size: queue size
// 2) [in] mss: maximum segament size
// 3) [in] version: IP version
// Returned value:
// 0: success, -1: failure.
int init(const int& size, const int& mss, const int& version);
// Functionality:
// Increase (double) the unit queue size.
// Parameters:
// None.
// Returned value:
// 0: success, -1: failure.
int increase();
// Functionality:
// Decrease (halve) the unit queue size.
// Parameters:
// None.
// Returned value:
// 0: success, -1: failure.
int shrink();
// Functionality:
// find an available unit for incoming packet.
// Parameters:
// None.
// Returned value:
// Pointer to the available unit, NULL if not found.
CUnit* getNextAvailUnit();
private:
struct CQEntry
{
CUnit* m_pUnit; // unit queue
char* m_pBuffer; // data buffer
int m_iSize; // size of each queue
CQEntry* m_pNext;
}
*m_pQEntry, // pointer to the first unit queue
*m_pCurrQueue, // pointer to the current available queue
*m_pLastQueue; // pointer to the last unit queue
CUnit* m_pAvailUnit; // recent available unit
int m_iSize; // total size of the unit queue, in number of packets
int m_iCount; // total number of valid packets in the queue
int m_iMSS; // unit buffer size
int m_iIPversion; // IP version
};
struct CUDTList
{
uint64_t m_llTimeStamp; // Time Stamp
int32_t m_iID; // UDT Socket ID
CUDT* m_pUDT; // Pointer to the instance of CUDT socket
CUDTList* m_pPrev; // previous link
CUDTList* m_pNext; // next link
};
class CSndUList
{
friend class CSndQueue;
public:
CSndUList();
~CSndUList();
public:
// Functionality:
// Insert a new UDT instance into the list.
// Parameters:
// 1) [in] ts: time stamp: next processing time
// 2) [in] id: socket ID
// 3) [in] u: pointer to the UDT instance
// Returned value:
// None.
void insert(const int64_t& ts, const int32_t& id, const CUDT* u);
// Functionality:
// Remove UDT instance from the list.
// Parameters:
// 1) [in] id: Socket ID
// Returned value:
// None.
void remove(const int32_t& id);
// Functionality:
// Update the timestamp of the UDT instance on the list.
// Parameters:
// 1) [in] id: socket ID
// 2) [in] u: pointer to the UDT instance
// 3) [in] resechedule: if the timestampe shoudl be rescheduled
// Returned value:
// None.
void update(const int32_t& id, const CUDT* u, const bool& reschedule = true);
// Functionality:
// Get and remove the first UDT instance on the list.
// Parameters:
// 1) [out] id: socket ID
// 2) [out] u: pointer to the UDT instance
// Returned value:
// UDT Socket ID if found one, otherwise -1.
int pop(int32_t& id, CUDT*& u);
public:
CUDTList* m_pUList; // The head node
private:
CUDTList* m_pLast; // The last node
pthread_mutex_t m_ListLock;
pthread_mutex_t* m_pWindowLock;
pthread_cond_t* m_pWindowCond;
};
class CRcvUList
{
public:
CRcvUList();
~CRcvUList();
public:
// Functionality:
// Insert a new UDT instance to the list.
// Parameters:
// 1) [in] u: pointer to the UDT instance
// Returned value:
// None.
void insert(const CUDT* u);
// Functionality:
// Remove the UDT instance from the list.
// Parameters:
// 1) [in] id: socket ID
// Returned value:
// None.
void remove(const int32_t& id);
// Functionality:
// Move the UDT instance to the end of the list, if it already exists; otherwise, do nothing.
// Parameters:
// 1) [in] id: socket ID
// Returned value:
// None.
void update(const int32_t& id);
// Functionality:
// Insert a new UDT instance to the new entry list.
// Parameters:
// 1) [in] u: pointer to the UDT instance
// Returned value:
// None.
void newEntry(CUDT* u);
// Functionality:
// Check if there is a new entry to be inserted to the rcv u list
// Parameters:
// None.
// Returned value:
// True if yes, otherwise false.
bool ifNewEntry();
// Functionality:
// Pick the first new entry on the waiting list.
// Parameters:
// None.
// Returned value:
// Pointer to a UDT instance.
CUDT* newEntry();
public:
CUDTList* m_pUList; // the head node
private:
CUDTList* m_pLast; // the last node
std::vector<CUDT*> m_vNewEntry; // newly added entries, to be inserted
pthread_mutex_t m_ListLock;
};
class CHash
{
public:
CHash();
~CHash();
public:
// Functionality:
// Initialize the hash table.
// Parameters:
// 1) [in] size: hash table size
// Returned value:
// None.
void init(const int& size);
// Functionality:
// Look for a UDT instance from the hash table.
// Parameters:
// 1) [in] id: socket ID
// Returned value:
// Pointer to a UDT instance, or NULL if not found.
CUDT* lookup(const int32_t& id);
// Functionality:
// Retrive a received packet that is temporally stored in the hash table.
// Parameters:
// 1) [in] id: socket ID
// 2) [out] packet: the returned packet
// Returned value:
// Data length of the packet, or -1.
int retrieve(const int32_t& id, CPacket& packet);
// Functionality:
// Store a packet in the hash table.
// Parameters:
// 1) [in] id: socket ID
// 2) [in] unit: information for the packet
// Returned value:
// None.
void setUnit(const int32_t& id, CUnit* unit);
// Functionality:
// Insert an entry to the hash table.
// Parameters:
// 1) [in] id: socket ID
// 2) [in] u: pointer to the UDT instance
// Returned value:
// None.
void insert(const int32_t& id, const CUDT* u);
// Functionality:
// Remove an entry from the hash table.
// Parameters:
// 1) [in] id: socket ID
// Returned value:
// None.
void remove(const int32_t& id);
private:
struct CBucket
{
int32_t m_iID; // Socket ID
CUDT* m_pUDT; // Socket instance
CBucket* m_pNext; // next bucket
CUnit* m_pUnit; // tempory buffer for a received packet
} **m_pBucket; // list of buckets (the hash table)
int m_iHashSize; // size of hash table
};
class CRendezvousQueue
{
public:
CRendezvousQueue();
~CRendezvousQueue();
public:
void insert(const UDTSOCKET& id, const int& ipv, const sockaddr* addr);
void remove(const UDTSOCKET& id);
bool retrieve(const sockaddr* addr, UDTSOCKET& id, const UDTSOCKET& peerid);
private:
struct CRL
{
UDTSOCKET m_iID;
UDTSOCKET m_iPeerID;
int m_iIPversion;
sockaddr* m_pPeerAddr;
};
std::vector<CRL> m_vRendezvousID; // The sockets currently in rendezvous mode
pthread_mutex_t m_RIDVectorLock;
};
class CSndQueue
{
friend class CUDT;
friend class CUDTUnited;
public:
CSndQueue();
~CSndQueue();
public:
// Functionality:
// Initialize the sending queue.
// Parameters:
// 1) [in] c: UDP channel to be associated to the queue
// 2) [in] t: Timer
// Returned value:
// None.
void init(const CChannel* c, const CTimer* t);
// Functionality:
// Send out a packet to a given address.
// Parameters:
// 1) [in] addr: destination address
// 2) [in] packet: packet to be sent out
// Returned value:
// Size of data sent out.
int sendto(const sockaddr* addr, CPacket& packet);
private:
#ifndef WIN32
static void* worker(void* param);
#else
static DWORD WINAPI worker(LPVOID param);
#endif
pthread_t m_WorkerThread;
private:
CSndUList* m_pSndUList; // List of UDT instances for data sending
CChannel* m_pChannel; // The UDP channel for data sending
CTimer* m_pTimer; // Timing facility
pthread_mutex_t m_WindowLock;
pthread_cond_t m_WindowCond;
volatile bool m_bClosing; // closing the worker
};
class CRcvQueue
{
friend class CUDT;
friend class CUDTUnited;
public:
CRcvQueue();
~CRcvQueue();
public:
// Functionality:
// Initialize the receiving queue.
// Parameters:
// 1) [in] size: queue size
// 2) [in] mss: maximum packet size
// 3) [in] version: IP version
// 4) [in] hsize: hash table size
// 5) [in] c: UDP channel to be associated to the queue
// 6) [in] t: timer
// Returned value:
// None.
void init(const int& size, const int& payload, const int& version, const int& hsize, const CChannel* c, const CTimer* t);
// Functionality:
// Read a packet for a specific UDT socket id.
// Parameters:
// 1) [in] id: Socket ID
// 2) [out] packet: received packet
// Returned value:
// Data size of the packet
int recvfrom(const int32_t& id, CPacket& packet);
private:
#ifndef WIN32
static void* worker(void* param);
#else
static DWORD WINAPI worker(LPVOID param);
#endif
pthread_t m_WorkerThread;
private:
CUnitQueue m_UnitQueue; // The received packet queue
CRcvUList* m_pRcvUList; // List of UDT instances that will read packets from the queue
CHash* m_pHash; // Hash table for UDT socket looking up
CChannel* m_pChannel; // UDP channel for receving packets
CTimer* m_pTimer; // shared timer with the snd queue
pthread_mutex_t m_PassLock;
pthread_cond_t m_PassCond;
volatile UDTSOCKET m_ListenerID; // The only listening socket that is associated to the queue, if there is one
CRendezvousQueue* m_pRendezvousQueue; // The list of sockets in rendezvous mode
int m_iPayloadSize; // packet payload size
volatile bool m_bClosing; // closing the workder
};
class CMultiplexer
{
public:
CSndQueue* m_pSndQueue; // The sending queue
CRcvQueue* m_pRcvQueue; // The receiving queue
CChannel* m_pChannel; // The UDP channel for sending and receiving
CTimer* m_pTimer; // The timer
int m_iPort; // The UDP port number of this multiplexer
int m_iIPversion; // IP version
int m_iMTU; // MTU
int m_iRefCount; // number of UDT instances that are associated with this multiplexer
bool m_bReusable; // if this one can be shared with others
};
#endif