-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinterface_c.cpp
370 lines (289 loc) · 6.44 KB
/
interface_c.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
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
#include "interface_c.h"
#include "PacketBase.h"
#include "socket.h"
#include "log.h"
#include "timer_event.h"
#include "net.h"
#include "mysql_part.h"
#include "redis.h"
#include "timewheel.h"
#include <string.h>
#include <uuid/uuid.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
extern lua_State* L;
static NETOutputPacket out_package;
extern timer_list_t timer_list;
extern CMysql mysql_handle;
extern CRedis redis_handle;
extern NETInputPacket in_package;
extern Net net;
int
send_buffer(int fd, char* buffer, int len)
{
return send(fd, buffer, len, 0);
}
static int
send_packet(int fd, bool encrypt)
{
if (encrypt) SocketHandler::EncryptBuffer(&out_package);
char* buffer = out_package.packet_buf();
int length = out_package.packet_size();
int nsend = 0;
while (nsend < length) {
int size = send(fd, buffer + nsend, length-nsend, 0);
printf("size = %d\n", size);
if (size < 0) {
log_debug("FD: %d\n", fd);
log_debug("Send ErrNo: %d\n", errno);
/* 判断非阻塞返回errno的处理 */
if (errno == EINTR) { /* EINTR 中断 信号 */
usleep(1);
continue;
}
if (errno == EAGAIN) { //如果缓冲区满了, 就继续
usleep(1); /* sleep 1us */
continue;
}
/* export lua */
lua_pushnumber(L, errno);
lua_setglobal(L, ERRNO);
return -1;
}
nsend += size;
}
return nsend; // modify by austin 2012-06-18
}
int send_raw_packet(int fd)
{
return send_packet(fd, false);
}
int send_packet(int fd)
{
return send_packet(fd, true);
}
int
transfer(int fd)
{
out_package.Copy(in_package.packet_buf(), in_package.packet_size());
return send_packet(fd,true);
}
int
transfer_raw(int fd)
{
out_package.Copy(in_package.packet_buf(), in_package.packet_size());
return send_packet(fd,false);
}
void
write_begin(short cmd)
{
out_package.Begin(cmd);
}
void
write_end()
{
out_package.End();
}
void write_int(int value)
{
out_package.WriteInt(value);
}
void write_uint(unsigned long value)
{
out_package.WriteULong(value);
}
void write_byte(unsigned char value)
{
out_package.WriteByte(value);
}
void write_short(short value)
{
out_package.WriteShort(value);
}
void write_string(char* value)
{
out_package.WriteString(value);
}
void write_binary(char* buf, int len)
{
out_package.WriteBinary(buf, len);
}
void error(const char* msg)
{
log_error(msg);
}
void info(const char* msg)
{
log_info(msg);
}
void debug(const char* msg)
{
log_debug(msg);
}
int create_timer()
{
TimerEvent *timer = new TimerEvent();
timer_list[timer->m_guid] = timer;
return timer->m_guid;
}
int start_timer(unsigned long timer_id, int time)
{
timer_list_itr_t iter = timer_list.find(timer_id);
if(iter == timer_list.end()) return -1;
TimerEvent* timer = iter->second;
timer_list[timer->m_guid] = timer;
timer->SetTimerId(timer->m_guid);
timer->StartTimer(time);
return (iter->second)->m_guid;
}
int stop_timer(unsigned long timer_id)
{
timer_list_itr_t iter = timer_list.find(timer_id);
if (iter == timer_list.end()) return -1;
TimerEvent* timer = iter->second;
timer->StopTimer();
return 0;
}
int reset_timer(unsigned long timer_id)
{
timer_list_itr_t iter = timer_list.find(timer_id);
if (iter == timer_list.end()) return -1;
TimerEvent* timer = iter->second;
timer->ResetTimer();
return 0;
}
int clear_timer(unsigned long timer_id)
{
timer_list_itr_t iter = timer_list.find(timer_id);
if(iter == timer_list.end()) {
return -1;
}
TimerEvent* event = iter->second;
timer_list.erase(iter);
delete(event);
return 0;
}
int remain_timer(unsigned long timer_id)
{
timer_list_itr_t iter = timer_list.find(timer_id);
if (iter == timer_list.end()) {
return -1;
}
TimerEvent* event = iter->second;
int remain = event->GetRemain();
return remain;
}
int create_listener(int port)
{
return net.create_listener(port);
}
int connect_server(char* ip, int port)
{
return connect_server(ip, port, true, 1);
}
int connect_server(char* ip, int port, bool is_encrypt, int conn_flag)
{
return net.connect_server(ip, port, is_encrypt, conn_flag);
}
int close_socket(int fd)
{
log_debug("FD: %d\n", fd);
::close(fd);
net.CloseHandler(fd);
return 0;
}
int connect_mysql(const char* host, const char* user, const char* password, const char* dbname, unsigned int port)
{
return mysql_handle.connect_mysql(host, user, password, dbname, port);
}
int query(const char* mysql)
{
return mysql_handle.query(mysql);
}
int connect_redis(const char* host, unsigned int port, unsigned short second)
{
return redis_handle.connect_redis(host, port, second);
}
int get_redis_value(const char* key)
{
return redis_handle.get_value(key);
}
int set_redis_value(const char* key, const char* value)
{
return redis_handle.set_value(key, value);
}
int set_redis_expire(const char* key, int expire)
{
return redis_handle.set_expire(key, expire);
}
int Enqueue(const char* queue, const char* value)
{
return redis_handle.Enqueue(queue, value);
}
int Dequeue(const char* queue)
{
return redis_handle.Dequeue(queue);
}
bool IsActived()
{
return redis_handle.IsActived();
}
int S_IsMember(const char* key, const char* value)
{
return redis_handle.S_IsMember(key, value);
}
int S_IsMember(const char* key, const int value)
{
return redis_handle.S_IsMember(key, value);
}
int
HSet(const char* key, const int field,const char* value)
{
return redis_handle.HashSetRedisValue(key,field,value);
}
int
HGet(const char* key,const int field)
{
return redis_handle.HashGetRedisValue(key,field);
}
int
Del(const char* key)
{
return redis_handle.DelRedisValue(key);
}
int
HDel(const char* key,const int field)
{
return redis_handle.DelRedisHashValue(key,field);
}
typedef struct {
unsigned int Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID, UUID;
void NewUUID()
{
UUID uuid;
//uuid_generate(reinterpret_cast<unsigned char *>(&uuid)); /* 产生的UUID有可能重复,/dev/urandom */
uuid_generate_random((unsigned char*)(&uuid));
char buf[64] = {0};
snprintf(buf,
sizeof(buf),
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
uuid.Data1, uuid.Data2, uuid.Data3,
uuid.Data4[0], uuid.Data4[1],
uuid.Data4[2], uuid.Data4[3],
uuid.Data4[4], uuid.Data4[5],
uuid.Data4[6], uuid.Data4[7]);
lua_pushstring(L, buf);
lua_setglobal(L, UUID_RESULT);
}