-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
executable file
·143 lines (130 loc) · 3.98 KB
/
Main.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
#include <chrono>
#include <vector>
#include <string>
#include <iostream>
#include <thread>
#include <memory>
#include <functional>
#include <boost/variant/variant.hpp>
#include <boost/asio.hpp>
//#include <boost/asio/yield.hpp>
//#include <boost/asio/coroutine.hpp>
#include <boost/asio/spawn.hpp>
using namespace std;
using namespace std::chrono_literals;
using namespace boost;
using namespace boost::asio;
class Session : public std::enable_shared_from_this<Session>, public coroutine
{
public:
Session(io_service &service)
: m_io(service), m_socket(service), m_timer(service)
{
cout << "session ctor" << std::hex << this << endl;
}
~Session()
{
cout << "session dtor" << std::hex << this << endl;
}
void start()
{
cout << "accept a new connect" << endl;
work();
// m_timer.expires_from_now(posix_time::seconds(100));
// m_timer.async_wait([self = shared_from_this()](system::error_code const &e) {
// self->stop();
// });
}
void stop()
{
cout << "stop a session" << endl;
m_socket.shutdown(socket_base::shutdown_both);
}
#if 0
void work()
{
m_socket.async_receive(buffer(m_data), [this, self = shared_from_this()](system::error_code const &e, std::size_t bytes_transferred) {
cout << "error=" << e << endl;
cout << "receive " << bytes_transferred << " byte" << endl;
if (e)
{
return;
}
m_socket.async_send(buffer(m_data), [this, self = shared_from_this()](system::error_code const &e, std::size_t bytes_transferred) {
if (e)
{
return;
}
work();
});
});
}
#elif 0
void work(system::error_code const &e = system::error_code(), std::size_t bytes_transferred = 0)
{
cout << "work>> " << e << endl;
cout << "error=" << e << endl;
cout << "receive " << bytes_transferred << " byte" << endl;
if (e)
{
return;
}
reenter(this) for (;;)
{
yield m_socket.async_receive(buffer(m_data), std::bind(&Session::work, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
yield m_socket.async_send(buffer(m_data), std::bind(&Session::work, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
}
#else
void work(system::error_code const &e = system::error_code(), std::size_t bytes_transferred = 0)
{
spawn(m_io, [this, self = shared_from_this()](yield_context yie) {
for (;;)
{
system::error_code e;
std::size_t bytes_transferred = m_socket.async_receive(buffer(m_data), yie[e]);
cout << "spawn receive " << bytes_transferred << " byte" << endl;
if (e)
{
return;
}
m_socket.async_send(buffer(m_data), yie[e]);
if (e)
{
return;
}
}
});
}
#endif
ip::tcp::socket &sock()
{
return m_socket;
}
private:
io_service &m_io;
ip::tcp::socket m_socket;
deadline_timer m_timer;
std::array<char, 1024> m_data;
};
int main()
{
int port = 18440;
io_service io;
spawn(io, [&io, port](yield_context yie) {
ip::tcp::acceptor acceptor(io, ip::tcp::endpoint(ip::tcp::v4(), port));
for (;;)
{
system::error_code e;
auto session = std::make_shared<Session>(io);
acceptor.async_accept(session->sock(), yie[e]);
if (e)
{
return;
}
session->start();
}
});
io.run();
cout << "main end" << endl;
}