-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_test.cpp
124 lines (110 loc) · 2.46 KB
/
server_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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;
}