forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
152 lines (129 loc) · 3.38 KB
/
server.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
/**
* gcc -o server server.c -lswoole
*/
#include <swoole/Server.h>
#include <swoole/swoole.h>
#include <swoole/Client.h>
int my_onReceive(swFactory *factory, swEventData *req);
void my_onStart(swServer *serv);
void my_onShutdown(swServer *serv);
void my_onConnect(swServer *serv, int fd, int from_id);
void my_onClose(swServer *serv, int fd, int from_id);
void my_onTimer(swServer *serv, int interval);
void my_onWorkerStart(swServer *serv, int worker_id);
void my_onWorkerStop(swServer *serv, int worker_id);
char* php_rtrim(char *str, int len)
{
int i;
for (i = len; i > 0; i--)
{
switch(str[i])
{
case ' ':
case '\0':
case '\n':
case '\r':
case '\t':
case '\v':
str[i] = 0;
break;
default:
return str;
}
}
return str;
}
int main(int argc, char **argv)
{
int ret;
swServer serv;
swServer_init(&serv); //初始化
//config
serv.backlog = 128;
serv.reactor_num = 2; //reactor线程数量
serv.writer_num = 2; //writer线程数量
serv.worker_num = 4; //worker进程数量
serv.factory_mode = SW_MODE_PROCESS; //SW_MODE_PROCESS SW_MODE_THREAD SW_MODE_BASE
serv.max_conn = 100000;
//serv.open_cpu_affinity = 1;
//serv.open_tcp_nodelay = 1;
//serv.daemonize = 1;
//serv.open_eof_check = 1;
//create Server
ret = swServer_create(&serv);
if (ret < 0)
{
swTrace("create server fail[error=%d].\n", ret);
exit(0);
}
//swServer_addListen(&serv, SW_SOCK_UDP, "127.0.0.1", 9500);
swServer_addListen(&serv, SW_SOCK_TCP, "127.0.0.1", 9501);
//swServer_addListen(&serv, SW_SOCK_UDP, "127.0.0.1", 9502);
//swServer_addListen(&serv, SW_SOCK_UDP, "127.0.0.1", 8888);
//swServer_addTimer(&serv, 2);
//swServer_addTimer(&serv, 4);
serv.onStart = my_onStart;
serv.onShutdown = my_onShutdown;
serv.onConnect = my_onConnect;
serv.onReceive = my_onReceive;
serv.onClose = my_onClose;
serv.onTimer = my_onTimer;
serv.onWorkerStart = my_onWorkerStart;
serv.onWorkerStop = my_onWorkerStop;
ret = swServer_start(&serv);
if (ret < 0)
{
swTrace("start server fail[error=%d].\n", ret);
exit(0);
}
return 0;
}
void my_onWorkerStart(swServer *serv, int worker_id)
{
printf("Worker[%d]PID=%d start\n", worker_id, getpid());
}
void my_onWorkerStop(swServer *serv, int worker_id)
{
printf("Worker[%d]PID=%d stop\n", worker_id, getpid());
}
void my_onTimer(swServer *serv, int interval)
{
printf("Timer Interval=[%d]\n", interval);
}
static int receive_count = 0;
int my_onReceive(swFactory *factory, swEventData *req)
{
int ret;
char resp_data[SW_BUFFER_SIZE];
swSendData resp;
receive_count ++;
resp.info.fd = req->info.fd; //fd can be not source fd.
resp.info.len = req->info.len + 8;
resp.info.from_id = req->info.from_id;
req->data[req->info.len] = 0;
snprintf(resp_data, resp.info.len, "Server:%s", req->data);
resp.data = resp_data;
ret = factory->finish(factory, &resp);
if (ret < 0)
{
printf("send to client fail.errno=%d\n", errno);
}
printf("onReceive[%d]: Data=%s|Len=%d\n",receive_count, php_rtrim(req->data, req->info.len), req->info.len);
return SW_OK;
}
void my_onStart(swServer *serv)
{
printf("Server is running\n");
}
void my_onShutdown(swServer *serv)
{
printf("Server is shutdown\n");
}
void my_onConnect(swServer *serv, int fd, int from_id)
{
printf("PID=%d\tConnect fd=%d|from_id=%d\n", getpid(), fd, from_id);
}
void my_onClose(swServer *serv, int fd, int from_id)
{
printf("PID=%d\tClose fd=%d|from_id=%d\n", getpid(), fd, from_id);
}