-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
59 lines (54 loc) · 1.33 KB
/
utils.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
#include"utils.h"
//pthread functions
#ifdef PTHREAD
bool StartThread(pthread_t* thread_id, void* thread_func(void*), void* param){
int ret = pthread_create(thread_id, NULL, thread_func, param);
if (ret != 0){
perror("create thread!\n");
return false;
}
return true;
}
void StopThread(pthread_t thread_id){
pthread_cancel(thread_id);
}
void JoinThread(pthread_t& thread_id){
pthread_join(thread_id, NULL);
}
#else
bool StartThread(std::thread* thread, void* thread_func(void*), void* param){
*thread = std::thread(thread_func, param);
if (!thread){
perror("create thread!\n");
return false;
}
return true;
}
void JoinThread(std::thread& t){
t.join();
}
#endif
//time function
void GetTime(char* buffer){
time_t now;
struct tm* timenow;
time(&now);
timenow = localtime(&now);
strcpy(buffer, asctime(timenow));
}
void BuildSockAddr(const char* ip, const uint16_t port, struct sockaddr_in* sock_addr){
memset((char*)sock_addr, 0, sizeof(struct sockaddr_in));
sock_addr->sin_family = AF_INET;
sock_addr->sin_port = htons(port);
sock_addr->sin_addr.s_addr = inet_addr(ip);
}
uint32_t Hex2Dec(char c){
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'a' && c <= 'f')
return c - 'a' + 10;
else if(c >= 'A' && c <= 'F')
return c - 'A' + 10;
printf("invalid hex char %c !\n", c);
return 0;
}