1 Star 0 Fork 0

aven/tlib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TServer.cc 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
qq66921494 提交于 2015-07-31 01:07 . .
#include "TServer.h"
#include <stdio.h>
#include <iostream>
#include <algorithm>
TServer::TServer()
{
}
TServer::~TServer()
{
}
#define IO_THREAD_COUNT 2
#define BS_THREAD_COUNT 2
void TServer::Start(char* host, int port)
{
serSock_.Listen(host, port);
ioThreadPool.Start(IO_THREAD_COUNT);
bsThreadPool.Start(BS_THREAD_COUNT);
for (int i = 0; i < IO_THREAD_COUNT; i++)
ioThreadPool.Run(std::bind(&TEventLoop::RunLoop, &eventLoop_[i], this));
while (true)
{
TSocket* cli = serSock_.Accept();
cli->SetNonBlock(true);
OnConn(cli);
eventLoop_[cli->fd() % IO_THREAD_COUNT].AddLoop(cli);
}
}
void TServer::OnConn(TSocket* sock)
{
printf("TServer::OnConn() is called, sock: %d conn\n", sock->fd());
}
void TServer::OnRecv(TSocket* sock)
{
}
void TServer::OnSend(TSocket* sock)
{
}
void TServer::OnClose(TSocket* sock)
{
printf("TServer::OnClose() is called, sock: %d quit\n", sock->fd());
}
void TServer::PreRecv(TSocket* sock)
{
//std::cout << "thread[" << std::this_thread::get_id() << "] recv socket[" << sock->fd() <<"] message" << std::endl;
char buff[1024] = {0};
int len = sock->Recv(buff, 1024);
if (len > 0)
{
sock->Send(buff, len);
}
else
{
OnClose(sock);
eventLoop_[sock->fd() % IO_THREAD_COUNT].DelLoop(sock);
sock->Close();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xubingyue/tlib.git
git@gitee.com:xubingyue/tlib.git
xubingyue
tlib
tlib
master

搜索帮助