diff --git a/code/server/webserver.cpp b/code/server/webserver.cpp index 8fa708a..9bad089 100644 --- a/code/server/webserver.cpp +++ b/code/server/webserver.cpp @@ -2,7 +2,7 @@ * @Author : mark * @Date : 2020-06-17 * @copyleft Apache 2.0 - */ + */ #include "webserver.h" @@ -10,11 +10,11 @@ using namespace std; WebServer::WebServer( int port, int trigMode, int timeoutMS, bool OptLinger, - int sqlPort, const char* sqlUser, const char* sqlPwd, + int sqlPort, const char* sqlUser, const char* sqlPwd, const char* dbName, int connPoolNum, int threadNum, bool openLog, int logLevel, int logQueSize): - port_(port), openLinger_(OptLinger), timeoutMS_(timeoutMS), isClose_(false), - timer_(new HeapTimer()), threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) + port_(port), openLinger_(OptLinger), timeoutMS_(timeoutMS), isClose_(false), + timer_(new HeapTimer()), threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) { srcDir_ = getcwd(nullptr, 256); assert(srcDir_); @@ -22,18 +22,18 @@ WebServer::WebServer( HttpConn::userCount = 0; HttpConn::srcDir = srcDir_; SqlConnPool::Instance()->Init("localhost", sqlPort, sqlUser, sqlPwd, dbName, connPoolNum); - + InitEventMode_(trigMode); if(!InitSocket_()) { isClose_ = true;} - if(openLog) { - Log::Instance()->init(logLevel, "./log", ".log", logQueSize); + if(openLog) { + Log::Instance()->init(logLevel, "./log", ".log", logQueSize); if(isClose_) { LOG_ERROR("========== Server init error!=========="); } else { LOG_INFO("========== Server init =========="); LOG_INFO("Port:%d, OpenLinger: %s", port_, OptLinger? "true":"false"); - LOG_INFO("Listen Mode: %s, OpenConn Mode: %s", - (listenEvent_ & EPOLLET ? "ET": "LT"), + LOG_INFO("Listen Mode: %s, OpenConn Mode: %s", + (listenEvent_ & EPOLLET ? "ET": "LT"), (connEvent_ & EPOLLET ? "ET": "LT")); LOG_INFO("LogSys level: %d", logLevel); LOG_INFO("srcDir: %s", HttpConn::srcDir); @@ -51,7 +51,7 @@ WebServer::~WebServer() { void WebServer::InitEventMode_(int trigMode) { listenEvent_ = EPOLLRDHUP; - connEvent_ = EPOLLONESHOT | EPOLLRDHUP; + connEvent_ = EPOLLONESHOT | EPOLLRDHUP; switch (trigMode) { case 0: @@ -185,7 +185,7 @@ void WebServer::OnProcess(HttpConn* client) { epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLIN); } } - + void WebServer::OnWrite_(HttpConn* client) { assert(client); int ret = -1; @@ -208,18 +208,18 @@ void WebServer::OnWrite_(HttpConn* client) { CloseConn_(client); } -/* Create listenFd */ -bool WebServer::InitSocket_() { +/* Create listenFd */ +bool WebServer::InitSocket_() { int ret; struct sockaddr_in addr; - if(port_ > 65535 && port_ < 1024) { + if(port_ > 65535 || port_ < 1024) { LOG_ERROR("Port:%d error!", port_); return false; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port_); - struct linger optLinger = { 0 }; + struct linger optLinger = { 0 }; if(openLinger_) { /* 优雅关闭: 直到所剩数据发送完毕或超时 */ optLinger.l_onoff = 1; @@ -238,7 +238,7 @@ bool WebServer::InitSocket_() { LOG_ERROR("Init linger error!", port_); return false; } - + int optval = 1; /* 端口复用 */ /* 只有最后一个套接字会正常接收数据。 */