mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
35954dc41b
At the moment, vcmilobby *requires* async writes in order to handle multiple connections with different speeds and at optimal performance, without hanging if one player is too slow and can't eat all data server sent to him at once. However server (and potentially - client) can not handle this mode and may shutdown either socket or entire asio service too early, before all writes are performed, leading to weird freeze on ending scenario where client would not receive notifications about end of game.
108 lines
3.8 KiB
C++
108 lines
3.8 KiB
C++
/*
|
|
* NetworkHandler.h, part of VCMI engine
|
|
*
|
|
* Authors: listed in file AUTHORS in main folder
|
|
*
|
|
* License: GNU General Public License v2.0 or later
|
|
* Full text of license available in license.txt file, in main folder
|
|
*
|
|
*/
|
|
#pragma once
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
/// Base class for connections with other services, either incoming or outgoing
|
|
class DLL_LINKAGE INetworkConnection : boost::noncopyable
|
|
{
|
|
public:
|
|
virtual ~INetworkConnection() = default;
|
|
virtual void sendPacket(const std::vector<std::byte> & message) = 0;
|
|
virtual void setAsyncWritesEnabled(bool on) = 0;
|
|
virtual void close() = 0;
|
|
};
|
|
|
|
using NetworkConnectionPtr = std::shared_ptr<INetworkConnection>;
|
|
using NetworkConnectionWeakPtr = std::weak_ptr<INetworkConnection>;
|
|
|
|
/// Base class for outgoing connections support
|
|
class DLL_LINKAGE INetworkClient : boost::noncopyable
|
|
{
|
|
public:
|
|
virtual ~INetworkClient() = default;
|
|
|
|
virtual bool isConnected() const = 0;
|
|
virtual void sendPacket(const std::vector<std::byte> & message) = 0;
|
|
};
|
|
|
|
/// Base class for incoming connections support
|
|
class DLL_LINKAGE INetworkServer : boost::noncopyable
|
|
{
|
|
public:
|
|
virtual ~INetworkServer() = default;
|
|
|
|
virtual void start(uint16_t port) = 0;
|
|
};
|
|
|
|
/// Base interface that must be implemented by user of networking API to handle any connection callbacks
|
|
class DLL_LINKAGE INetworkConnectionListener
|
|
{
|
|
public:
|
|
virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) = 0;
|
|
virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) = 0;
|
|
|
|
virtual ~INetworkConnectionListener() = default;
|
|
};
|
|
|
|
/// Interface that must be implemented by user of networking API to handle outgoing connection callbacks
|
|
class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
|
|
{
|
|
public:
|
|
virtual void onConnectionFailed(const std::string & errorMessage) = 0;
|
|
virtual void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) = 0;
|
|
};
|
|
|
|
/// Interface that must be implemented by user of networking API to handle incoming connection callbacks
|
|
class DLL_LINKAGE INetworkServerListener : public INetworkConnectionListener
|
|
{
|
|
public:
|
|
virtual void onNewConnection(const std::shared_ptr<INetworkConnection> &) = 0;
|
|
};
|
|
|
|
/// Interface that must be implemented by user of networking API to handle timers on network thread
|
|
class DLL_LINKAGE INetworkTimerListener
|
|
{
|
|
public:
|
|
virtual ~INetworkTimerListener() = default;
|
|
|
|
virtual void onTimer() = 0;
|
|
};
|
|
|
|
/// Main class for handling of all network activity
|
|
class DLL_LINKAGE INetworkHandler : boost::noncopyable
|
|
{
|
|
public:
|
|
virtual ~INetworkHandler() = default;
|
|
|
|
/// Constructs default implementation
|
|
static std::unique_ptr<INetworkHandler> createHandler();
|
|
|
|
/// Creates an instance of TCP server that allows to receiving connections on a local port
|
|
virtual std::unique_ptr<INetworkServer> createServerTCP(INetworkServerListener & listener) = 0;
|
|
|
|
/// Creates an instance of TCP client that allows to establish single outgoing connection to a remote port
|
|
/// On success: INetworkTimerListener::onConnectionEstablished() will be called, established connection provided as parameter
|
|
/// On failure: INetworkTimerListener::onConnectionFailed will be called with human-readable error message
|
|
virtual void connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port) = 0;
|
|
|
|
/// Creates a timer that will be called once, after specified interval has passed
|
|
/// On success: INetworkTimerListener::onTimer() will be called
|
|
/// On failure: no-op
|
|
virtual void createTimer(INetworkTimerListener & listener, std::chrono::milliseconds duration) = 0;
|
|
|
|
/// Starts network processing on this thread. Does not returns until networking processing has been terminated
|
|
virtual void run() = 0;
|
|
virtual void stop() = 0;
|
|
};
|
|
|
|
VCMI_LIB_NAMESPACE_END
|