- 写入出错处理
- Send Response时,如果对方不读了,继续写入就没有意义了
- 此时写入出现问题,系统会给进程发送SIGPIPE信号
- HttpServer初始化时,要将SIGPIPE进行忽略
- 如果不忽略,在向对端写入的时候,如果对端关闭链接,而Server正在写,可能直接崩溃Server
class HttpServer
{
public:HttpServer(int port = PORT): _port(port), _stop(false){}void Init(){signal(SIGPIPE, SIG_IGN);}void Loop(int threadNum = THREAD_POOL_NUM){TcpServer *tsvr = TcpServer::GetInstance(_port);LOG(INFO, "Loop Begin");while(!_stop){struct sockaddr_in peer;socklen_t len = sizeof(peer);int sock = accept(tsvr->Sock(), (struct sockaddr *)&peer, &len);if(sock < 0){continue;}LOG(INFO, "Get a new link");Task task(sock);ThreadPool::GetInstance(threadNum)->Push(task);}}private:uint16_t _port;bool _stop;
};