From d92d3f194d7f70a999b77f7924dffb3a065ac831 Mon Sep 17 00:00:00 2001 From: yanyiwu Date: Mon, 11 Jan 2016 14:33:43 +0800 Subject: [PATCH] upgrade husky to version v0.2.2 --- deps/husky/thread_pool_server.h | 93 ++++++++++++++++++++++++++-- deps/husky/worker_thread.h | 105 -------------------------------- 2 files changed, 89 insertions(+), 109 deletions(-) delete mode 100644 deps/husky/worker_thread.h diff --git a/deps/husky/thread_pool_server.h b/deps/husky/thread_pool_server.h index 0ffcd11..3716274 100644 --- a/deps/husky/thread_pool_server.h +++ b/deps/husky/thread_pool_server.h @@ -2,15 +2,23 @@ #define HUSKY_THREADPOOLSERVER_H #include "net_util.h" -#include "worker_thread.h" +#include "irequest_handler.h" +#include "limonp/ThreadPool.hpp" namespace husky { using namespace limonp; +const char* const CLIENT_IP_K = "CLIENT_IP"; +const size_t RECV_BUFFER_SIZE = 16 * 1024; + +const struct linger LNG = {1, 1}; +const struct timeval SOCKET_TIMEOUT = {16, 0}; + + class ThreadPoolServer { public: - ThreadPoolServer(size_t thread_number, size_t queue_max_size, size_t port, IRequestHandler & handler): - pool_(thread_number, queue_max_size), req_handler_(handler), host_socket_(-1) { + ThreadPoolServer(size_t thread_number, size_t port, IRequestHandler & handler): + pool_(thread_number), req_handler_(handler), host_socket_(-1) { host_socket_ = CreateAndListenSocket(port); } ~ThreadPoolServer() {}; @@ -26,12 +34,89 @@ class ThreadPoolServer { LOG(ERROR) << strerror(errno); break; } - pool_.Add(CreateTask(clientSock, req_handler_)); + pool_.Add(NewClosure(this, &ThreadPoolServer::Run, clientSock)); + //pool_.Add(CreateTask(clientSock, req_handler_)); } return true; } private: + void Run(int sockfd) { + do { + if(!SetSockopt(sockfd)) { + LOG(ERROR) << "_getsockopt failed."; + break; + } + string strSnd, strRetByHandler; + HttpReqInfo httpReq; + if(!Receive(sockfd, httpReq)) { + LOG(ERROR) << "Receive failed."; + break; + } + + if(httpReq.IsGET() && !req_handler_.DoGET(httpReq, strRetByHandler)) { + LOG(ERROR) << "DoGET failed."; + break; + } + if(httpReq.IsPOST() && !req_handler_.DoPOST(httpReq, strRetByHandler)) { + LOG(ERROR) << "DoPOST failed."; + break; + } + strSnd = StringFormat(HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str()); + + if(!Send(sockfd, strSnd)) { + LOG(ERROR) << "Send failed."; + break; + } + } while(false); + + + if(-1 == close(sockfd)) { + LOG(ERROR) << strerror(errno); + } + } + bool Receive(int sockfd, HttpReqInfo& httpInfo) const { + char recvBuf[RECV_BUFFER_SIZE]; + int n = 0; + while(!httpInfo.IsBodyFinished() && (n = recv(sockfd, recvBuf, RECV_BUFFER_SIZE, 0)) > 0) { + if(!httpInfo.IsHeaderFinished()) { + if(!httpInfo.ParseHeader(recvBuf, n)) { + LOG(ERROR) << "ParseHeader failed. "; + return false; + } + continue; + } + httpInfo.AppendBody(recvBuf, n); + } + if(n < 0) { + LOG(ERROR) << strerror(errno); + return false; + } + return true; + } + bool Send(int sockfd, const string& strSnd) const { + if(-1 == send(sockfd, strSnd.c_str(), strSnd.length(), 0)) { + LOG(ERROR) << strerror(errno); + return false; + } + return true; + } + bool SetSockopt(int sockfd) const { + if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&LNG, sizeof(LNG))) { + LOG(ERROR) << strerror(errno); + return false; + } + if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT))) { + LOG(ERROR) << strerror(errno); + return false; + } + if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT))) { + LOG(ERROR) << strerror(errno); + return false; + } + return true; + } + ThreadPool pool_; IRequestHandler & req_handler_; int host_socket_; diff --git a/deps/husky/worker_thread.h b/deps/husky/worker_thread.h deleted file mode 100644 index 1377f5a..0000000 --- a/deps/husky/worker_thread.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef HUSKY_WORKER_HPP -#define HUSKY_WORKER_HPP - -#include "limonp/ThreadPool.hpp" -#include "irequest_handler.h" -#include "net_util.h" - -namespace husky { -const char* const CLIENT_IP_K = "CLIENT_IP"; -const size_t RECV_BUFFER_SIZE = 16 * 1024; - -const struct linger LNG = {1, 1}; -const struct timeval SOCKET_TIMEOUT = {16, 0}; - -class WorkerThread: public ITask { - public: - WorkerThread(int sockfs, IRequestHandler& reqHandler): - sockfd_(sockfs), req_handler_(reqHandler) { - } - virtual ~WorkerThread() { - } - - virtual void Run() { - do { - if(!SetSockopt(sockfd_)) { - LOG(ERROR) << "_getsockopt failed."; - break; - } - string strSnd, strRetByHandler; - HttpReqInfo httpReq; - if(!Receive(sockfd_, httpReq)) { - LOG(ERROR) << "Receive failed."; - break; - } - - if(httpReq.IsGET() && !req_handler_.DoGET(httpReq, strRetByHandler)) { - LOG(ERROR) << "DoGET failed."; - break; - } - if(httpReq.IsPOST() && !req_handler_.DoPOST(httpReq, strRetByHandler)) { - LOG(ERROR) << "DoPOST failed."; - break; - } - strSnd = StringFormat(HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str()); - - if(!Send(sockfd_, strSnd)) { - LOG(ERROR) << "Send failed."; - break; - } - } while(false); - - - if(-1 == close(sockfd_)) { - LOG(ERROR) << strerror(errno); - } - } - private: - bool Receive(int sockfd, HttpReqInfo& httpInfo) const { - char recvBuf[RECV_BUFFER_SIZE]; - int n = 0; - while(!httpInfo.IsBodyFinished() && (n = recv(sockfd, recvBuf, RECV_BUFFER_SIZE, 0)) > 0) { - if(!httpInfo.IsHeaderFinished()) { - if(!httpInfo.ParseHeader(recvBuf, n)) { - LOG(ERROR) << "ParseHeader failed. "; - return false; - } - continue; - } - httpInfo.AppendBody(recvBuf, n); - } - if(n < 0) { - LOG(ERROR) << strerror(errno); - return false; - } - return true; - } - bool Send(int sockfd, const string& strSnd) const { - if(-1 == send(sockfd, strSnd.c_str(), strSnd.length(), 0)) { - LOG(ERROR) << strerror(errno); - return false; - } - return true; - } - bool SetSockopt(int sockfd) const { - if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&LNG, sizeof(LNG))) { - LOG(ERROR) << strerror(errno); - return false; - } - if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT))) { - LOG(ERROR) << strerror(errno); - return false; - } - if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT))) { - LOG(ERROR) << strerror(errno); - return false; - } - return true; - } - - int sockfd_; - IRequestHandler& req_handler_; -}; -} - -#endif