From 5a4d4508a70bd8c1f494d0a15073e71313038c1e Mon Sep 17 00:00:00 2001 From: Date: Mon, 2 Oct 2017 19:38:45 +0800 Subject: [PATCH] server --- nefdaemon/server.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 nefdaemon/server.cpp diff --git a/nefdaemon/server.cpp b/nefdaemon/server.cpp new file mode 100644 index 0000000..d236f37 --- /dev/null +++ b/nefdaemon/server.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + +#define DEFAULT_PORT 7000 +#define DEFAULT_BACKLOG 128 + +uv_loop_t *loop; +struct sockaddr_in addr; + +typedef struct { + uv_write_t req; + uv_buf_t buf; +} write_req_t; + +void daemonize(void) +{ + //下面用于屏蔽一些有关控制终端操作的信号 + + //防止守护进程没有正常运作之前控制终端受到干扰退出或挂起 +#ifdef SIGTTOU + signal(SIGTTOU, SIG_IGN); //屏蔽后台进程写控制终端信号 +#endif +#ifdef SIGTTIN + signal(SIGTTIN, SIG_IGN); //屏蔽后台读控制终端信号 +#endif +#ifdef SIGTSTP + signal(SIGTSTP, SIG_IGN); //忽略终端挂起 +#endif + if (fork() != 0) + exit(0); //fork()产生子进程被init进程收养,父进程退出,进程变成daemon + if (setsid() < 0) + exit(0); //setsid()创建新会话,并成为首进程 + signal(SIGHUP, SIG_IGN); + if (fork() != 0) + exit(0); //再fork()一次防鬼畜 + if (chdir("/home/fanghr") != 0) + exit(0); //切换工作目录 +} + +void free_write_req(uv_write_t *req) { + write_req_t *wr = (write_req_t*)req; + free(wr->buf.base); + free(wr); +} + +void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { + buf->base = (char*)malloc(suggested_size); + buf->len = suggested_size; +} + +void on_close(uv_handle_t* handle) { + free(handle); +} + +void echo_write(uv_write_t *req, int status) { + if (status) { + fprintf(stderr, "Write error %s\n", uv_strerror(status)); + } + free_write_req(req); +} + +void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { + if (nread > 0) { + write_req_t *req = (write_req_t*)malloc(sizeof(write_req_t)); + req->buf = uv_buf_init(buf->base, nread); + uv_write((uv_write_t*)req, client, &req->buf, 1, echo_write); + return; + } + if (nread < 0) { + if (nread != UV_EOF) + fprintf(stderr, "Read error %s\n", uv_err_name(nread)); + uv_close((uv_handle_t*)client, on_close); + } + + free(buf->base); +} + +void on_new_connection(uv_stream_t *server, int status) { + if (status < 0) { + fprintf(stderr, "New connection error %s\n", uv_strerror(status)); + // error! + return; + } + FILE* fp = fopen("/tmp/test.log", "a"); + fprintf(fp, "%s\n", "newconnection"); + fclose(fp); + uv_tcp_t *client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t)); + uv_tcp_init(loop, client); + if (uv_accept(server, (uv_stream_t*)client) == 0) { + uv_read_start((uv_stream_t*)client, alloc_buffer, echo_read); + } + else { + uv_close((uv_handle_t*)client, on_close); + } +} + +int main() { + daemonize(); + loop = uv_default_loop(); + + uv_tcp_t server; + uv_tcp_init(loop, &server); + + uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr); + + uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0); + int r = uv_listen((uv_stream_t*)&server, DEFAULT_BACKLOG, on_new_connection); + if (r) { + fprintf(stderr, "Listen error %s\n", uv_strerror(r)); + return 1; + } + return uv_run(loop, UV_RUN_DEFAULT); +} + -- Gitee