forked from harite/im_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
564 lines (468 loc) · 10.4 KB
/
util.cpp
File metadata and controls
564 lines (468 loc) · 10.4 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include "util.h"
CThread::CThread()
{
m_thread_id = 0;
}
CThread::~CThread()
{
}
#ifdef _WIN32
DWORD WINAPI CThread::StartRoutine(LPVOID arg)
#else
void* CThread::StartRoutine(void* arg)
#endif
{
CThread* pThread = (CThread*)arg;
pThread->OnThreadRun();
#ifdef _WIN32
return 0;
#else
return NULL;
#endif
}
void CThread::StartThread()
{
#ifdef _WIN32
(void)CreateThread(NULL, 0, StartRoutine, this, 0, &m_thread_id);
#else
(void)pthread_create(&m_thread_id, NULL, StartRoutine, this);
#endif
}
CEventThread::CEventThread()
{
m_bRunning = false;
}
CEventThread::~CEventThread()
{
StopThread();
}
void CEventThread::StartThread()
{
m_bRunning = true;
CThread::StartThread();
}
void CEventThread::StopThread()
{
m_bRunning = false;
}
void CEventThread::OnThreadRun()
{
while (m_bRunning)
{
OnThreadTick();
}
}
/////////// CThreadLock ///////////
CThreadLock::CThreadLock()
{
#ifdef _WIN32
InitializeCriticalSection(&m_critical_section);
#else
pthread_mutexattr_init(&m_mutexattr);
pthread_mutexattr_settype(&m_mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_mutex, &m_mutexattr);
#endif
}
CThreadLock::~CThreadLock()
{
#ifdef _WIN32
DeleteCriticalSection(&m_critical_section);
#else
pthread_mutexattr_destroy(&m_mutexattr);
pthread_mutex_destroy(&m_mutex);
#endif
}
void CThreadLock::Lock(void)
{
#ifdef _WIN32
EnterCriticalSection(&m_critical_section);
#else
pthread_mutex_lock(&m_mutex);
#endif
}
void CThreadLock::Unlock(void)
{
#ifdef _WIN32
LeaveCriticalSection(&m_critical_section);
#else
pthread_mutex_unlock(&m_mutex);
#endif
}
CRefObject::CRefObject()
{
m_lock = NULL;
m_refCount = 1;
}
CRefObject::~CRefObject()
{
}
void CRefObject::AddRef()
{
if (m_lock)
{
m_lock->Lock();
m_refCount++;
m_lock->Unlock();
}
else
{
m_refCount++;
}
}
void CRefObject::ReleaseRef()
{
if (m_lock)
{
m_lock->Lock();
m_refCount--;
if (m_refCount == 0)
{
delete this;
return;
}
m_lock->Unlock();
}
else
{
m_refCount--;
if (m_refCount == 0)
delete this;
}
}
///////////// CSimpleBuffer ////////////////
CSimpleBuffer::CSimpleBuffer()
{
m_buffer = NULL;
m_alloc_size = 0;
m_write_offset = 0;
}
CSimpleBuffer::~CSimpleBuffer()
{
m_alloc_size = 0;
m_write_offset = 0;
if (m_buffer)
{
free(m_buffer);
m_buffer = NULL;
}
}
void CSimpleBuffer::Extend(uint32_t len)
{
m_alloc_size = m_write_offset + len;
m_alloc_size += m_alloc_size >> 2; // increase by 1/4 allocate size
uchar_t* new_buf = (uchar_t*)realloc(m_buffer, m_alloc_size);
m_buffer = new_buf;
}
uint32_t CSimpleBuffer::Write(void* buf, uint32_t len)
{
if (m_write_offset + len > m_alloc_size)
{
Extend(len);
}
if (buf)
{
memcpy(m_buffer + m_write_offset, buf, len);
}
m_write_offset += len;
return len;
}
uint32_t CSimpleBuffer::Read(void* buf, uint32_t len)
{
if (len > m_write_offset)
len = m_write_offset;
if (buf)
memcpy(buf, m_buffer, len);
m_write_offset -= len;
memmove(m_buffer, m_buffer + len, m_write_offset);
return len;
}
////// CByteStream //////
CByteStream::CByteStream(uchar_t* buf, uint32_t len)
{
m_pBuf = buf;
m_len = len;
m_pSimpBuf = NULL;
m_pos = 0;
}
CByteStream::CByteStream(CSimpleBuffer* pSimpBuf, uint32_t pos)
{
m_pSimpBuf = pSimpBuf;
m_pos = pos;
m_pBuf = NULL;
m_len = 0;
}
int16_t CByteStream::ReadInt16(uchar_t *buf)
{
int16_t data = (buf[0] << 8) | buf[1];
return data;
}
uint16_t CByteStream::ReadUint16(uchar_t* buf)
{
uint16_t data = (buf[0] << 8) | buf[1];
return data;
}
int32_t CByteStream::ReadInt32(uchar_t *buf)
{
int32_t data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
return data;
}
uint32_t CByteStream::ReadUint32(uchar_t *buf)
{
uint32_t data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
return data;
}
void CByteStream::WriteInt16(uchar_t *buf, int16_t data)
{
buf[0] = static_cast<uchar_t>(data >> 8);
buf[1] = static_cast<uchar_t>(data & 0xFF);
}
void CByteStream::WriteUint16(uchar_t *buf, uint16_t data)
{
buf[0] = static_cast<uchar_t>(data >> 8);
buf[1] = static_cast<uchar_t>(data & 0xFF);
}
void CByteStream::WriteInt32(uchar_t *buf, int32_t data)
{
buf[0] = static_cast<uchar_t>(data >> 24);
buf[1] = static_cast<uchar_t>((data >> 16) & 0xFF);
buf[2] = static_cast<uchar_t>((data >> 8) & 0xFF);
buf[3] = static_cast<uchar_t>(data & 0xFF);
}
void CByteStream::WriteUint32(uchar_t *buf, uint32_t data)
{
buf[0] = static_cast<uchar_t>(data >> 24);
buf[1] = static_cast<uchar_t>((data >> 16) & 0xFF);
buf[2] = static_cast<uchar_t>((data >> 8) & 0xFF);
buf[3] = static_cast<uchar_t>(data & 0xFF);
}
void CByteStream::operator << (int8_t data)
{
_WriteByte(&data, 1);
}
void CByteStream::operator << (uint8_t data)
{
_WriteByte(&data, 1);
}
void CByteStream::operator << (int16_t data)
{
unsigned char buf[2];
buf[0] = static_cast<uchar_t>(data >> 8);
buf[1] = static_cast<uchar_t>(data & 0xFF);
_WriteByte(buf, 2);
}
void CByteStream::operator << (uint16_t data)
{
unsigned char buf[2];
buf[0] = static_cast<uchar_t>(data >> 8);
buf[1] = static_cast<uchar_t>(data & 0xFF);
_WriteByte(buf, 2);
}
void CByteStream::operator << (int32_t data)
{
unsigned char buf[4];
buf[0] = static_cast<uchar_t>(data >> 24);
buf[1] = static_cast<uchar_t>((data >> 16) & 0xFF);
buf[2] = static_cast<uchar_t>((data >> 8) & 0xFF);
buf[3] = static_cast<uchar_t>(data & 0xFF);
_WriteByte(buf, 4);
}
void CByteStream::operator << (uint32_t data)
{
unsigned char buf[4];
buf[0] = static_cast<uchar_t>(data >> 24);
buf[1] = static_cast<uchar_t>((data >> 16) & 0xFF);
buf[2] = static_cast<uchar_t>((data >> 8) & 0xFF);
buf[3] = static_cast<uchar_t>(data & 0xFF);
_WriteByte(buf, 4);
}
void CByteStream::operator >> (int8_t& data)
{
_ReadByte(&data, 1);
}
void CByteStream::operator >> (uint8_t& data)
{
_ReadByte(&data, 1);
}
void CByteStream::operator >> (int16_t& data)
{
unsigned char buf[2];
_ReadByte(buf, 2);
data = (buf[0] << 8) | buf[1];
}
void CByteStream::operator >> (uint16_t& data)
{
unsigned char buf[2];
_ReadByte(buf, 2);
data = (buf[0] << 8) | buf[1];
}
void CByteStream::operator >> (int32_t& data)
{
unsigned char buf[4];
_ReadByte(buf, 4);
data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}
void CByteStream::operator >> (uint32_t& data)
{
unsigned char buf[4];
_ReadByte(buf, 4);
data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}
void CByteStream::WriteString(const char *str)
{
uint8_t size = str ? (uint8_t)strlen(str) + 1 : 0;
*this << size;
_WriteByte((void*)str, size);
}
char* CByteStream::ReadString()
{
uint8_t size = 0;
*this >> size;
char* pStr = (char*)malloc(size);
_ReadByte(pStr, size);
return pStr;
}
void CByteStream::WriteStdString(string& s)
{
WriteString(s.c_str());
}
char* CByteStream::ReadStdString()
{
uint8_t size = 0;
*this >> size; // WriteStdString() will always have at least 1 byte
char* ptr = (char*)(m_pBuf + m_pos);
m_pos += size;
return ptr;
}
void CByteStream::WriteData(uchar_t *data, uint16_t len)
{
*this << len;
_WriteByte(data, len);
}
void CByteStream::WriteData(uchar_t *data, uint32_t len)
{
*this << len;
_WriteByte(data, len);
}
uchar_t* CByteStream::ReadData(uint16_t &len)
{
*this >> len;
uchar_t* pData = (uchar_t*)malloc(len);
_ReadByte(pData, len);
return pData;
}
uchar_t* CByteStream::ReadData(uint32_t &len)
{
*this >> len;
uchar_t* pData = (uchar_t*)malloc(len);
_ReadByte(pData, len);
return pData;
}
void CByteStream::_ReadByte(void* buf, uint32_t len)
{
if (m_pos + len > m_len)
return;
if (m_pSimpBuf)
m_pSimpBuf->Read((char*)buf, len);
else
memcpy(buf, m_pBuf + m_pos, len);
m_pos += len;
}
void CByteStream::_WriteByte(void* buf, uint32_t len)
{
if (m_pBuf && (m_pos + len > m_len))
return;
if (m_pSimpBuf)
m_pSimpBuf->Write((char*)buf, len);
else
memcpy(m_pBuf + m_pos, buf, len);
m_pos += len;
}
static void print_tread_id(FILE* fp)
{
uint16_t thread_id = 0;
#ifdef _WIN32
thread_id = (uint16_t)GetCurrentThreadId();
#elif __APPLE__
thread_id = syscall(SYS_gettid);
#else
thread_id = (uint16_t)pthread_self();
#endif
fprintf(fp, "(tid=%d)", thread_id);
}
static void print_format_time(FILE* fp)
{
#ifdef _WIN32
SYSTEMTIME systemTime;
GetLocalTime(&systemTime);
fprintf(fp, "%04d-%02d-%02d, %02d:%02d:%02d.%03d, ", systemTime.wYear, systemTime.wMonth, systemTime.wDay,
systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
#else
struct timeval tval;
struct tm* tm;
time_t currTime;
time(&currTime);
tm = localtime(&currTime);
gettimeofday(&tval, NULL);
fprintf(fp, "%04d-%02d-%02d, %02d:%02d:%02d.%03d, ", 1900 + tm->tm_year, 1 + tm->tm_mon,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tval.tv_usec / 1000);
#endif
}
static CThreadLock s_log_lock;
void log(const char* fmt, ...)
{
CFuncLock func_lock(&s_log_lock);
static int file_no = 1;
static FILE* log_fp = NULL;
if (log_fp == NULL)
{
char log_name[64];
uint16_t pid = 0;
#ifdef _WIN32
pid = (uint16_t)GetCurrentProcessId();
#else
pid = (uint16_t)getpid();
#endif
snprintf(log_name, 64, "log_%d_%d.txt", pid, file_no);
log_fp = fopen(log_name, "w");
if (log_fp == NULL)
return;
}
print_tread_id(log_fp);
print_format_time(log_fp);
va_list ap;
va_start(ap, fmt);
vfprintf(log_fp, fmt, ap);
va_end(ap);
fflush(log_fp);
if (ftell(log_fp) > MAX_LOG_FILE_SIZE)
{
fclose(log_fp);
log_fp = NULL;
file_no++;
}
}
uint32_t get_tick_count()
{
#ifdef _WIN32
LARGE_INTEGER liCounter;
LARGE_INTEGER liCurrent;
if (!QueryPerformanceFrequency(&liCounter))
return GetTickCount();
QueryPerformanceCounter(&liCurrent);
return (uint32_t)(liCurrent.QuadPart * 1000 / liCounter.QuadPart);
#else
struct timeval tval;
uint32_t ret_tick;
gettimeofday(&tval, NULL);
ret_tick = tval.tv_sec * 1000 + tval.tv_usec / 1000;
return ret_tick;
#endif
}
void util_sleep(uint32_t millisecond)
{
#ifdef _WIN32
Sleep(millisecond);
#else
usleep(millisecond * 1000);
#endif
}