Merge lp:~widelands-dev/widelands/nethost-split into lp:widelands

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
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 :

Cod LGTM, not tested.

review: Approve
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://travis-ci.org/widelands/widelands/builds/295046033.
Appveyor build 2533. State: failed. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_nethost_split-2533.

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

Subscribers

People subscribed via source and target branches

to status/vote changes: