-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
226 lines (195 loc) · 5.54 KB
/
Copy pathmain.cpp
File metadata and controls
226 lines (195 loc) · 5.54 KB
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
221
222
223
224
225
226
/*
* Copyright 2016, Andrej Kislovskij
*
* This is PUBLIC DOMAIN software so use at your own risk as it comes
* with no warranties. This code is yours to share, use and modify without
* any restrictions or obligations.
*
* For more information see conwrap/LICENSE or refer refer to http://unlicense.org
*
* Author: gimesketvirtadieni at gmail dot com (Andrej Kislovskij)
*/
#include <asio.hpp>
#include <chrono>
#include <conwrap/ProcessorAsio.hpp>
#include <conwrap/ProcessorAsioProxy.hpp>
#include <conwrap/ProcessorQueue.hpp>
#include <conwrap/ProcessorQueueProxy.hpp>
#include <conwrap/ProcessorProxy.hpp>
#include <functional>
#include <iostream>
class Server
{
public:
Server(short p)
: processorProxyPtr(nullptr)
, port(p) {}
void close()
{
// closing acceptor
acceptorPtr->close();
// closing socket
if (socketPtr->is_open())
{
try {
socketPtr->shutdown(asio::socket_base::shutdown_both);
} catch (...) {}
try {
socketPtr->close();
} catch (...) {}
}
}
void ping()
{
if (socketPtr->is_open())
{
socketPtr->send(asio::buffer("ping\n\r"));
std::cout << "'ping' message was sent to client\n\r";
}
}
void setProcessorProxy(conwrap::ProcessorAsioProxy<Server>* p)
{
processorProxyPtr = p;
// creating an acceptor
acceptorPtr = std::make_unique<asio::ip::tcp::acceptor>(
*processorProxyPtr->getDispatcher(),
asio::ip::tcp::endpoint(
asio::ip::tcp::v4(),
port
)
);
// creating a socket
socketPtr = std::make_unique<asio::ip::tcp::socket>(
*processorProxyPtr->getDispatcher()
);
acceptorPtr->async_accept(
*socketPtr,
[=](const std::error_code error)
{
// wrapping is needed to pass handler from asio's thread to the processor's thread
processorProxyPtr->wrap([=]
{
// start receiving data
onData(error, 0);
})();
}
);
}
protected:
void onData(const std::error_code error, const std::size_t receivedSize)
{
if (!error)
{
// if there is data to send back to the client
if (receivedSize > 0)
{
socketPtr->send(asio::buffer(buffer, receivedSize));
}
// keep receiving
socketPtr->async_read_some(
asio::buffer(buffer, BUFFER_SIZE),
[=](const std::error_code error, const std::size_t receivedSize)
{
// wrapping is needed to pass handler from asio's thread to the processor's thread
processorProxyPtr->wrap([=]
{
onData(error, receivedSize);
})();
}
);
}
}
private:
conwrap::ProcessorAsioProxy<Server>* processorProxyPtr;
short port;
std::unique_ptr<asio::ip::tcp::acceptor> acceptorPtr;
std::unique_ptr<asio::ip::tcp::socket> socketPtr;
enum {
BUFFER_SIZE = 1024
};
char buffer[BUFFER_SIZE];
};
int main(int argc, char *argv[])
{
{
conwrap::ProcessorQueue<bool> processorQueue;
processorQueue.process([]
{
std::cout << "Hello from queue task\n\r";
});
}
std::cout << "Back to main\n\r";
// example demonstrating that Asio handlers are flushable now
{
conwrap::ProcessorAsio<bool> processorAsio;
processorAsio.process([]
{
std::cout << "Hello from asio task\n\r";
});
processorAsio.flush();
std::cout << "Back to main\n\r";
}
// example demonstrating composition of tasks
{
conwrap::ProcessorQueue<bool> processorQueue;
processorQueue.process([]() -> int
{
std::cout << "Hello from the 1'st task\n\r";
return 123;
}).then([](auto context) -> bool
{
std::cout << "Hello from the 2'nd task (result from previous task is " << context.getResult() << ")\n\r";
context.getProcessorProxy()->process([]() -> int
{
std::cout << "Hello from the 2.1'st task\n\r";
return 1234;
}).then([](auto context) -> bool
{
std::cout << "Hello from the 2.2'nd task (result from previous task is " << context.getResult() << ")\n\r";
return true;
}).then([](auto context)
{
std::cout << "Hello from the 2.3'rd task (result from previous task is " << context.getResult() << ")\n\r";
});
return false;
}).then([](auto context)
{
std::cout << "Hello from the 3'rd task (result from previous task is " << context.getResult() << ")\n\r";
});
processorQueue.flush();
std::cout << "Back to main\n\r";
}
// example demonstrating combining conwrap task with asio handlers
{
conwrap::ProcessorAsio<Server> processorAsio(1234);
std::cout << "Echo server is listening on 1234 port (press ^C to terminate)\n\r";
// now server runs on a separate thread; any other logic can be done here
// submitting a task in a regular way
processorAsio.process([]() -> int
{
std::cout << "Hello from asio task\n\r";
// this is just to demonstrate that now asio can be used with tasks that return value
return 1234;
}).get();
for (int i = 0; i < 5; i++)
{
// submitting a task directly via asio io_service
processorAsio.getDispatcher()->post(
// a task submitted directly via asio io_service must be wrapped as following
processorAsio.wrap([&]
{
processorAsio.getResource()->ping();
})
);
// sleeping
std::this_thread::sleep_for(std::chrono::seconds{5});
}
// without this close invocation, flush would wait forever for any handlers
// sync socket operations are thread safe since asio 1.4.0 so posting to asio thread is optional
processorAsio.getResource()->close();
// after this flush all tasks including asio handlers are executed
processorAsio.flush();
std::cout << "All tasks were executed\n\r";
}
return 0;
}