1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| #include "webserver.h"
WebServer::WebServer( int port, int trigMode, int timeoutMS, bool OptLinger, 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()) { srcDir_ = getcwd(nullptr, 256); assert(srcDir_); strncat(srcDir_, "/resources/", 16); 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 (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"), (connEvent_ & EPOLLET ? "ET" : "LT")); LOG_INFO("LogSys level: %d", logLevel); LOG_INFO("srcDir: %s", HttpConn::srcDir); LOG_INFO("SqlConnPool num: %d, ThreadPool num: %d", connPoolNum, threadNum); } } }
WebServer::~WebServer() { close(listenFd_); isClose_ = true; free(srcDir_); SqlConnPool::Instance()->ClosePool(); }
void WebServer::Start() { int timeMS = -1; if (!isClose_) { LOG_INFO("========== Server start =========="); } while (!isClose_) { if (timeoutMS_ > 0) timeMS = timer_->GetNextTick(); int eventCnt = epoller_->Wait(timeMS); for (int i = 0; i < eventCnt; i++) { int fd = epoller_->GetEventFd(i); uint32_t events = epoller_->GetEvents(i); if (fd == listenFd_) { 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"); } } } }
bool WebServer::InitSocket_() { int ret; struct sockaddr_in addr; 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}; if (openLinger_) { optLinger.l_onoff = 1; optLinger.l_linger = 1; } listenFd_ = socket(AF_INET, SOCK_STREAM, 0); if (listenFd_ < 0) { 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!", port_); return false; }
int optval = 1; ret = setsockopt(listenFd_, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int)); if (ret == -1) { LOG_ERROR("set socket setsockopt error !"); close(listenFd_); return false; }
ret = bind(listenFd_, (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) { LOG_ERROR("Bind Port:%d error!", port_); close(listenFd_); return false; }
ret = listen(listenFd_, 6); if (ret < 0) { LOG_ERROR("Listen port:%d error!", port_); close(listenFd_); return false; } ret = epoller_->AddFd(listenFd_, listenEvent_ | EPOLLIN); if (ret == 0) { LOG_ERROR("Add listen error!"); close(listenFd_); return false; } SetFdNonblock(listenFd_); LOG_INFO("Server port:%d", port_); return true; } void WebServer::InitEventMode_(int trigMode) { listenEvent_ = EPOLLRDHUP; connEvent_ = EPOLLONESHOT | EPOLLRDHUP; switch (trigMode) { case 0: break; case 1: connEvent_ |= EPOLLET; break; case 2: listenEvent_ |= EPOLLET; break; case 3: listenEvent_ |= EPOLLET; connEvent_ |= EPOLLET; break; default: listenEvent_ |= EPOLLET; connEvent_ |= EPOLLET; break; } HttpConn::isET = (connEvent_ & EPOLLET); }
void WebServer::AddClient_(int fd, sockaddr_in addr) { assert(fd > 0); users_[fd].init(fd, addr); if (timeoutMS_ > 0) { timer_->add(fd, timeoutMS_, std::bind(&WebServer::CloseConn_, this, &users_[fd])); } epoller_->AddFd(fd, EPOLLIN | connEvent_); SetFdNonblock(fd); LOG_INFO("Client[%d] in!", users_[fd].GetFd()); }
void WebServer::DealListen_() { struct sockaddr_in addr; socklen_t len = sizeof(addr); do { int fd = accept(listenFd_, (struct sockaddr *)&addr, &len); if (fd <= 0) return; else if (HttpConn::userCount >= MAX_FD) { SendError_(fd, "Server busy!"); LOG_WARN("Clients is full!"); return; } AddClient_(fd, addr); } while (listenEvent_ & EPOLLET); }
void WebServer::DealWrite_(HttpConn *client) { assert(client); ExtentTime_(client); threadpool_->AddTask(std::bind(&WebServer::OnWrite_, this, client)); }
void WebServer::DealRead_(HttpConn *client) { assert(client); ExtentTime_(client); threadpool_->AddTask(std::bind(&WebServer::OnRead_, this, client)); }
void WebServer::SendError_(int fd, const char *info) { assert(fd > 0); int ret = send(fd, info, strlen(info), 0); if (ret < 0) { LOG_WARN("send error to client[%d] error!", fd); } close(fd); }
void WebServer::ExtentTime_(HttpConn *client) { assert(client); if (timeoutMS_ > 0) { timer_->adjust(client->GetFd(), timeoutMS_); } }
void WebServer::CloseConn_(HttpConn *client) { assert(client); LOG_INFO("Client[%d] quit!", client->GetFd()); epoller_->DelFd(client->GetFd()); client->Close(); }
void WebServer::OnRead_(HttpConn *client) { assert(client); int res = -1; int readErrno = 0; res = client->read(&readErrno); if (res <= 0 && readErrno != EAGAIN) { CloseConn_(client); return; } OnProcess(client); }
void WebServer::OnWrite_(HttpConn *client) { assert(client); int res = -1; int writeErrno = 0; res = client->write(&writeErrno); if (client->ToWriteBytes() == 0) { if (client->IsKeepAlive()) { OnProcess(client); return; } } else if (res < 0) { if (writeErrno == EAGAIN) { epoller_->AddFd(client->GetFd(), connEvent_ | EPOLLOUT); return; } }
CloseConn_(client); }
void WebServer::OnProcess(HttpConn *client) { if (client->process()) { epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLOUT); } else { epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLIN); } }
int WebServer::SetFdNonblock(int fd) { assert(fd > 0); return fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK); }
|