acl/lib_acl/samples/server/main.c

73 lines
1.4 KiB
C
Raw Normal View History

#include "lib_acl.h"
2014-11-19 00:25:21 +08:00
#include <stdio.h>
#include <stdlib.h>
static void init(void)
{
acl_lib_init();
2017-04-07 20:13:08 +08:00
acl_msg_stdout_enable(1);
2014-11-19 00:25:21 +08:00
}
static void end(void)
{
acl_lib_end();
2014-11-19 00:25:21 +08:00
}
static void thread_run(void *arg)
{
ACL_VSTREAM *client = (ACL_VSTREAM*) arg;
char buf[8192];
int ret;
ret = acl_vstream_read(client, buf, sizeof(buf));
if (ret == ACL_VSTREAM_EOF) {
acl_vstream_close(client);
return;
}
buf[ret] = 0;
2017-04-07 20:13:08 +08:00
(void) acl_vstream_write(client, buf, ret);
2014-11-19 00:25:21 +08:00
acl_vstream_close(client);
}
2017-04-07 20:13:08 +08:00
static void run(const char *addr)
2014-11-19 00:25:21 +08:00
{
unsigned flag = ACL_INET_FLAG_REUSEPORT;
int bufsize = 4096, timeout = 10;
ACL_VSTREAM *sstream = acl_vstream_listen_ex(addr, 128,
flag, bufsize, timeout);
2014-11-19 00:25:21 +08:00
acl_pthread_pool_t *pool;
if (sstream == NULL) {
acl_msg_error("listen %s error(%s)", addr, acl_last_serror());
return;
}
printf("listening on %s ...\n", addr);
pool = acl_thread_pool_create(100, 120);
while (1) {
ACL_VSTREAM *client = acl_vstream_accept(sstream, NULL, 0);
if (client == NULL) {
acl_msg_error("accept error(%s)", acl_last_serror());
break;
}
2017-04-07 20:13:08 +08:00
printf("accept one client\r\n");
2014-11-19 00:25:21 +08:00
acl_pthread_pool_add(pool, thread_run, client);
}
acl_vstream_close(sstream);
}
2017-04-07 20:13:08 +08:00
int main(int argc, char *argv[])
2014-11-19 00:25:21 +08:00
{
2017-04-07 20:13:08 +08:00
const char *addr = "0:8809";
if (argc >= 2)
addr = argv[1];
2014-11-19 00:25:21 +08:00
init();
2017-04-07 20:13:08 +08:00
run(addr);
2014-11-19 00:25:21 +08:00
end();
return (0);
}