From 7de2f800f9696b6144024a5055227a8a6537d2bf Mon Sep 17 00:00:00 2001 From: wangyu- Date: Wed, 29 Aug 2018 05:01:30 -0500 Subject: [PATCH] server works again --- common.cpp | 37 +++++++++++++++++++++++++++++++++++++ common.h | 4 ++-- main.cpp | 45 ++++++++++++++++++++++++++++----------------- misc.cpp | 3 +-- server.cpp | 6 ------ 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/common.cpp b/common.cpp index 3d7f85b..4aa39ba 100644 --- a/common.cpp +++ b/common.cpp @@ -347,11 +347,48 @@ int my_ip_t::from_str(char * str) } return 0; }*/ + +#if defined(__MINGW32__) +char *get_sock_error() +{ + static char buf[1000]; + int e=WSAGetLastError(); + wchar_t *s = NULL; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, e, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&s, 0, NULL); + sprintf(buf, "%d:%S", e,s); + int len=strlen(buf); + if(len>0&&buf[len-1]=='\n') buf[len-1]=0; + LocalFree(s); + return buf; +} +int get_sock_errno() +{ + return WSAGetLastError(); +} +#else +char *get_sock_error() +{ + static char buf[1000]; + sprintf(buf, "%d:%s", errno,strerror(errno)); + return buf; +} +int get_sock_errno() +{ + return errno; +} +#endif + + u64_t get_current_time() { timespec tmp_time; clock_gettime(CLOCK_MONOTONIC, &tmp_time); return ((u64_t)tmp_time.tv_sec)*1000llu+((u64_t)tmp_time.tv_nsec)/(1000*1000llu); + + //return (u64_t)(ev_time()*1000); //todo change to this later } u64_t pack_u64(u32_t a,u32_t b) diff --git a/common.h b/common.h index d461687..04422a9 100644 --- a/common.h +++ b/common.h @@ -50,11 +50,11 @@ //#include #include #include -//#include +#include //#include //signal #include #include -//#include +#include #endif diff --git a/main.cpp b/main.cpp index 5d189ef..bbf5088 100755 --- a/main.cpp +++ b/main.cpp @@ -48,26 +48,37 @@ int main(int argc, char *argv[]) assert(sizeof(unsigned long long)==8); dup2(1, 2);//redirect stderr to stdout - - struct ev_loop* loop=ev_default_loop(0); - -#if !defined(__MINGW32__) - ev_signal signal_watcher_sigpipe; - ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE); - ev_signal_start(loop, &signal_watcher_sigpipe); -#else - enable_log_color=0; +#if defined(__MINGW32__) + enable_log_color=0; #endif - ev_signal signal_watcher_sigterm; - ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM); - ev_signal_start(loop, &signal_watcher_sigterm); - - ev_signal signal_watcher_sigint; - ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT); - ev_signal_start(loop, &signal_watcher_sigint); - pre_process_arg(argc,argv); + if(program_mode==client_mode) + { + struct ev_loop* loop=ev_default_loop(0); +#if !defined(__MINGW32__) + ev_signal signal_watcher_sigpipe; + ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE); + ev_signal_start(loop, &signal_watcher_sigpipe); +#endif + + ev_signal signal_watcher_sigterm; + ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM); + ev_signal_start(loop, &signal_watcher_sigterm); + + ev_signal signal_watcher_sigint; + ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT); + ev_signal_start(loop, &signal_watcher_sigint); + } + else + { + signal(SIGINT, signal_handler); + signal(SIGHUP, signal_handler); + signal(SIGKILL, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGQUIT, signal_handler); + } + #if !defined(__MINGW32__) if(geteuid() != 0) { diff --git a/misc.cpp b/misc.cpp index 1beef9a..c9734c9 100644 --- a/misc.cpp +++ b/misc.cpp @@ -1091,7 +1091,6 @@ int unit_test() return 0; } -/* int set_timer(int epollfd,int &timer_fd)//put a timer_fd into epoll,general function,used both in client and server { int ret; @@ -1154,7 +1153,7 @@ int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64)//only for server } return 0; } -*/ + int handle_lower_level(raw_info_t &raw_info)//fill lower_level info,when --lower-level is enabled,only for server { diff --git a/server.cpp b/server.cpp index 861f068..f42e68d 100644 --- a/server.cpp +++ b/server.cpp @@ -15,7 +15,6 @@ #include "encrypt.h" #include "fd_manager.h" -#if 0 int server_easytcp=0;//currently only for test int server_on_timer_multi(conn_info_t &conn_info) //for server. called when a timer is ready in epoll.for server,there will be one timer for every connection @@ -900,8 +899,3 @@ int server_event_loop() } return 0; } -#endif -int server_event_loop() -{ - return 0; -}