1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | #include <csignal> |
11 | #include <string> |
12 | |
13 | extern "C" { |
14 | #include <uv.h> |
15 | } |
16 | |
17 | #include "../cmd.hpp" |
18 | #include "../errors.hpp" |
19 | #include "../messages.h" |
20 | #include "../player/player.hpp" |
21 | #include "io_reactor.hpp" |
22 | #include "io_response.hpp" |
23 | |
24 | const std::uint16_t IoReactor::PLAYER_UPDATE_PERIOD = 5; |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | struct WriteReq { |
33 | uv_write_t req; |
34 | uv_buf_t buf; |
35 | }; |
36 | |
37 | |
38 | void UvAlloc(uv_handle_t *, size_t suggested_size, uv_buf_t *buf) |
39 | { |
40 | *buf = uv_buf_init(new char[suggested_size](), suggested_size); |
| |
| |
41 | } |
42 | |
43 | |
44 | void UvCloseCallback(uv_handle_t *handle) |
45 | { |
46 | if (handle->data != nullptr) { |
47 | auto tcp = static_cast<TcpResponseSink *>(handle->data); |
48 | tcp->Close(); |
49 | } |
50 | delete handle; |
51 | } |
52 | |
53 | |
54 | void UvReadCallback(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) |
55 | { |
56 | TcpResponseSink *tcp = static_cast<TcpResponseSink *>(stream->data); |
57 | tcp->Read(stream, nread, buf); |
58 | } |
59 | |
60 | |
61 | void UvListenCallback(uv_stream_t *server, int status) |
62 | { |
63 | if (status == -1) { |
64 | return; |
65 | } |
66 | IoReactor *reactor = static_cast<IoReactor *>(server->data); |
67 | reactor->NewConnection(server); |
68 | } |
69 | |
70 | |
71 | void UvRespondCallback(uv_write_t *req, int) |
72 | { |
73 | |
74 | WriteReq *wr = (WriteReq *)req; |
75 | delete[] wr->buf.base; |
76 | delete wr; |
77 | } |
78 | |
79 | |
80 | void UvUpdateTimerCallback(uv_timer_t *handle) |
81 | { |
82 | Player *player = static_cast<Player *>(handle->data); |
83 | bool running = player->Update(); |
84 | if (!running) { |
85 | uv_stop(uv_default_loop()); |
86 | } |
87 | } |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | void IoReactor::NewConnection(uv_stream_t *server) |
94 | { |
95 | uv_tcp_t *client = new uv_tcp_t(); |
96 | uv_tcp_init(uv_default_loop(), client); |
97 | |
98 | if (uv_accept(server, (uv_stream_t *)client) == 0) { |
99 | auto tcp = std::make_shared<TcpResponseSink>(*this, client, |
100 | this->handler); |
101 | this->player.WelcomeClient(*tcp); |
102 | this->connections.insert(tcp); |
103 | client->data = static_cast<void *>(tcp.get()); |
104 | |
105 | uv_read_start((uv_stream_t *)client, UvAlloc, UvReadCallback); |
106 | } else { |
107 | uv_close((uv_handle_t *)client, UvCloseCallback); |
108 | } |
109 | } |
110 | |
111 | void IoReactor::RemoveConnection(TcpResponseSink &conn) |
112 | { |
113 | this->connections.erase(std::make_shared<TcpResponseSink>(conn)); |
114 | } |
115 | |
116 | IoReactor::IoReactor(Player &player, CommandHandler &handler, |
117 | const std::string &address, const std::string &port) |
118 | : player(player), handler(handler) |
119 | { |
120 | InitAcceptor(address, port); |
121 | DoUpdateTimer(); |
122 | } |
123 | |
124 | void IoReactor::Run() |
125 | { |
126 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
127 | } |
128 | |
129 | void IoReactor::DoUpdateTimer() |
130 | { |
131 | uv_timer_init(uv_default_loop(), &this->updater); |
132 | this->updater.data = static_cast<void *>(&this->player); |
133 | uv_timer_start(&this->updater, UvUpdateTimerCallback, 0, |
134 | PLAYER_UPDATE_PERIOD); |
135 | } |
136 | |
137 | void IoReactor::InitAcceptor(const std::string &address, |
138 | const std::string &port) |
139 | { |
140 | int uport = std::stoi(port); |
141 | |
142 | uv_tcp_init(uv_default_loop(), &this->server); |
143 | this->server.data = static_cast<void *>(this); |
144 | |
145 | struct sockaddr_in bind_addr; |
146 | uv_ip4_addr(address.c_str(), uport, &bind_addr); |
147 | uv_tcp_bind(&this->server, (const sockaddr *)&bind_addr, 0); |
148 | |
149 | |
150 | uv_listen((uv_stream_t *)&this->server, 128, UvListenCallback); |
151 | } |
152 | |
153 | void IoReactor::RespondRaw(const std::string &string) const |
154 | { |
155 | for (const auto &conn : this->connections) { |
156 | conn->RespondRaw(string); |
157 | } |
158 | } |
159 | |
160 | void IoReactor::End() |
161 | { |
162 | uv_stop(uv_default_loop()); |
163 | } |
164 | |
165 | |
166 | |
167 | |
168 | |
169 | TcpResponseSink::TcpResponseSink(IoReactor &parent, uv_tcp_t *tcp, |
170 | CommandHandler &handler) |
171 | : parent(parent), tcp(tcp), tokeniser(handler, *this) |
172 | { |
173 | } |
174 | |
175 | void TcpResponseSink::RespondRaw(const std::string &string) const |
176 | { |
177 | unsigned int l = string.length(); |
178 | const char *s = string.c_str(); |
179 | |
180 | WriteReq *req = new WriteReq; |
181 | req->buf = uv_buf_init(new char[l + 1], l + 1); |
182 | memcpy(req->buf.base, s, l); |
183 | req->buf.base[l] = '\n'; |
184 | |
185 | uv_write((uv_write_t *)req, (uv_stream_t *)tcp, &req->buf, 1, |
186 | UvRespondCallback); |
187 | } |
188 | |
189 | void TcpResponseSink::Read(uv_stream_t *stream, ssize_t nread, |
190 | const uv_buf_t *buf) |
191 | { |
192 | if (nread < 0) { |
193 | if (nread == UV_EOF) { |
194 | uv_close((uv_handle_t *)stream, UvCloseCallback); |
195 | } |
196 | |
197 | return; |
198 | } |
199 | |
200 | if (buf->base != nullptr) { |
201 | this->tokeniser.Feed(buf->base, nread); |
202 | delete[] buf->base; |
203 | } |
204 | } |
205 | |
206 | void TcpResponseSink::Close() |
207 | { |
208 | Debug() << "Closing client connection" << std::endl; |
209 | this->parent.RemoveConnection(*this); |
210 | } |