-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoro_server_test.cpp
220 lines (198 loc) · 4.95 KB
/
coro_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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;
int recvXDataNum = 0;
};
class MyServer :public nicehero::TcpServer
{
public:
MyServer(const std::string& ip, ui16 port)
:TcpServer(ip, port)
{}
virtual nicehero::TcpSessionS* createSession();
};
auto MyServer::createSession()->nicehero::TcpSessionS*
{
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();
};
auto MyKcpServer::createSession()->nicehero::KcpSessionS*
{
return new MyKcpSession();
}
#ifdef NICE_HAS_CO_AWAIT
#include "Mongo.hpp"
#include "MongoAsync.hpp"
nicehero::Task<int, nicehero::TO_MULTIWORKER,nicehero::TO_MAIN> async_add(int x, int y)
{
co_return x + y;
}
nicehero::Task<int, nicehero::TO_MULTIWORKER> async_mul(int x,int y)
{
co_return x * y;
}
using copy_int = nicehero::CopyablePtr<int>;
using unique_int = std::unique_ptr<int>;
nicehero::Task<copy_int, nicehero::TO_MULTIWORKER> CoroAsyncXX4(int x,int y)
{
co_return nicehero::CopyablePtr<int>(new int(x + y + 400));
}
nicehero::Task<copy_int, nicehero::TO_MULTIWORKER> CoroAsyncXX5(unique_int x, int y)
{
int r = *x + y + 500;
co_return nicehero::CopyablePtr<int>(new int(r));
}
#endif
int main(int argc, char* argv[])
{
bool v6 = (argc > 1 && std::string(argv[1]) == "v6") ? true : false;
nicehero::start(true);
#ifdef NICE_HAS_CO_AWAIT
std::shared_ptr<nicehero::MongoConnectionPool> pool = std::make_shared<nicehero::MongoConnectionPool>();
bool r = pool->init("mongodb://192.168.9.5:27018", "test");
if (!r){
pool = std::shared_ptr<nicehero::MongoConnectionPool>();
}
#else
int pool = 0;
#endif
std::string listenIP = "0.0.0.0";
if (v6)
{
listenIP = "::";
}
nicehero::HttpServer httpServer(listenIP,8080);
auto f = [pool] HTTP_HANDLER_PARAMS
{
res->write("hello world:\n");
#ifdef NICE_HAS_CO_AWAIT
//!!!Must copy first ,if use the catch of lambda after co_await ,crash!
//!!!or If u use catch of lambda and coroutine not start from the same thread(TO_MAIN in this sample),crash!
auto nPool = pool;
std::stringstream ss;
int r = 0;
if (r == 2) {
co_return true;
}
auto xx = std::make_unique<int>(2);
//auto xx = nicehero::make_copyable<int>(2);
auto r2 = co_await CoroAsyncXX5(std::move(xx),7);
(ss = std::stringstream()) << "co_await CoroAsyncXX5(2,7):" << *r2 << "\n";
res->write(ss.str());
auto cursor = co_await nicehero::MongoPoolFindAsync(nPool
, "x"
, NBSON("_id", BCON_INT32(1))
, nicehero::Bson::createBsonPtr()
, MONGOC_READ_PRIMARY);
nlog("co_await nicehero::MongoPoolFindAsync ok");
while (auto bobj = cursor->fetch()) {
res->write(bobj->toJson());
}
r += co_await async_add(1,3);
if (r > 2) {
co_return true;
}
co_return true;
#else
return true;
#endif
};
httpServer.addHandler("/", f);
httpServer.addHandler("/v2", [pool] 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)
{
MyClient& client = (MyClient&)*session.get();
XData d;
++client.recvXDataNum;
*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,%d", int(msg->getSize()), s.c_str(), client.recvXDataNum);
#ifdef NICE_HAS_CO_AWAIT
int r = co_await async_add(2, 3);
r += 1;
#endif
nlog("tcp recv XData r:%d,%d", r, client.recvXDataNum);
session->sendMessage(d);
co_return true;
}
TCP_SESSION_COMMAND(MyClient, 101)
{
MyClient& client = (MyClient&)*session.get();
++client.recv101Num;
nlog("recv101 recv101Num:%d", client.recv101Num);
#ifdef NICE_HAS_CO_AWAIT
auto a = async_add(1, 1);
int r = co_await a;
r += 1;
#endif
nlog("recv101 r:%d", r);
co_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);
#ifdef NICE_HAS_CO_AWAIT
auto a = async_add(2, 3);
int r = co_await a;
r += 1;
#endif
nlog("recv102 r:%d,client.recv101Num", r, client.recv101Num);
co_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());
#ifdef NICE_HAS_CO_AWAIT
auto a = async_add(1, 1);
int r = co_await a;
r += 1;
#endif
nlog("kcp recv XData r:%d", r);
session->sendMessage(d);
co_return true;
}