-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathim_server.cpp
More file actions
70 lines (55 loc) · 1.61 KB
/
im_server.cpp
File metadata and controls
70 lines (55 loc) · 1.61 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
#include "netlib.h"
#include "util.h"
#include "imconn.h"
#define IM_SERVER_PORT 8008
#define ROUTE_SERVER_PORT 8000
// this callback will be replaced by imconn_callback() in OnConnect()
void serv_callback(void* callback_data, uint8_t msg, uint32_t handle, uint32_t uParam, void* pParam)
{
NOTUSED_ARG(callback_data);
NOTUSED_ARG(uParam);
NOTUSED_ARG(pParam);
if (msg == NETLIB_MSG_CONNECT)
{
CImConn* pConn = new CImConn();
pConn->OnConnect(handle);
}
else
{
log("!!!error msg: %s\n", msg);
}
}
int main(int argc, char* argv[])
{
if (argc != 3) {
printf("usage: im_server listen_ip route_server_ip\n");
return -1;
}
char* im_server_ip = argv[1];
char* route_server_ip = argv[2];
/*
* Write to a socket that have received RST signal will cause SIGPIPE signal,
* the default action of this signal is exit process, so the process is just disappear
* without any trace(actually echo $? will print 141(128+13)).
* So we change the signal action to ignore the SIGPIPE.
*/
signal(SIGPIPE, SIG_IGN);
//InitImConn();
int ret = netlib_init();
if (ret == NETLIB_ERROR)
return ret;
ret = netlib_listen(im_server_ip, IM_SERVER_PORT, serv_callback, NULL);
if (ret == NETLIB_ERROR)
return ret;
printf("server start listen on: %s:%d\n", im_server_ip, IM_SERVER_PORT);
CImConn* pConn = new CImConn();
conn_handle_t handle = pConn->Connect(route_server_ip, ROUTE_SERVER_PORT, 0, NULL, NULL);
if (handle == NETLIB_INVALID_HANDLE) {
printf("wrong route server ip\n");
} else {
printf("connecting to route_server...\n");
}
printf("now enter the event loop...\n");
netlib_eventloop();
return 0;
}