-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_utils.c
188 lines (158 loc) · 4.02 KB
/
module_utils.c
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
/**
* @brief some utility functions
* @author Ye Shengnan
* @date 2015-08-17 created
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include "module_utils.h"
int set_module_data(modules_data_t *d, int module_id, void *data)
{
if (module_id < 0 || module_id >= MODULES_MAX)
return -1;
d->modules[module_id] = data;
return 0;
}
void* get_module_data(modules_data_t *d, int module_id)
{
if (module_id < 0 || module_id >= MODULES_MAX)
return NULL;
return d->modules[module_id];
}
void* queue_get_block(queue_t *queue, int timeout)
{
uint8_t *queue_buf;
int queue_buf_size;
void *buf;
for (; ;)
{
if (queue_get_readbuf(queue, &queue_buf, &queue_buf_size) != 0)
{
//printf("read block to from queue FAILED!\n");
if (timeout == -1 || timeout >= 20)
usleep(20000);
else
return NULL;
if (timeout >= 20)
timeout -= 20;
continue;
}
memcpy(&buf, queue_buf, sizeof(void *));
queue_read_complete(queue);
break;
}
return buf;
}
int queue_put_block(queue_t *queue, void *block, int timeout)
{
uint8_t *queue_buf;
int queue_buf_size = sizeof(void *);
for (; ;)
{
if (queue_get_writebuf(queue, &queue_buf, &queue_buf_size) != 0)
{
//printf("write block to queue FAILED!\n");
if (timeout == -1 || timeout >= 20)
usleep(20000);
else
return -1;
if (timeout >= 20)
timeout -= 20;
continue;
}
memcpy(queue_buf, &block, sizeof(void *));
queue_write_complete(queue, queue_buf, sizeof(void *), 0);
return 0;
}
}
void* memory_pool_get_block(memory_pool_t *memory_pool, int timeout)
{
void *block;
for (; ;)
{
block = memory_pool_alloc(memory_pool);
if (block == NULL)
{
//printf("allocate from memory pool FAILED!\n");
if (timeout == -1 || timeout >= 20)
usleep(20000);
else
return NULL;
if (timeout >= 20)
timeout -= 20;
continue;
}
return block;
}
}
int memory_pool_put_block(memory_pool_t *memory_pool, void *block, int timeout)
{
memory_pool_free(memory_pool, block);
return 0;
}
int serve_socket(int port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
char port_str[16];
int reuse = 1;
int fd;
int ret;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; //return IPv4 and IPv6 choices
hints.ai_socktype = SOCK_STREAM; //we want a TCP socket
hints.ai_flags = AI_PASSIVE; //all interfaces
snprintf(port_str, 16, "%d", port);
ret = getaddrinfo(NULL, port_str, &hints, &result);
if (ret != 0)
{
printf("getaddrinfo: %s\n", gai_strerror(ret));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd == -1)
continue;
ret = bind(fd, rp->ai_addr, rp->ai_addrlen);
if (ret == 0)
{
//we managed to bind successfully!
break;
}
close(fd);
}
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (rp == NULL)
{
printf("Could not bind\n");
return -1;
}
freeaddrinfo(result);
return fd;
}
int set_socket_nonblocking(int fd)
{
int flags;
int ret;
flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
{
printf("fnctl F_GETFL got error: %s", strerror(errno));
return -1;
}
flags |= O_NONBLOCK;
ret = fcntl(fd, F_SETFL, flags);
if (ret == -1)
{
printf("fnctl F_SETFL got error: %s", strerror(errno));
return -1;
}
return 0;
}