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
=== modified file 'src/network/CMakeLists.txt'
--- src/network/CMakeLists.txt 2017-06-26 11:35:50 +0000
+++ src/network/CMakeLists.txt 2017-10-25 10:15:57 +0000
@@ -10,8 +10,10 @@
10 gameclient.h10 gameclient.h
11 gamehost.cc11 gamehost.cc
12 gamehost.h12 gamehost.h
13 netclient_interface.h
13 netclient.cc14 netclient.cc
14 netclient.h15 netclient.h
16 nethost_interface.h
15 nethost.cc17 nethost.cc
16 nethost.h18 nethost.h
17 network.cc19 network.cc
1820
=== modified file 'src/network/gamehost.cc'
--- src/network/gamehost.cc 2017-09-20 21:27:25 +0000
+++ src/network/gamehost.cc 2017-10-25 10:15:57 +0000
@@ -50,6 +50,7 @@
50#include "map_io/widelands_map_loader.h"50#include "map_io/widelands_map_loader.h"
51#include "network/constants.h"51#include "network/constants.h"
52#include "network/internet_gaming.h"52#include "network/internet_gaming.h"
53#include "network/nethost.h"
53#include "network/network_gaming_messages.h"54#include "network/network_gaming_messages.h"
54#include "network/network_lan_promotion.h"55#include "network/network_lan_promotion.h"
55#include "network/network_player_settings_backend.h"56#include "network/network_player_settings_backend.h"
@@ -387,7 +388,7 @@
387};388};
388389
389struct Client {390struct Client {
390 NetHost::ConnectionId sock_id;391 NetHostInterface::ConnectionId sock_id;
391 uint8_t playernum;392 uint8_t playernum;
392 int16_t usernum;393 int16_t usernum;
393 std::string build_id;394 std::string build_id;
@@ -411,7 +412,7 @@
411 NetworkPlayerSettingsBackend npsb;412 NetworkPlayerSettingsBackend npsb;
412413
413 LanGamePromoter* promoter;414 LanGamePromoter* promoter;
414 std::unique_ptr<NetHost> net;415 std::unique_ptr<NetHostInterface> net;
415416
416 /// List of connected clients. Note that clients are not in the same417 /// List of connected clients. Note that clients are not in the same
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.
@@ -1420,12 +1421,14 @@
14201421
1421// Send the packet to all properly connected clients1422// Send the packet to all properly connected clients
1422void GameHost::broadcast(SendPacket& packet) {1423void GameHost::broadcast(SendPacket& packet) {
1424 std::vector<NetHostInterface::ConnectionId> receivers;
1423 for (const Client& client : d->clients) {1425 for (const Client& client : d->clients) {
1424 if (client.playernum != UserSettings::not_connected()) {1426 if (client.playernum != UserSettings::not_connected()) {
1425 assert(client.sock_id > 0);1427 assert(client.sock_id > 0);
1426 d->net->send(client.sock_id, packet);1428 receivers.push_back(client.sock_id);
1427 }1429 }
1428 }1430 }
1431 d->net->send(receivers, packet);
1429}1432}
14301433
1431void GameHost::write_setting_map(SendPacket& packet) {1434void GameHost::write_setting_map(SendPacket& packet) {
@@ -2195,7 +2198,7 @@
2195 }2198 }
2196}2199}
21972200
2198void GameHost::send_file_part(NetHost::ConnectionId csock_id, uint32_t part) {2201void GameHost::send_file_part(NetHostInterface::ConnectionId csock_id, uint32_t part) {
2199 assert(part < file_->parts.size());2202 assert(part < file_->parts.size());
22002203
2201 uint32_t left = file_->bytes - NETFILEPARTSIZE * part;2204 uint32_t left = file_->bytes - NETFILEPARTSIZE * part;
22022205
=== modified file 'src/network/gamehost.h'
--- src/network/gamehost.h 2017-08-11 15:30:42 +0000
+++ src/network/gamehost.h 2017-10-25 10:15:57 +0000
@@ -24,7 +24,7 @@
24#include "logic/game_settings.h"24#include "logic/game_settings.h"
25#include "logic/player_end_result.h"25#include "logic/player_end_result.h"
26#include "logic/widelands.h"26#include "logic/widelands.h"
27#include "network/nethost.h"27#include "network/nethost_interface.h"
28#include "network/network.h"28#include "network/network.h"
2929
30struct ChatMessage;30struct ChatMessage;
@@ -127,7 +127,7 @@
127127
128 void handle_packet(uint32_t i, RecvPacket&);128 void handle_packet(uint32_t i, RecvPacket&);
129 void handle_network();129 void handle_network();
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);
131131
132 void check_hung_clients();132 void check_hung_clients();
133 void broadcast_real_speed(uint32_t speed);133 void broadcast_real_speed(uint32_t speed);
134134
=== modified file 'src/network/netclient.h'
--- src/network/netclient.h 2017-07-01 08:22:54 +0000
+++ src/network/netclient.h 2017-10-25 10:15:57 +0000
@@ -22,6 +22,7 @@
2222
23#include <memory>23#include <memory>
2424
25#include "network/netclient_interface.h"
25#include "network/network.h"26#include "network/network.h"
2627
27/**28/**
@@ -30,7 +31,7 @@
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.
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().
32 */33 */
33class NetClient {34class NetClient : public NetClientInterface {
34public:35public:
35 /**36 /**
36 * Tries to establish a connection to the given host.37 * Tries to establish a connection to the given host.
@@ -39,10 +40,6 @@
39 */40 */
40 static std::unique_ptr<NetClient> connect(const NetAddress& host);41 static std::unique_ptr<NetClient> connect(const NetAddress& host);
4142
42 /**
43 * Closes the connection.
44 * If you want to send a goodbye-message to the host, do so before freeing the object.
45 */
46 ~NetClient();43 ~NetClient();
4744
48 /**45 /**
@@ -53,33 +50,11 @@
53 */50 */
54 bool get_remote_address(NetAddress* addr) const;51 bool get_remote_address(NetAddress* addr) const;
5552
56 /**53 // Inherited from NetClientInterface
57 * Returns whether the client is connected.54 bool is_connected() const override;
58 * \return \c true if the connection is open, \c false otherwise.55 void close() override;
59 */56 bool try_receive(RecvPacket* packet) override;
60 bool is_connected() const;57 void send(const SendPacket& packet) override;
61
62 /**
63 * Closes the connection.
64 * If you want to send a goodbye-message to the host, do so before calling this.
65 */
66 void close();
67
68 /**
69 * Tries to receive a packet.
70 * \param packet A packet that should be overwritten with the received data.
71 * \return \c true if a packet is available, \c false otherwise.
72 * The given packet is only modified when \c true is returned.
73 * Calling this on a closed connection will return false.
74 */
75 bool try_receive(RecvPacket* packet);
76
77 /**
78 * Sends a packet.
79 * Calling this on a closed connection will silently fail.
80 * \param packet The packet to send.
81 */
82 void send(const SendPacket& packet);
8358
84private:59private:
85 /**60 /**
8661
=== added file 'src/network/netclient_interface.h'
--- src/network/netclient_interface.h 1970-01-01 00:00:00 +0000
+++ src/network/netclient_interface.h 2017-10-25 10:15:57 +0000
@@ -0,0 +1,73 @@
1/*
2 * Copyright (C) 2008-2017 by the Widelands Development Team
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20#ifndef WL_NETWORK_NETCLIENT_INTERFACE_H
21#define WL_NETWORK_NETCLIENT_INTERFACE_H
22
23#include <memory>
24
25#include "network/network.h"
26
27/**
28 * NetClient manages the network connection for a network game in which this computer
29 * participates as a client.
30 * This class provides the interface all NetClient implementation have to follow.
31 * Currently two implementations exists: A "real" NetClient for local games and a
32 * NetClientProxy which relays commands over a relay server.
33 */
34class NetClientInterface {
35public:
36
37 /**
38 * Closes the connection.
39 * If you want to send a goodbye-message to the host, do so before freeing the object.
40 */
41 virtual ~NetClientInterface() {
42 }
43
44 /**
45 * Returns whether the client is connected.
46 * \return \c true if the connection is open, \c false otherwise.
47 */
48 virtual bool is_connected() const = 0;
49
50 /**
51 * Closes the connection.
52 * If you want to send a goodbye-message to the host, do so before calling this.
53 */
54 virtual void close() = 0;
55
56 /**
57 * Tries to receive a packet.
58 * \param packet A packet that should be overwritten with the received data.
59 * \return \c true if a packet is available, \c false otherwise.
60 * The given packet is only modified when \c true is returned.
61 * Calling this on a closed connection will return false.
62 */
63 virtual bool try_receive(RecvPacket* packet) = 0;
64
65 /**
66 * Sends a packet.
67 * Calling this on a closed connection will silently fail.
68 * \param packet The packet to send.
69 */
70 virtual void send(const SendPacket& packet) = 0;
71};
72
73#endif // end of include guard: WL_NETWORK_NETCLIENT_INTERFACE_H
074
=== modified file 'src/network/nethost.cc'
--- src/network/nethost.cc 2017-06-19 18:32:06 +0000
+++ src/network/nethost.cc 2017-10-25 10:15:57 +0000
@@ -191,6 +191,12 @@
191 }191 }
192}192}
193193
194void NetHost::send(const std::vector<ConnectionId>& ids, const SendPacket& packet) {
195 for (ConnectionId id : ids) {
196 send(id, packet);
197 }
198}
199
194NetHost::NetHost(const uint16_t port)200NetHost::NetHost(const uint16_t port)
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_) {
196202
197203
=== modified file 'src/network/nethost.h'
--- src/network/nethost.h 2017-06-24 13:44:38 +0000
+++ src/network/nethost.h 2017-10-25 10:15:57 +0000
@@ -23,17 +23,15 @@
23#include <map>23#include <map>
24#include <memory>24#include <memory>
2525
26#include "network/network.h"26#include "network/nethost_interface.h"
2727
28/**28/**
29 * NetHost manages the client connections of a network game in which this computer29 * NetHost manages the client connections of a network game in which this computer
30 * participates as a server.30 * participates as a server.
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.
32 */32 */
33class NetHost {33class NetHost : public NetHostInterface {
34public:34public:
35 /// IDs used to enumerate the clients.
36 using ConnectionId = uint32_t;
3735
38 /**36 /**
39 * Tries to listen on the given port.37 * Tries to listen on the given port.
@@ -47,60 +45,30 @@
47 */45 */
48 ~NetHost();46 ~NetHost();
4947
48 // Inherited from NetHostInterface
49 bool is_connected(ConnectionId id) const override;
50 void close(ConnectionId id) override;
51 bool try_accept(ConnectionId* new_id) override;
52 bool try_receive(ConnectionId id, RecvPacket* packet) override;
53 void send(ConnectionId id, const SendPacket& packet) override;
54 void send(const std::vector<ConnectionId>& ids, const SendPacket& packet) override;
55
56private:
57
50 /**58 /**
51 * Returns whether the server is started and is listening.59 * Returns whether the server is started and is listening.
52 * \return \c true if the server is listening, \c false otherwise.60 * \return \c true if the server is listening, \c false otherwise.
53 */61 */
62 // Feel free to make this method public if you need it
54 bool is_listening() const;63 bool is_listening() const;
5564
56 /**65 /**
57 * Returns whether the given client is connected.
58 * \param The id of the client to check.
59 * \return \c true if the connection is open, \c false otherwise.
60 */
61 bool is_connected(ConnectionId id) const;
62
63 /**
64 * Stops listening for connections.66 * Stops listening for connections.
65 */67 */
68 // Feel free to make this method public if you need it
66 void stop_listening();69 void stop_listening();
6770
68 /**71 /**
69 * Closes the connection to the given client.
70 * \param id The id of the client to close the connection to.
71 */
72 void close(ConnectionId id);
73
74 /**
75 * Tries to accept a new client.
76 * \param new_id The connection id of the new client will be stored here.
77 * \return \c true if a client has connected, \c false otherwise.
78 * The given id is only modified when \c true is returned.
79 * Calling this on a closed server will return false.
80 * The returned id is always greater than 0.
81 */
82 bool try_accept(ConnectionId* new_id);
83
84 /**
85 * Tries to receive a packet.
86 * \param id The connection id of the client that should be received.
87 * \param packet A packet that should be overwritten with the received data.
88 * \return \c true if a packet is available, \c false otherwise.
89 * The given packet is only modified when \c true is returned.
90 * Calling this on a closed connection will return false.
91 */
92 bool try_receive(ConnectionId id, RecvPacket* packet);
93
94 /**
95 * Sends a packet.
96 * Calling this on a closed connection will silently fail.
97 * \param id The connection id of the client that should be sent to.
98 * \param packet The packet to send.
99 */
100 void send(ConnectionId id, const SendPacket& packet);
101
102private:
103 /**
104 * Tries to listen on the given port.72 * Tries to listen on the given port.
105 * If it fails, is_listening() will return \c false.73 * If it fails, is_listening() will return \c false.
106 * \param port The port to listen on.74 * \param port The port to listen on.
@@ -129,9 +97,9 @@
12997
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.
131 /// Client ids not in this map should be considered invalid.99 /// Client ids not in this map should be considered invalid.
132 std::map<NetHost::ConnectionId, Client> clients_;100 std::map<NetHostInterface::ConnectionId, Client> clients_;
133 /// The next client id that will be used101 /// The next client id that will be used
134 NetHost::ConnectionId next_id_;102 NetHostInterface::ConnectionId next_id_;
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.
136 boost::asio::io_service io_service_;104 boost::asio::io_service io_service_;
137 /// The acceptor we get IPv4 connection requests to.105 /// The acceptor we get IPv4 connection requests to.
138106
=== added file 'src/network/nethost_interface.h'
--- src/network/nethost_interface.h 1970-01-01 00:00:00 +0000
+++ src/network/nethost_interface.h 2017-10-25 10:15:57 +0000
@@ -0,0 +1,93 @@
1/*
2 * Copyright (C) 2008-2017 by the Widelands Development Team
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20#ifndef WL_NETWORK_NETHOST_INTERFACE_H
21#define WL_NETWORK_NETHOST_INTERFACE_H
22
23#include "network/network.h"
24
25/**
26 * A NetHost manages the client connections of a network game in
27 * which this computer participates as a server.
28 * This class provides the interface all NetHost implementation have to follow.
29 * Currently two implementations exists: A "real" NetHost for local games and a
30 * NetHostProxy which relays commands over a relay server.
31 */
32class NetHostInterface {
33public:
34 /// IDs used to enumerate the clients.
35 using ConnectionId = uint8_t;
36
37 /**
38 * Closes the server.
39 */
40 virtual ~NetHostInterface() {
41 }
42
43 /**
44 * Returns whether the given client is connected.
45 * \param The id of the client to check.
46 * \return \c true if the connection is open, \c false otherwise.
47 */
48 virtual bool is_connected(ConnectionId id) const = 0;
49
50 /**
51 * Closes the connection to the given client.
52 * \param id The id of the client to close the connection to.
53 */
54 virtual void close(ConnectionId id) = 0;
55
56 /**
57 * Tries to accept a new client.
58 * \param new_id The connection id of the new client will be stored here.
59 * \return \c true if a client has connected, \c false otherwise.
60 * The given id is only modified when \c true is returned.
61 * Calling this on a closed server will return false.
62 * The returned id is always greater than 0.
63 */
64 virtual bool try_accept(ConnectionId* new_id) = 0;
65
66 /**
67 * Tries to receive a packet.
68 * \param id The connection id of the client that should be received.
69 * \param packet A packet that should be overwritten with the received data.
70 * \return \c true if a packet is available, \c false otherwise.
71 * The given packet is only modified when \c true is returned.
72 * Calling this on a closed connection will return false.
73 */
74 virtual bool try_receive(ConnectionId id, RecvPacket* packet) = 0;
75
76 /**
77 * Sends a packet.
78 * Calling this on a closed connection will silently fail.
79 * \param id The connection id of the client that should be sent to.
80 * \param packet The packet to send.
81 */
82 virtual void send(ConnectionId id, const SendPacket& packet) = 0;
83
84 /**
85 * Sends a packet to a group of clients.
86 * Calling this on a closed connection will silently fail.
87 * \param ids The connection ids of the clients that should be sent to.
88 * \param packet The packet to send.
89 */
90 virtual void send(const std::vector<ConnectionId>& ids, const SendPacket& packet) = 0;
91};
92
93#endif // end of include guard: WL_NETWORK_NETHOST_INTERFACE_H

Subscribers

People subscribed via source and target branches

to status/vote changes: