1 Star 3 Fork 3

贺先生/HEpoll

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ServerTest.cpp 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
hejinlong 提交于 2020-09-15 16:34 . fill sample code.
#include <cstdio>
#include <iostream>
#include "HEpoll.h"
#include "AliasRef.h"
/**
* 测试服务端功能
*/
#define CHKRET(r) if(r){printf("line:%d " #r "=%d\n", __LINE__, r); return r;}
atomic_int g_recv_count(0);
atomic_ullong g_recv_bytes(0);
void OnRequest(const string& msg, weak_ptr<ConnectMeta> wconnMeta)
{
//printf("recv Req: %s\n", msg.c_str());
auto connMeta = wconnMeta.lock();
//connMeta->sendMsg(msg);
connMeta->sendMsg("R\n");
//g_recv_count += 1;
//g_recv_bytes += msg.size();
}
void SplitReqest(queue<string>& reqQueue, string& stream, weak_ptr<ConnectMeta>)
{
g_recv_count += 1;
g_recv_bytes += stream.size();
reqQueue.push("R");
printf("recv bytes: %llu count:%d\n", g_recv_bytes.load(), g_recv_count.load());
stream.clear();
}
int main(int argc, char** argv)
{
HEpoll hep;
int ret = hep.init();
CHKRET(ret);
hep.setAliasNameInstance(make_shared<AliasRef<ConnectMetaSPtr>>());
hep.run(true);
auto lisnMeta = hep.addTcpListen(argc > 1? stoi(argv[1]) :8888);
ret = lisnMeta->setKeepAlive(60);
CHKRET(ret);
lisnMeta->onMessageReachFunc = OnRequest;
lisnMeta->splitReqFunc = SplitReqest;
lisnMeta->start();
ConnectMetaSPtr connMeta;
string input;
while (cin >> input, input != "q")
{
if ("r" == input)
{
g_recv_bytes = 0;
g_recv_count = 0;
}
else if ('p' == input[0])
{
printf("hep status=%s\n", hep.toString().c_str());
printf("recv=%llu bytes %d rows\n", g_recv_bytes.load(), g_recv_count.load());
if (input[1] > 0)
{
int fd = stoi(input.c_str()+1);
auto connMeta = hep.find(fd);
if (connMeta)
{
printf("fd=%d : %s\n", fd, connMeta->toString().c_str());
}
}
}
input.clear();
}
printf("notify Exit");
hep.notifyExit();
hep.unInit();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ho999/hepoll.git
git@gitee.com:ho999/hepoll.git
ho999
hepoll
HEpoll
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385