[go: up one dir, main page]

Menu

[r19]: / udt / common.cpp  Maximize  Restore  History

Download this file

512 lines (395 with data), 12.3 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
505
506
507
508
509
510
511
/*****************************************************************************
Copyright © 2001 - 2006, The Board of Trustees of the University of Illinois.
All Rights Reserved.
UDP-based Data Transfer Library (UDT) version 3
Laboratory for Advanced Computing (LAC)
National Center for Data Mining (NCDM)
University of Illinois at Chicago
http://www.lac.uic.edu/
This library 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 2.1 of the License, or (at
your option) any later version.
This library 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 library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*****************************************************************************/
/*****************************************************************************
This file contains implementation of UDT common routines of timer,
mutex facility, and exception processing.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu [ygu@cs.uic.edu], last updated 03/23/2006
*****************************************************************************/
#ifndef WIN32
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <cmath>
#include "common.h"
#ifdef WIN32
int gettimeofday(timeval *tv, void*)
{
LARGE_INTEGER ccf;
if (QueryPerformanceFrequency(&ccf))
{
LARGE_INTEGER cc;
QueryPerformanceCounter(&cc);
tv->tv_sec = (long)(cc.QuadPart / ccf.QuadPart);
tv->tv_usec = (long)((cc.QuadPart % ccf.QuadPart) / (ccf.QuadPart / 1000000));
}
else
{
uint64_t ft;
GetSystemTimeAsFileTime((FILETIME *)&ft);
tv->tv_sec = (long)(ft / 10000000);
tv->tv_usec = (long)((ft % 10000000) / 10);
}
return 0;
}
int readv(SOCKET s, const iovec* vector, int count)
{
DWORD rsize = 0;
DWORD flag = 0;
WSARecv(s, (LPWSABUF)vector, count, &rsize, &flag, NULL, NULL);
return rsize;
}
int writev(SOCKET s, const iovec* vector, int count)
{
DWORD ssize = 0;
WSASend(s, (LPWSABUF)vector, count, &ssize, 0, NULL, NULL);
return ssize;
}
#endif
uint64_t CTimer::s_ullCPUFrequency = CTimer::readCPUFrequency();
void CTimer::rdtsc(uint64_t &x)
{
#ifdef WIN32
if (!QueryPerformanceCounter((LARGE_INTEGER *)&x))
{
timeval t;
gettimeofday(&t, 0);
x = t.tv_sec * 1000000 + t.tv_usec;
}
#elif IA32
// read CPU clock with RDTSC instruction on IA32 acrh
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
// on Windows
/*
unsigned int a, b;
__asm
{
__emit 0x0f
__emit 0x31
mov a, eax
mov b, ebx
}
x = b;
x = (x << 32) + a;
*/
#elif IA64
__asm__ volatile ("mov %0=ar.itc" : "=r"(x) :: "memory");
#elif AMD64
unsigned int lval, hval;
__asm__ volatile ("rdtsc" : "=a" (lval), "=d" (hval));
x = hval;
x = (x << 32) | lval;
#else
// use system call to read time clock for other archs
timeval t;
gettimeofday(&t, 0);
x = t.tv_sec * 1000000 + t.tv_usec;
#endif
}
uint64_t CTimer::readCPUFrequency()
{
#ifdef WIN32
int64_t ccf;
if (QueryPerformanceFrequency((LARGE_INTEGER *)&ccf))
return ccf / 1000000;
else
return 1;
#elif IA32 || IA64 || AMD64
// alternative: read /proc/cpuinfo
uint64_t t1, t2;
rdtsc(t1);
usleep(100000);
rdtsc(t2);
// CPU clocks per microsecond
return (t2 - t1) / 100000;
#else
return 1;
#endif
}
uint64_t CTimer::getCPUFrequency()
{
return s_ullCPUFrequency;
}
void CTimer::sleep(const uint64_t& interval)
{
uint64_t t;
rdtsc(t);
// sleep next "interval" time
sleepto(t + interval);
}
void CTimer::sleepto(const uint64_t& nexttime)
{
// Use class member such that the method can be interrupted by others
m_ullSchedTime = nexttime;
uint64_t t;
rdtsc(t);
while (t < m_ullSchedTime)
{
#ifdef IA32
//__asm__ volatile ("nop; nop; nop; nop; nop;");
__asm__ volatile ("pause; rep; nop; nop; nop; nop; nop;");
#elif IA64
__asm__ volatile ("nop 0; nop 0; nop 0; nop 0; nop 0;");
#elif AMD64
__asm__ volatile ("nop; nop; nop; nop; nop;");
#endif
// TODO: use high precision timer if it is available
rdtsc(t);
}
}
void CTimer::interrupt()
{
// schedule the sleepto time to the current CCs, so that it will stop
rdtsc(m_ullSchedTime);
}
//
// Automatically lock in constructor
CGuard::CGuard(pthread_mutex_t& lock):
m_Mutex(lock)
{
#ifndef WIN32
m_iLocked = pthread_mutex_lock(&m_Mutex);
#else
m_iLocked = WaitForSingleObject(m_Mutex, INFINITE);
#endif
}
// Automatically unlock in destructor
CGuard::~CGuard()
{
#ifndef WIN32
if (0 == m_iLocked)
pthread_mutex_unlock(&m_Mutex);
#else
if (WAIT_FAILED != m_iLocked)
ReleaseMutex(m_Mutex);
#endif
}
//
CUDTException::CUDTException(int major, int minor, int err):
m_iMajor(major),
m_iMinor(minor)
{
if (-1 == err)
#ifndef WIN32
m_iErrno = errno;
#else
m_iErrno = GetLastError();
#endif
else
m_iErrno = err;
}
CUDTException::CUDTException(const CUDTException& e):
m_iMajor(e.m_iMajor),
m_iMinor(e.m_iMinor),
m_iErrno(e.m_iErrno)
{
}
CUDTException::~CUDTException()
{
}
const char* CUDTException::getErrorMessage()
{
// translate "Major:Minor" code into text message.
switch (m_iMajor)
{
case 0:
strcpy(m_pcMsg, "Success");
break;
case 1:
strcpy(m_pcMsg, "Connection setup failure");
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "connection time out");
break;
case 2:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "connection rejected");
break;
case 3:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "unable to create/configure UDP socket");
break;
case 4:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "abort for security reasons");
break;
default:
break;
}
break;
case 2:
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg, "Connection was broken");
break;
case 2:
strcpy(m_pcMsg, "Connection does not exist");
break;
default:
break;
}
break;
case 3:
strcpy(m_pcMsg, "System resource failure");
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg, "unable to create new threads");
break;
case 2:
strcpy(m_pcMsg, "unable to allocate buffers");
break;
default:
break;
}
break;
case 4:
strcpy(m_pcMsg, "File system failure");
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "cannot seek read position");
break;
case 2:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "failure in read");
break;
case 3:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "cannot seek write position");
break;
case 4:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "failure in write");
break;
default:
break;
}
break;
case 5:
strcpy(m_pcMsg, "Operation not supported");
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Cannot do this operation on a BOUND socket");
break;
case 2:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Cannot do this operation on a CONNECTED socket");
break;
case 3:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Bad parameters");
break;
case 4:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Invalid socket ID");
break;
case 5:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Cannot do this operation on an UNBOUND socket");
break;
case 6:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Socket is not in listening state");
break;
case 7:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Listen/accept is not supported in rendezous connection setup");
break;
case 8:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "Cannot call connect on UNBOUND socket in rendezvous connection setup");
break;
case 9:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "This operation is not supported in SOCK_STREAM mode");
break;
case 10:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "This operation is not supported in SOCK_DGRAM mode");
break;
default:
break;
}
break;
case 6:
strcpy(m_pcMsg, "Non-blocking call failure");
switch (m_iMinor)
{
case 1:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "no buffer available for sending");
break;
case 2:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "no data available for reading");
break;
case 3:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "no buffer available for overlapped reading");
break;
case 4:
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
strcpy(m_pcMsg + strlen(m_pcMsg), "non-blocking overlapped recv is on going");
break;
default:
break;
}
break;
default:
strcpy(m_pcMsg, "Unknown error");
}
// Adding "errno" information
if (0 < m_iErrno)
{
strcpy(m_pcMsg + strlen(m_pcMsg), ": ");
#ifndef WIN32
strncpy(m_pcMsg + strlen(m_pcMsg), strerror(m_iErrno), 1024 - strlen(m_pcMsg) - 2);
#else
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, m_iErrno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strncpy(m_pcMsg + strlen(m_pcMsg), (char*)lpMsgBuf, 1024 - strlen(m_pcMsg) - 2);
LocalFree(lpMsgBuf);
#endif
}
// period
#ifndef WIN32
strcpy(m_pcMsg + strlen(m_pcMsg), ".");
#endif
return m_pcMsg;
}
const int CUDTException::getErrorCode() const
{
return m_iMajor * 1000 + m_iMinor;
}