Sfoglia il codice sorgente

Added timer functionality to NetworkServer

Might not be related to networking strictly speaking, but useful to have
functionality for network thread
Ivan Savenko 1 anno fa
parent
commit
a50cdbda0c

+ 5 - 0
client/serverLobby/LobbyWindow.cpp

@@ -90,6 +90,11 @@ void LobbyClient::onDisconnected(const std::shared_ptr<NetworkConnection> &)
 	CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
 }
 
+void LobbyClient::onTimer()
+{
+	// no-op
+}
+
 void LobbyClient::sendMessage(const JsonNode & data)
 {
 	std::string payloadString = data.toJson(true);

+ 1 - 0
client/serverLobby/LobbyWindow.h

@@ -36,6 +36,7 @@ class LobbyClient : public INetworkClientListener
 	void onConnectionFailed(const std::string & errorMessage) override;
 	void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) override;
 	void onDisconnected(const std::shared_ptr<NetworkConnection> &) override;
+	void onTimer() override;
 
 public:
 	explicit LobbyClient(LobbyWindow * window);

+ 9 - 0
lib/network/NetworkClient.cpp

@@ -75,6 +75,15 @@ void NetworkClient::stop()
 	io->stop();
 }
 
+void NetworkClient::setTimer(std::chrono::milliseconds duration)
+{
+	auto timer = std::make_shared<NetworkTimer>(*io, duration);
+	timer->async_wait([this, timer](const boost::system::error_code& error){
+		if (!error)
+			listener.onTimer();
+	});
+}
+
 void NetworkClient::sendPacket(const std::vector<uint8_t> & message)
 {
 	connection->sendPacket(message);

+ 1 - 0
lib/network/NetworkClient.h

@@ -37,6 +37,7 @@ public:
 	NetworkClient(INetworkClientListener & listener);
 	virtual ~NetworkClient() = default;
 
+	void setTimer(std::chrono::milliseconds duration);
 	void sendPacket(const std::vector<uint8_t> & message);
 
 	void start(const std::string & host, uint16_t port);

+ 3 - 2
lib/network/NetworkDefines.h

@@ -14,8 +14,9 @@
 VCMI_LIB_NAMESPACE_BEGIN
 
 using NetworkService = boost::asio::io_service;
-using NetworkSocket = boost::asio::basic_stream_socket<boost::asio::ip::tcp>;
-using NetworkAcceptor = boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>;
+using NetworkSocket = boost::asio::ip::tcp::socket;
+using NetworkAcceptor = boost::asio::ip::tcp::acceptor;
 using NetworkBuffer = boost::asio::streambuf;
+using NetworkTimer = boost::asio::steady_timer;
 
 VCMI_LIB_NAMESPACE_END

+ 1 - 0
lib/network/NetworkListener.h

@@ -38,6 +38,7 @@ class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
 {
 	friend class NetworkClient;
 protected:
+	virtual void onTimer() = 0;
 	virtual void onConnectionFailed(const std::string & errorMessage) = 0;
 	virtual void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) = 0;