Skip to content

Commit

Permalink
🎨 improved structure
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Jun 28, 2020
1 parent 3cb745b commit 4f2b952
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 54 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
18 changes: 0 additions & 18 deletions build/makefile

This file was deleted.

11 changes: 5 additions & 6 deletions code/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void Log::init(int level = 1, const char* path, const char* suffix,
if(!deque_) {
unique_ptr<BlockDeque<std::string>> newDeque(new BlockDeque<std::string>);
deque_ = move(newDeque);

std::unique_ptr<std::thread> NewThread(new thread(FlushLogThread));
writeThread_ = move(NewThread);
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -120,16 +121,14 @@ void Log::write(int level, const char *format, ...) {
va_start(vaList, format);
{
lock_guard<mutex> 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);
}

Expand Down
6 changes: 3 additions & 3 deletions code/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions code/server/epoller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ 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;
return 0 == epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &ev);
}

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;
Expand Down
17 changes: 12 additions & 5 deletions code/server/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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_);
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
24 changes: 9 additions & 15 deletions code/server/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#define WEBSERVER_H

#include <unordered_map>
#include <fcntl.h> // fcntl()
#include <unistd.h> // close()
#include <assert.h> // close()
#include <fcntl.h> // fcntl()
#include <unistd.h> // close()
#include <assert.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
Expand All @@ -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:
Expand All @@ -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_;
Expand All @@ -74,7 +69,6 @@ class WebServer {
std::unique_ptr<ThreadPool> threadpool_;
std::unique_ptr<Epoller> epoller_;
std::unordered_map<int, HttpConn> users_;

};


Expand Down
2 changes: 0 additions & 2 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ void TestLog() {

int main() {
TestLog();


}
File renamed without changes.
File renamed without changes.

0 comments on commit 4f2b952

Please sign in to comment.