Merge lp:~widelands-dev/widelands/nethost-split into lp:widelands
- nethost-split
- Merge into trunk
Proposed by
Notabilis
Status: | Merged |
---|---|
Merged at revision: | 8469 |
Proposed branch: | lp:~widelands-dev/widelands/nethost-split |
Merge into: | lp:widelands |
Diff against target: |
465 lines (+207/-87) 8 files modified
src/network/CMakeLists.txt (+2/-0) src/network/gamehost.cc (+7/-4) src/network/gamehost.h (+2/-2) src/network/netclient.h (+7/-32) src/network/netclient_interface.h (+73/-0) src/network/nethost.cc (+6/-0) src/network/nethost.h (+17/-49) src/network/nethost_interface.h (+93/-0) |
To merge this branch: | bzr merge lp:~widelands-dev/widelands/nethost-split |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
GunChleoc | Approve | ||
Review via email: mp+332385@code.launchpad.net |
Commit message
Extracting an interface class out of the NetHost/NetClient.
Description of the change
Extracting an interface class out of the NetHost/NetClient.
Useless with only this branch but the network relay branch will make use of it.
To post a comment you must log in.
Revision history for this message
GunChleoc (gunchleoc) wrote : | # |
Tested, good to go :)
@bunnybot merge
Revision history for this message
Notabilis (notabilis27) wrote : | # |
Thanks!
Revision history for this message
GunChleoc (gunchleoc) wrote : | # |
@bunnybot merge
I hope to have time next week to look at your bigger changes.
Revision history for this message
kaputtnik (franku) wrote : | # |
Is bunnybot ill? Another try:
@bunnybot merge
Revision history for this message
bunnybot (widelandsofficial) wrote : | # |
Continuous integration builds have changed state:
Travis build 2721. State: passed. Details: https:/
Appveyor build 2533. State: failed. Details: https:/
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/network/CMakeLists.txt' | |||
2 | --- src/network/CMakeLists.txt 2017-06-26 11:35:50 +0000 | |||
3 | +++ src/network/CMakeLists.txt 2017-10-25 10:15:57 +0000 | |||
4 | @@ -10,8 +10,10 @@ | |||
5 | 10 | gameclient.h | 10 | gameclient.h |
6 | 11 | gamehost.cc | 11 | gamehost.cc |
7 | 12 | gamehost.h | 12 | gamehost.h |
8 | 13 | netclient_interface.h | ||
9 | 13 | netclient.cc | 14 | netclient.cc |
10 | 14 | netclient.h | 15 | netclient.h |
11 | 16 | nethost_interface.h | ||
12 | 15 | nethost.cc | 17 | nethost.cc |
13 | 16 | nethost.h | 18 | nethost.h |
14 | 17 | network.cc | 19 | network.cc |
15 | 18 | 20 | ||
16 | === modified file 'src/network/gamehost.cc' | |||
17 | --- src/network/gamehost.cc 2017-09-20 21:27:25 +0000 | |||
18 | +++ src/network/gamehost.cc 2017-10-25 10:15:57 +0000 | |||
19 | @@ -50,6 +50,7 @@ | |||
20 | 50 | #include "map_io/widelands_map_loader.h" | 50 | #include "map_io/widelands_map_loader.h" |
21 | 51 | #include "network/constants.h" | 51 | #include "network/constants.h" |
22 | 52 | #include "network/internet_gaming.h" | 52 | #include "network/internet_gaming.h" |
23 | 53 | #include "network/nethost.h" | ||
24 | 53 | #include "network/network_gaming_messages.h" | 54 | #include "network/network_gaming_messages.h" |
25 | 54 | #include "network/network_lan_promotion.h" | 55 | #include "network/network_lan_promotion.h" |
26 | 55 | #include "network/network_player_settings_backend.h" | 56 | #include "network/network_player_settings_backend.h" |
27 | @@ -387,7 +388,7 @@ | |||
28 | 387 | }; | 388 | }; |
29 | 388 | 389 | ||
30 | 389 | struct Client { | 390 | struct Client { |
32 | 390 | NetHost::ConnectionId sock_id; | 391 | NetHostInterface::ConnectionId sock_id; |
33 | 391 | uint8_t playernum; | 392 | uint8_t playernum; |
34 | 392 | int16_t usernum; | 393 | int16_t usernum; |
35 | 393 | std::string build_id; | 394 | std::string build_id; |
36 | @@ -411,7 +412,7 @@ | |||
37 | 411 | NetworkPlayerSettingsBackend npsb; | 412 | NetworkPlayerSettingsBackend npsb; |
38 | 412 | 413 | ||
39 | 413 | LanGamePromoter* promoter; | 414 | LanGamePromoter* promoter; |
41 | 414 | std::unique_ptr<NetHost> net; | 415 | std::unique_ptr<NetHostInterface> net; |
42 | 415 | 416 | ||
43 | 416 | /// List of connected clients. Note that clients are not in the same | 417 | /// List of connected clients. Note that clients are not in the same |
44 | 417 | /// order as players. In fact, a client must not be assigned to a player. | 418 | /// order as players. In fact, a client must not be assigned to a player. |
45 | @@ -1420,12 +1421,14 @@ | |||
46 | 1420 | 1421 | ||
47 | 1421 | // Send the packet to all properly connected clients | 1422 | // Send the packet to all properly connected clients |
48 | 1422 | void GameHost::broadcast(SendPacket& packet) { | 1423 | void GameHost::broadcast(SendPacket& packet) { |
49 | 1424 | std::vector<NetHostInterface::ConnectionId> receivers; | ||
50 | 1423 | for (const Client& client : d->clients) { | 1425 | for (const Client& client : d->clients) { |
51 | 1424 | if (client.playernum != UserSettings::not_connected()) { | 1426 | if (client.playernum != UserSettings::not_connected()) { |
52 | 1425 | assert(client.sock_id > 0); | 1427 | assert(client.sock_id > 0); |
54 | 1426 | d->net->send(client.sock_id, packet); | 1428 | receivers.push_back(client.sock_id); |
55 | 1427 | } | 1429 | } |
56 | 1428 | } | 1430 | } |
57 | 1431 | d->net->send(receivers, packet); | ||
58 | 1429 | } | 1432 | } |
59 | 1430 | 1433 | ||
60 | 1431 | void GameHost::write_setting_map(SendPacket& packet) { | 1434 | void GameHost::write_setting_map(SendPacket& packet) { |
61 | @@ -2195,7 +2198,7 @@ | |||
62 | 2195 | } | 2198 | } |
63 | 2196 | } | 2199 | } |
64 | 2197 | 2200 | ||
66 | 2198 | void GameHost::send_file_part(NetHost::ConnectionId csock_id, uint32_t part) { | 2201 | void GameHost::send_file_part(NetHostInterface::ConnectionId csock_id, uint32_t part) { |
67 | 2199 | assert(part < file_->parts.size()); | 2202 | assert(part < file_->parts.size()); |
68 | 2200 | 2203 | ||
69 | 2201 | uint32_t left = file_->bytes - NETFILEPARTSIZE * part; | 2204 | uint32_t left = file_->bytes - NETFILEPARTSIZE * part; |
70 | 2202 | 2205 | ||
71 | === modified file 'src/network/gamehost.h' | |||
72 | --- src/network/gamehost.h 2017-08-11 15:30:42 +0000 | |||
73 | +++ src/network/gamehost.h 2017-10-25 10:15:57 +0000 | |||
74 | @@ -24,7 +24,7 @@ | |||
75 | 24 | #include "logic/game_settings.h" | 24 | #include "logic/game_settings.h" |
76 | 25 | #include "logic/player_end_result.h" | 25 | #include "logic/player_end_result.h" |
77 | 26 | #include "logic/widelands.h" | 26 | #include "logic/widelands.h" |
79 | 27 | #include "network/nethost.h" | 27 | #include "network/nethost_interface.h" |
80 | 28 | #include "network/network.h" | 28 | #include "network/network.h" |
81 | 29 | 29 | ||
82 | 30 | struct ChatMessage; | 30 | struct ChatMessage; |
83 | @@ -127,7 +127,7 @@ | |||
84 | 127 | 127 | ||
85 | 128 | void handle_packet(uint32_t i, RecvPacket&); | 128 | void handle_packet(uint32_t i, RecvPacket&); |
86 | 129 | void handle_network(); | 129 | void handle_network(); |
88 | 130 | void send_file_part(NetHost::ConnectionId client_sock_id, uint32_t part); | 130 | void send_file_part(NetHostInterface::ConnectionId client_sock_id, uint32_t part); |
89 | 131 | 131 | ||
90 | 132 | void check_hung_clients(); | 132 | void check_hung_clients(); |
91 | 133 | void broadcast_real_speed(uint32_t speed); | 133 | void broadcast_real_speed(uint32_t speed); |
92 | 134 | 134 | ||
93 | === modified file 'src/network/netclient.h' | |||
94 | --- src/network/netclient.h 2017-07-01 08:22:54 +0000 | |||
95 | +++ src/network/netclient.h 2017-10-25 10:15:57 +0000 | |||
96 | @@ -22,6 +22,7 @@ | |||
97 | 22 | 22 | ||
98 | 23 | #include <memory> | 23 | #include <memory> |
99 | 24 | 24 | ||
100 | 25 | #include "network/netclient_interface.h" | ||
101 | 25 | #include "network/network.h" | 26 | #include "network/network.h" |
102 | 26 | 27 | ||
103 | 27 | /** | 28 | /** |
104 | @@ -30,7 +31,7 @@ | |||
105 | 30 | * This class only tries to create a single socket, either for IPv4 and IPv6. | 31 | * This class only tries to create a single socket, either for IPv4 and IPv6. |
106 | 31 | * Which is used depends on what kind of address is given on call to connect(). | 32 | * Which is used depends on what kind of address is given on call to connect(). |
107 | 32 | */ | 33 | */ |
109 | 33 | class NetClient { | 34 | class NetClient : public NetClientInterface { |
110 | 34 | public: | 35 | public: |
111 | 35 | /** | 36 | /** |
112 | 36 | * Tries to establish a connection to the given host. | 37 | * Tries to establish a connection to the given host. |
113 | @@ -39,10 +40,6 @@ | |||
114 | 39 | */ | 40 | */ |
115 | 40 | static std::unique_ptr<NetClient> connect(const NetAddress& host); | 41 | static std::unique_ptr<NetClient> connect(const NetAddress& host); |
116 | 41 | 42 | ||
117 | 42 | /** | ||
118 | 43 | * Closes the connection. | ||
119 | 44 | * If you want to send a goodbye-message to the host, do so before freeing the object. | ||
120 | 45 | */ | ||
121 | 46 | ~NetClient(); | 43 | ~NetClient(); |
122 | 47 | 44 | ||
123 | 48 | /** | 45 | /** |
124 | @@ -53,33 +50,11 @@ | |||
125 | 53 | */ | 50 | */ |
126 | 54 | bool get_remote_address(NetAddress* addr) const; | 51 | bool get_remote_address(NetAddress* addr) const; |
127 | 55 | 52 | ||
155 | 56 | /** | 53 | // Inherited from NetClientInterface |
156 | 57 | * Returns whether the client is connected. | 54 | bool is_connected() const override; |
157 | 58 | * \return \c true if the connection is open, \c false otherwise. | 55 | void close() override; |
158 | 59 | */ | 56 | bool try_receive(RecvPacket* packet) override; |
159 | 60 | bool is_connected() const; | 57 | void send(const SendPacket& packet) override; |
133 | 61 | |||
134 | 62 | /** | ||
135 | 63 | * Closes the connection. | ||
136 | 64 | * If you want to send a goodbye-message to the host, do so before calling this. | ||
137 | 65 | */ | ||
138 | 66 | void close(); | ||
139 | 67 | |||
140 | 68 | /** | ||
141 | 69 | * Tries to receive a packet. | ||
142 | 70 | * \param packet A packet that should be overwritten with the received data. | ||
143 | 71 | * \return \c true if a packet is available, \c false otherwise. | ||
144 | 72 | * The given packet is only modified when \c true is returned. | ||
145 | 73 | * Calling this on a closed connection will return false. | ||
146 | 74 | */ | ||
147 | 75 | bool try_receive(RecvPacket* packet); | ||
148 | 76 | |||
149 | 77 | /** | ||
150 | 78 | * Sends a packet. | ||
151 | 79 | * Calling this on a closed connection will silently fail. | ||
152 | 80 | * \param packet The packet to send. | ||
153 | 81 | */ | ||
154 | 82 | void send(const SendPacket& packet); | ||
160 | 83 | 58 | ||
161 | 84 | private: | 59 | private: |
162 | 85 | /** | 60 | /** |
163 | 86 | 61 | ||
164 | === added file 'src/network/netclient_interface.h' | |||
165 | --- src/network/netclient_interface.h 1970-01-01 00:00:00 +0000 | |||
166 | +++ src/network/netclient_interface.h 2017-10-25 10:15:57 +0000 | |||
167 | @@ -0,0 +1,73 @@ | |||
168 | 1 | /* | ||
169 | 2 | * Copyright (C) 2008-2017 by the Widelands Development Team | ||
170 | 3 | * | ||
171 | 4 | * This program is free software; you can redistribute it and/or | ||
172 | 5 | * modify it under the terms of the GNU General Public License | ||
173 | 6 | * as published by the Free Software Foundation; either version 2 | ||
174 | 7 | * of the License, or (at your option) any later version. | ||
175 | 8 | * | ||
176 | 9 | * This program is distributed in the hope that it will be useful, | ||
177 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
178 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
179 | 12 | * GNU General Public License for more details. | ||
180 | 13 | * | ||
181 | 14 | * You should have received a copy of the GNU General Public License | ||
182 | 15 | * along with this program; if not, write to the Free Software | ||
183 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
184 | 17 | * | ||
185 | 18 | */ | ||
186 | 19 | |||
187 | 20 | #ifndef WL_NETWORK_NETCLIENT_INTERFACE_H | ||
188 | 21 | #define WL_NETWORK_NETCLIENT_INTERFACE_H | ||
189 | 22 | |||
190 | 23 | #include <memory> | ||
191 | 24 | |||
192 | 25 | #include "network/network.h" | ||
193 | 26 | |||
194 | 27 | /** | ||
195 | 28 | * NetClient manages the network connection for a network game in which this computer | ||
196 | 29 | * participates as a client. | ||
197 | 30 | * This class provides the interface all NetClient implementation have to follow. | ||
198 | 31 | * Currently two implementations exists: A "real" NetClient for local games and a | ||
199 | 32 | * NetClientProxy which relays commands over a relay server. | ||
200 | 33 | */ | ||
201 | 34 | class NetClientInterface { | ||
202 | 35 | public: | ||
203 | 36 | |||
204 | 37 | /** | ||
205 | 38 | * Closes the connection. | ||
206 | 39 | * If you want to send a goodbye-message to the host, do so before freeing the object. | ||
207 | 40 | */ | ||
208 | 41 | virtual ~NetClientInterface() { | ||
209 | 42 | } | ||
210 | 43 | |||
211 | 44 | /** | ||
212 | 45 | * Returns whether the client is connected. | ||
213 | 46 | * \return \c true if the connection is open, \c false otherwise. | ||
214 | 47 | */ | ||
215 | 48 | virtual bool is_connected() const = 0; | ||
216 | 49 | |||
217 | 50 | /** | ||
218 | 51 | * Closes the connection. | ||
219 | 52 | * If you want to send a goodbye-message to the host, do so before calling this. | ||
220 | 53 | */ | ||
221 | 54 | virtual void close() = 0; | ||
222 | 55 | |||
223 | 56 | /** | ||
224 | 57 | * Tries to receive a packet. | ||
225 | 58 | * \param packet A packet that should be overwritten with the received data. | ||
226 | 59 | * \return \c true if a packet is available, \c false otherwise. | ||
227 | 60 | * The given packet is only modified when \c true is returned. | ||
228 | 61 | * Calling this on a closed connection will return false. | ||
229 | 62 | */ | ||
230 | 63 | virtual bool try_receive(RecvPacket* packet) = 0; | ||
231 | 64 | |||
232 | 65 | /** | ||
233 | 66 | * Sends a packet. | ||
234 | 67 | * Calling this on a closed connection will silently fail. | ||
235 | 68 | * \param packet The packet to send. | ||
236 | 69 | */ | ||
237 | 70 | virtual void send(const SendPacket& packet) = 0; | ||
238 | 71 | }; | ||
239 | 72 | |||
240 | 73 | #endif // end of include guard: WL_NETWORK_NETCLIENT_INTERFACE_H | ||
241 | 0 | 74 | ||
242 | === modified file 'src/network/nethost.cc' | |||
243 | --- src/network/nethost.cc 2017-06-19 18:32:06 +0000 | |||
244 | +++ src/network/nethost.cc 2017-10-25 10:15:57 +0000 | |||
245 | @@ -191,6 +191,12 @@ | |||
246 | 191 | } | 191 | } |
247 | 192 | } | 192 | } |
248 | 193 | 193 | ||
249 | 194 | void NetHost::send(const std::vector<ConnectionId>& ids, const SendPacket& packet) { | ||
250 | 195 | for (ConnectionId id : ids) { | ||
251 | 196 | send(id, packet); | ||
252 | 197 | } | ||
253 | 198 | } | ||
254 | 199 | |||
255 | 194 | NetHost::NetHost(const uint16_t port) | 200 | NetHost::NetHost(const uint16_t port) |
256 | 195 | : clients_(), next_id_(1), io_service_(), acceptor_v4_(io_service_), acceptor_v6_(io_service_) { | 201 | : clients_(), next_id_(1), io_service_(), acceptor_v4_(io_service_), acceptor_v6_(io_service_) { |
257 | 196 | 202 | ||
258 | 197 | 203 | ||
259 | === modified file 'src/network/nethost.h' | |||
260 | --- src/network/nethost.h 2017-06-24 13:44:38 +0000 | |||
261 | +++ src/network/nethost.h 2017-10-25 10:15:57 +0000 | |||
262 | @@ -23,17 +23,15 @@ | |||
263 | 23 | #include <map> | 23 | #include <map> |
264 | 24 | #include <memory> | 24 | #include <memory> |
265 | 25 | 25 | ||
267 | 26 | #include "network/network.h" | 26 | #include "network/nethost_interface.h" |
268 | 27 | 27 | ||
269 | 28 | /** | 28 | /** |
270 | 29 | * NetHost manages the client connections of a network game in which this computer | 29 | * NetHost manages the client connections of a network game in which this computer |
271 | 30 | * participates as a server. | 30 | * participates as a server. |
273 | 31 | * This class tries to create sockets for IPv4 and IPv6. | 31 | * This class tries to create sockets for IPv4 and IPv6 for gaming in the local network. |
274 | 32 | */ | 32 | */ |
276 | 33 | class NetHost { | 33 | class NetHost : public NetHostInterface { |
277 | 34 | public: | 34 | public: |
278 | 35 | /// IDs used to enumerate the clients. | ||
279 | 36 | using ConnectionId = uint32_t; | ||
280 | 37 | 35 | ||
281 | 38 | /** | 36 | /** |
282 | 39 | * Tries to listen on the given port. | 37 | * Tries to listen on the given port. |
283 | @@ -47,60 +45,30 @@ | |||
284 | 47 | */ | 45 | */ |
285 | 48 | ~NetHost(); | 46 | ~NetHost(); |
286 | 49 | 47 | ||
287 | 48 | // Inherited from NetHostInterface | ||
288 | 49 | bool is_connected(ConnectionId id) const override; | ||
289 | 50 | void close(ConnectionId id) override; | ||
290 | 51 | bool try_accept(ConnectionId* new_id) override; | ||
291 | 52 | bool try_receive(ConnectionId id, RecvPacket* packet) override; | ||
292 | 53 | void send(ConnectionId id, const SendPacket& packet) override; | ||
293 | 54 | void send(const std::vector<ConnectionId>& ids, const SendPacket& packet) override; | ||
294 | 55 | |||
295 | 56 | private: | ||
296 | 57 | |||
297 | 50 | /** | 58 | /** |
298 | 51 | * Returns whether the server is started and is listening. | 59 | * Returns whether the server is started and is listening. |
299 | 52 | * \return \c true if the server is listening, \c false otherwise. | 60 | * \return \c true if the server is listening, \c false otherwise. |
300 | 53 | */ | 61 | */ |
301 | 62 | // Feel free to make this method public if you need it | ||
302 | 54 | bool is_listening() const; | 63 | bool is_listening() const; |
303 | 55 | 64 | ||
304 | 56 | /** | 65 | /** |
305 | 57 | * Returns whether the given client is connected. | ||
306 | 58 | * \param The id of the client to check. | ||
307 | 59 | * \return \c true if the connection is open, \c false otherwise. | ||
308 | 60 | */ | ||
309 | 61 | bool is_connected(ConnectionId id) const; | ||
310 | 62 | |||
311 | 63 | /** | ||
312 | 64 | * Stops listening for connections. | 66 | * Stops listening for connections. |
313 | 65 | */ | 67 | */ |
314 | 68 | // Feel free to make this method public if you need it | ||
315 | 66 | void stop_listening(); | 69 | void stop_listening(); |
316 | 67 | 70 | ||
317 | 68 | /** | 71 | /** |
318 | 69 | * Closes the connection to the given client. | ||
319 | 70 | * \param id The id of the client to close the connection to. | ||
320 | 71 | */ | ||
321 | 72 | void close(ConnectionId id); | ||
322 | 73 | |||
323 | 74 | /** | ||
324 | 75 | * Tries to accept a new client. | ||
325 | 76 | * \param new_id The connection id of the new client will be stored here. | ||
326 | 77 | * \return \c true if a client has connected, \c false otherwise. | ||
327 | 78 | * The given id is only modified when \c true is returned. | ||
328 | 79 | * Calling this on a closed server will return false. | ||
329 | 80 | * The returned id is always greater than 0. | ||
330 | 81 | */ | ||
331 | 82 | bool try_accept(ConnectionId* new_id); | ||
332 | 83 | |||
333 | 84 | /** | ||
334 | 85 | * Tries to receive a packet. | ||
335 | 86 | * \param id The connection id of the client that should be received. | ||
336 | 87 | * \param packet A packet that should be overwritten with the received data. | ||
337 | 88 | * \return \c true if a packet is available, \c false otherwise. | ||
338 | 89 | * The given packet is only modified when \c true is returned. | ||
339 | 90 | * Calling this on a closed connection will return false. | ||
340 | 91 | */ | ||
341 | 92 | bool try_receive(ConnectionId id, RecvPacket* packet); | ||
342 | 93 | |||
343 | 94 | /** | ||
344 | 95 | * Sends a packet. | ||
345 | 96 | * Calling this on a closed connection will silently fail. | ||
346 | 97 | * \param id The connection id of the client that should be sent to. | ||
347 | 98 | * \param packet The packet to send. | ||
348 | 99 | */ | ||
349 | 100 | void send(ConnectionId id, const SendPacket& packet); | ||
350 | 101 | |||
351 | 102 | private: | ||
352 | 103 | /** | ||
353 | 104 | * Tries to listen on the given port. | 72 | * Tries to listen on the given port. |
354 | 105 | * If it fails, is_listening() will return \c false. | 73 | * If it fails, is_listening() will return \c false. |
355 | 106 | * \param port The port to listen on. | 74 | * \param port The port to listen on. |
356 | @@ -129,9 +97,9 @@ | |||
357 | 129 | 97 | ||
358 | 130 | /// A map linking client ids to the respective data about the clients. | 98 | /// A map linking client ids to the respective data about the clients. |
359 | 131 | /// Client ids not in this map should be considered invalid. | 99 | /// Client ids not in this map should be considered invalid. |
361 | 132 | std::map<NetHost::ConnectionId, Client> clients_; | 100 | std::map<NetHostInterface::ConnectionId, Client> clients_; |
362 | 133 | /// The next client id that will be used | 101 | /// The next client id that will be used |
364 | 134 | NetHost::ConnectionId next_id_; | 102 | NetHostInterface::ConnectionId next_id_; |
365 | 135 | /// An io_service needed by boost.asio. Primary needed for async operations. | 103 | /// An io_service needed by boost.asio. Primary needed for async operations. |
366 | 136 | boost::asio::io_service io_service_; | 104 | boost::asio::io_service io_service_; |
367 | 137 | /// The acceptor we get IPv4 connection requests to. | 105 | /// The acceptor we get IPv4 connection requests to. |
368 | 138 | 106 | ||
369 | === added file 'src/network/nethost_interface.h' | |||
370 | --- src/network/nethost_interface.h 1970-01-01 00:00:00 +0000 | |||
371 | +++ src/network/nethost_interface.h 2017-10-25 10:15:57 +0000 | |||
372 | @@ -0,0 +1,93 @@ | |||
373 | 1 | /* | ||
374 | 2 | * Copyright (C) 2008-2017 by the Widelands Development Team | ||
375 | 3 | * | ||
376 | 4 | * This program is free software; you can redistribute it and/or | ||
377 | 5 | * modify it under the terms of the GNU General Public License | ||
378 | 6 | * as published by the Free Software Foundation; either version 2 | ||
379 | 7 | * of the License, or (at your option) any later version. | ||
380 | 8 | * | ||
381 | 9 | * This program is distributed in the hope that it will be useful, | ||
382 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
383 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
384 | 12 | * GNU General Public License for more details. | ||
385 | 13 | * | ||
386 | 14 | * You should have received a copy of the GNU General Public License | ||
387 | 15 | * along with this program; if not, write to the Free Software | ||
388 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
389 | 17 | * | ||
390 | 18 | */ | ||
391 | 19 | |||
392 | 20 | #ifndef WL_NETWORK_NETHOST_INTERFACE_H | ||
393 | 21 | #define WL_NETWORK_NETHOST_INTERFACE_H | ||
394 | 22 | |||
395 | 23 | #include "network/network.h" | ||
396 | 24 | |||
397 | 25 | /** | ||
398 | 26 | * A NetHost manages the client connections of a network game in | ||
399 | 27 | * which this computer participates as a server. | ||
400 | 28 | * This class provides the interface all NetHost implementation have to follow. | ||
401 | 29 | * Currently two implementations exists: A "real" NetHost for local games and a | ||
402 | 30 | * NetHostProxy which relays commands over a relay server. | ||
403 | 31 | */ | ||
404 | 32 | class NetHostInterface { | ||
405 | 33 | public: | ||
406 | 34 | /// IDs used to enumerate the clients. | ||
407 | 35 | using ConnectionId = uint8_t; | ||
408 | 36 | |||
409 | 37 | /** | ||
410 | 38 | * Closes the server. | ||
411 | 39 | */ | ||
412 | 40 | virtual ~NetHostInterface() { | ||
413 | 41 | } | ||
414 | 42 | |||
415 | 43 | /** | ||
416 | 44 | * Returns whether the given client is connected. | ||
417 | 45 | * \param The id of the client to check. | ||
418 | 46 | * \return \c true if the connection is open, \c false otherwise. | ||
419 | 47 | */ | ||
420 | 48 | virtual bool is_connected(ConnectionId id) const = 0; | ||
421 | 49 | |||
422 | 50 | /** | ||
423 | 51 | * Closes the connection to the given client. | ||
424 | 52 | * \param id The id of the client to close the connection to. | ||
425 | 53 | */ | ||
426 | 54 | virtual void close(ConnectionId id) = 0; | ||
427 | 55 | |||
428 | 56 | /** | ||
429 | 57 | * Tries to accept a new client. | ||
430 | 58 | * \param new_id The connection id of the new client will be stored here. | ||
431 | 59 | * \return \c true if a client has connected, \c false otherwise. | ||
432 | 60 | * The given id is only modified when \c true is returned. | ||
433 | 61 | * Calling this on a closed server will return false. | ||
434 | 62 | * The returned id is always greater than 0. | ||
435 | 63 | */ | ||
436 | 64 | virtual bool try_accept(ConnectionId* new_id) = 0; | ||
437 | 65 | |||
438 | 66 | /** | ||
439 | 67 | * Tries to receive a packet. | ||
440 | 68 | * \param id The connection id of the client that should be received. | ||
441 | 69 | * \param packet A packet that should be overwritten with the received data. | ||
442 | 70 | * \return \c true if a packet is available, \c false otherwise. | ||
443 | 71 | * The given packet is only modified when \c true is returned. | ||
444 | 72 | * Calling this on a closed connection will return false. | ||
445 | 73 | */ | ||
446 | 74 | virtual bool try_receive(ConnectionId id, RecvPacket* packet) = 0; | ||
447 | 75 | |||
448 | 76 | /** | ||
449 | 77 | * Sends a packet. | ||
450 | 78 | * Calling this on a closed connection will silently fail. | ||
451 | 79 | * \param id The connection id of the client that should be sent to. | ||
452 | 80 | * \param packet The packet to send. | ||
453 | 81 | */ | ||
454 | 82 | virtual void send(ConnectionId id, const SendPacket& packet) = 0; | ||
455 | 83 | |||
456 | 84 | /** | ||
457 | 85 | * Sends a packet to a group of clients. | ||
458 | 86 | * Calling this on a closed connection will silently fail. | ||
459 | 87 | * \param ids The connection ids of the clients that should be sent to. | ||
460 | 88 | * \param packet The packet to send. | ||
461 | 89 | */ | ||
462 | 90 | virtual void send(const std::vector<ConnectionId>& ids, const SendPacket& packet) = 0; | ||
463 | 91 | }; | ||
464 | 92 | |||
465 | 93 | #endif // end of include guard: WL_NETWORK_NETHOST_INTERFACE_H |
Cod LGTM, not tested.