代码拉取完成,页面将自动刷新
// asio_server.cpp : Defines the entry point for the console application.
//
#include "Service.h"
#include "HttpServer.h"
#include "Tcp.h"
#include "Log.h"
#include "TestProtocol.h"
#include "Clock.h"
#include "Kcp.h"
#include "CopyablePtr.hpp"
class MyClient :public nicehero::TcpSessionS
{
public:
int recv101Num = 0;
};
class MyServer :public nicehero::TcpServer
{
public:
MyServer(const std::string& ip, ui16 port)
:TcpServer(ip, port)
{}
virtual nicehero::TcpSessionS* createSession();
};
nicehero::TcpSessionS* MyServer::createSession()
{
return new MyClient();
}
class MyKcpSession : public nicehero::KcpSessionS
{
public:
};
class MyKcpServer :public nicehero::KcpServer
{
public:
MyKcpServer(const std::string& ip, ui16 port)
:KcpServer(ip, port)
{}
virtual nicehero::KcpSessionS* createSession();
};
nicehero::KcpSessionS* MyKcpServer::createSession()
{
return new MyKcpSession();
}
int main(int argc, char* argv[])
{
bool v6 = (argc > 1 && std::string(argv[1]) == "v6") ? true : false;
nicehero::start(true);
std::string listenIP = "0.0.0.0";
if (v6)
{
listenIP = "::";
}
nicehero::HttpServer httpServer(listenIP,8080);
httpServer.addHandler("/", [] HTTP_HANDLER_PARAMS
{
res->write("hello world:\n");
return true;
});
httpServer.addHandler("/v2", [] HTTP_HANDLER_PARAMS
{
res->write("hello world v2:\n");
return true;
});
httpServer.start();
MyServer tcpServer(listenIP, 7000);
tcpServer.accept();
MyKcpServer kcpServer(listenIP, 7001);
kcpServer.accept();
nicehero::joinMain();
return 0;
}
using namespace Proto;
TCP_SESSION_COMMAND(MyClient, XDataID)
{
XData d;
*msg >> d;
d.s1 = "xxxx";
std::string s;
s.assign(d.s2.m_Data.get(), d.s2.m_Size);
nlog("tcp recv XData size:%d,%s", int(msg->getSize()),s.c_str());
MyClient& client = (MyClient&)*session.get();
client.sendMessage(d);
return true;
}
TCP_SESSION_COMMAND(MyClient, 101)
{
MyClient& client = (MyClient&)*session.get();
//nlog("recv101 recv101Num:%d", client.recv101Num);
++client.recv101Num;
return true;
}
static int numClients = 0;
TCP_SESSION_COMMAND(MyClient, 102)
{
MyClient& client = (MyClient&)*session.get();
++numClients;
nlog("tcp recv102 recv101Num:%d,%d", client.recv101Num,numClients);
return true;
}
KCP_SESSION_COMMAND(MyKcpSession, XDataID)
{
XData d;
*msg >> d;
d.s1 = "xxxx";
std::string s;
s.assign(d.s2.m_Data.get(), d.s2.m_Size);
nlog("kcp recv XData size:%d,%s", int(msg->getSize()),s.c_str());
session->sendMessage(d);
return true;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。