代码拉取完成,页面将自动刷新
#pragma once
#include <iostream>
#include <memory>
#include <functional>
#include <sys/wait.h>
#include "Socket.hpp"
namespace TcpServerModule
{
using namespace SocketModule;
using namespace LogModule;
using tcphandler_t = std::function<bool(SockPtr, InetAddr)>;
// 它只负责进行IO,流失的IO,不对协议进行任何处理
class TcpServer
{
public:
TcpServer(int port)
: _listensockp(std::make_unique<TcpSocket>())
, _running(false)
, _port(port)
{
}
void InitServer(tcphandler_t handler)
{
_handler = handler;
_listensockp->BulidTcpSocketMethod(_port);
}
void Loop()
{
_running = true;
while (_running)
{
// 1.Accept
InetAddr clientaddr;
auto sockfd = _listensockp->Accepter(&clientaddr);
if (sockfd == nullptr)
continue;
// 2.IO处理
LOG(LogLevel::DEBUG) << "get a new client,info is : " << clientaddr.Addr();
// sockfd->Recv();
// sockfd->Send();
pid_t id = fork();
if (id == 0)
{
_listensockp->Close();
if (fork() > 0)
exit(0);
_handler(sockfd,clientaddr);
exit(0);
}
sockfd->Close();
waitpid(id, nullptr, 0);
}
_running = false;
}
~TcpServer()
{
_listensockp->Close();
}
private:
// 一定要有一个Listensock
std::unique_ptr<Socket> _listensockp;
bool _running;
tcphandler_t _handler;
int _port;
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。