diff --git a/LICENSE b/LICENSE index 261eeb9..6e79d80 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ diff --git a/build/makefile b/build/makefile deleted file mode 100644 index 10731bc..0000000 --- a/build/makefile +++ /dev/null @@ -1,18 +0,0 @@ -#heaptimer.cpp -CXX = g++ -CFLAGS = -std=c++14 -pipe -O2 -g - -TARGET = server -OBJS = ../code/log/*.cpp ../code/pool/*.cpp ../code/timer/*.cpp \ - ../code/http/*.cpp ../code/server/*.cpp \ - ../code/buffer/*.cpp ../code/main.cpp - -all: $(OBJS) - $(CXX) $(CFLAGS) $(OBJS) -o $(TARGET) -pthread -lmysqlclient - -clean: - rm -rf $(OBJS) $(TARGET) - - - - diff --git a/code/log/log.cpp b/code/log/log.cpp index dbf51c5..11d3d4f 100644 --- a/code/log/log.cpp +++ b/code/log/log.cpp @@ -49,6 +49,7 @@ void Log::init(int level = 1, const char* path, const char* suffix, if(!deque_) { unique_ptr> newDeque(new BlockDeque); deque_ = move(newDeque); + std::unique_ptr NewThread(new thread(FlushLogThread)); writeThread_ = move(NewThread); } @@ -94,7 +95,7 @@ void Log::write(int level, const char *format, ...) { { char newFile[LOG_NAME_LEN]; char tail[16] = {0}; - sprintf(tail, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday); + snprintf(tail, 20, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday); if (toDay_ != t.tm_mday) { @@ -120,16 +121,14 @@ void Log::write(int level, const char *format, ...) { va_start(vaList, format); { lock_guard locker(mtx_); - int n = sprintf(buff_.BeginWrite(), "%d-%02d-%02d %02d:%02d:%02d.%06ld ", + int n = snprintf(buff_.BeginWrite(), 128, "%d-%02d-%02d %02d:%02d:%02d.%06ld ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, now.tv_usec); buff_.HasWritten(n); - AppendLogLevel_(); - - int m = vsprintf(buff_.BeginWrite(), format, vaList); + + int m = vsnprintf(buff_.BeginWrite(), buff_.WritableBytes(), format, vaList); buff_.HasWritten(m); - buff_.Append("\n\0", 2); } diff --git a/code/log/log.h b/code/log/log.h index 01747e9..3026c14 100644 --- a/code/log/log.h +++ b/code/log/log.h @@ -20,8 +20,9 @@ class Log { public: - void init(int level, const char* path = "./log", const char* suffix =".log", - int maxQueueCapacity = 800); + void init(int level, const char* path = "./log", + const char* suffix =".log", + int maxQueueCapacity = 800); static Log* Instance(); static void FlushLogThread(); @@ -73,7 +74,6 @@ class Log }\ } while(0); - #define LOG_DEBUG(format, ...) do {LOG_BASE(0, format, ##__VA_ARGS__)} while(0); #define LOG_INFO(format, ...) do {LOG_BASE(1, format, ##__VA_ARGS__)} while(0); #define LOG_WARN(format, ...) do {LOG_BASE(2, format, ##__VA_ARGS__)} while(0); diff --git a/code/main.cpp b/code/main.cpp index 90167ca..0b35519 100644 --- a/code/main.cpp +++ b/code/main.cpp @@ -10,9 +10,9 @@ int main() { /* 守护进程 后台运行 */ //daemon(1, 0); WebServer server( - 1315, 3, true, false, /* 端口 ET模式 Proactor/Reactor 优雅退出 */ - 3306, "root", "root", "webserver", /* sql配置 */ - 1, 4, false, 2, 5000); /* 连接池 线程池 日志开关 日志等级 日志异步队列最大 */ + 1315, 3, true, false, /* 端口 ET模式 Proactor/Reactor(使用异步线程池) 优雅退出 */ + 3306, "root", "root", "webserver", /* Mysql配置 */ + 1, 6, true, 2, 5000); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */ server.Start(); } diff --git a/code/server/epoller.cpp b/code/server/epoller.cpp index 20b17c9..dffa811 100644 --- a/code/server/epoller.cpp +++ b/code/server/epoller.cpp @@ -15,8 +15,8 @@ Epoller::~Epoller() { } bool Epoller::AddFd(int fd, uint32_t events) { - if(fd < 0) return false; assert(fd >= 0); + if(fd < 0) return false; epoll_event ev = {0}; ev.data.fd = fd; ev.events = events; @@ -24,8 +24,8 @@ bool Epoller::AddFd(int fd, uint32_t events) { } bool Epoller::ModFd(int fd, uint32_t events) { - if(fd < 0) return false; assert(fd >= 0); + if(fd < 0) return false; epoll_event ev = {0}; ev.data.fd = fd; ev.events = events; diff --git a/code/server/webserver.cpp b/code/server/webserver.cpp index dbbb8f6..4b5b950 100644 --- a/code/server/webserver.cpp +++ b/code/server/webserver.cpp @@ -17,6 +17,7 @@ WebServer::WebServer( timer_(new HeapTimer()), threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) { srcDir_ = getcwd(nullptr, 256); + assert(srcDir_); strcat(srcDir_, "/resources/html"); HttpConn::userCount = 0; HttpConn::srcDir = srcDir_; @@ -85,12 +86,15 @@ void WebServer::Start() { DealListen_(); } else if(events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { + assert(users_.count(fd) > 0); CloseConn_(&users_[fd]); } else if(events & EPOLLIN) { + assert(users_.count(fd) > 0); DealRead_(&users_[fd]); } else if(events & EPOLLOUT) { + assert(users_.count(fd) > 0); DealWrite_(&users_[fd]); } else { LOG_ERROR("Unexpected event"); @@ -210,7 +214,10 @@ void WebServer::OnWrite_(HttpConn* client) { bool WebServer::InitSocket_() { int ret; struct sockaddr_in addr; - assert(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_); @@ -223,14 +230,14 @@ bool WebServer::InitSocket_() { listenFd_ = socket(AF_INET, SOCK_STREAM, 0); if(listenFd_ < 0) { - LOG_ERROR("Create socket error!", addr.sin_port); + LOG_ERROR("Create socket error!", port_); return false; } ret = setsockopt(listenFd_, SOL_SOCKET, SO_LINGER, &optLinger, sizeof(optLinger)); if(ret < 0) { close(listenFd_); - LOG_ERROR("Init linger error!", addr.sin_port); + LOG_ERROR("Init linger error!", port_); return false; } @@ -244,14 +251,14 @@ bool WebServer::InitSocket_() { ret = bind(listenFd_, (struct sockaddr *)&addr, sizeof(addr)); if(ret < 0) { - LOG_ERROR("Bind Port:%d error!", addr.sin_port); + LOG_ERROR("Bind Port:%d error!", port_); close(listenFd_); return false; } ret = listen(listenFd_, 6); if(ret < 0) { - LOG_ERROR("Listen port:%d error!", addr.sin_port); + LOG_ERROR("Listen port:%d error!", port_); close(listenFd_); return false; } diff --git a/code/server/webserver.h b/code/server/webserver.h index 253781a..407223a 100644 --- a/code/server/webserver.h +++ b/code/server/webserver.h @@ -7,9 +7,9 @@ #define WEBSERVER_H #include -#include // fcntl() -#include // close() -#include // close() +#include // fcntl() +#include // close() +#include #include #include #include @@ -32,11 +32,6 @@ class WebServer { bool openLog, int logLevel, int logQueSize); ~WebServer(); - - static const int MAX_FD = 65536; - static const time_t TIME_SLOT = 10000; - - static int SetFdNonblock(int fd); void Start(); private: @@ -47,18 +42,18 @@ class WebServer { void DealListen_(); void DealWrite_(HttpConn* client); void DealRead_(HttpConn* client); - - bool DealSignal_(bool &isTimeOut); - void DealTimeOut_(); void SendError_(int fd, const char*info); - void ExtentTime_(HttpConn* client); + void CloseConn_(HttpConn* client); + void OnRead_(HttpConn* client); void OnWrite_(HttpConn* client); - - void CloseConn_(HttpConn* client); + static const int MAX_FD = 65536; + static const time_t TIME_SLOT = 10000; /* 毫秒MS */ + static int SetFdNonblock(int fd); + int port_; bool openLinger_; bool isReactor_; @@ -74,7 +69,6 @@ class WebServer { std::unique_ptr threadpool_; std::unique_ptr epoller_; std::unordered_map users_; - }; diff --git a/test/test.cpp b/test/test.cpp index f062ca7..9bea45e 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -30,6 +30,4 @@ void TestLog() { int main() { TestLog(); - - } \ No newline at end of file diff --git a/test/webbench-1.5/socket.c b/webbench-1.5/socket.c similarity index 100% rename from test/webbench-1.5/socket.c rename to webbench-1.5/socket.c diff --git a/test/webbench-1.5/webbench.c b/webbench-1.5/webbench.c similarity index 100% rename from test/webbench-1.5/webbench.c rename to webbench-1.5/webbench.c