2016-09-10 02:28:11 +02:00
|
|
|
/*
|
|
|
|
* Connection.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
|
|
|
|
|
|
|
|
#include "BinaryDeserializer.h"
|
|
|
|
#include "BinarySerializer.h"
|
|
|
|
|
2020-01-21 10:55:28 +02:00
|
|
|
#if BOOST_VERSION >= 107000 // Boost version >= 1.70
|
|
|
|
#include <boost/asio.hpp>
|
2023-04-17 23:11:16 +02:00
|
|
|
using TSocket = boost::asio::basic_stream_socket<boost::asio::ip::tcp>;
|
|
|
|
using TAcceptor = boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>;
|
2020-01-21 10:55:28 +02:00
|
|
|
#else
|
2016-09-10 02:28:11 +02:00
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
namespace asio
|
|
|
|
{
|
|
|
|
namespace ip
|
|
|
|
{
|
|
|
|
class tcp;
|
|
|
|
}
|
2018-03-11 15:02:20 +02:00
|
|
|
|
|
|
|
#if BOOST_VERSION >= 106600 // Boost version >= 1.66
|
|
|
|
class io_context;
|
|
|
|
typedef io_context io_service;
|
|
|
|
#else
|
2016-09-10 02:28:11 +02:00
|
|
|
class io_service;
|
2018-03-11 15:02:20 +02:00
|
|
|
#endif
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
template <typename Protocol> class stream_socket_service;
|
|
|
|
template <typename Protocol,typename StreamSocketService>
|
|
|
|
class basic_stream_socket;
|
|
|
|
|
|
|
|
template <typename Protocol> class socket_acceptor_service;
|
|
|
|
template <typename Protocol,typename SocketAcceptorService>
|
|
|
|
class basic_socket_acceptor;
|
|
|
|
}
|
|
|
|
class mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
|
|
|
|
typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
|
2020-01-21 10:55:28 +02:00
|
|
|
#endif
|
|
|
|
|
2016-09-10 02:28:11 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct CPack;
|
2022-12-27 12:55:31 +02:00
|
|
|
struct ConnectionBuffers;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
2016-09-10 02:28:11 +02:00
|
|
|
/// Main class for network communication
|
|
|
|
/// Allows establishing connection and bidirectional read-write
|
|
|
|
class DLL_LINKAGE CConnection
|
2018-01-05 19:21:07 +02:00
|
|
|
: public IBinaryReader, public IBinaryWriter, public std::enable_shared_from_this<CConnection>
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
void init();
|
2017-08-11 19:03:05 +02:00
|
|
|
void reportState(vstd::CLoggerBase * out) override;
|
2016-10-29 18:52:19 +02:00
|
|
|
|
|
|
|
int write(const void * data, unsigned size) override;
|
|
|
|
int read(void * data, unsigned size) override;
|
2022-12-26 20:13:07 +02:00
|
|
|
void flushBuffers();
|
2018-04-30 17:09:48 +02:00
|
|
|
|
|
|
|
std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket
|
2022-12-26 20:13:07 +02:00
|
|
|
|
|
|
|
bool enableBufferedWrite;
|
2022-12-26 21:28:36 +02:00
|
|
|
bool enableBufferedRead;
|
2022-12-27 12:55:31 +02:00
|
|
|
std::unique_ptr<ConnectionBuffers> connectionBuffers;
|
2022-12-26 21:28:36 +02:00
|
|
|
|
2016-09-10 02:28:11 +02:00
|
|
|
public:
|
|
|
|
BinaryDeserializer iser;
|
|
|
|
BinarySerializer oser;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::shared_ptr<boost::mutex> mutexRead;
|
|
|
|
std::shared_ptr<boost::mutex> mutexWrite;
|
|
|
|
std::shared_ptr<TSocket> socket;
|
2016-09-10 02:28:11 +02:00
|
|
|
bool connected;
|
|
|
|
bool myEndianess, contactEndianess; //true if little endian, if endianness is different we'll have to revert received multi-byte vars
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string contactUuid;
|
2016-09-10 02:28:11 +02:00
|
|
|
std::string name; //who uses this connection
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string uuid;
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
int connectionID;
|
2018-01-05 19:21:07 +02:00
|
|
|
std::shared_ptr<boost::thread> handler;
|
2016-09-10 02:28:11 +02:00
|
|
|
|
2023-02-11 17:13:21 +02:00
|
|
|
CConnection(const std::string & host, ui16 port, std::string Name, std::string UUID);
|
|
|
|
CConnection(const std::shared_ptr<TAcceptor> & acceptor, const std::shared_ptr<boost::asio::io_service> & Io_service, std::string Name, std::string UUID);
|
2018-01-05 19:21:07 +02:00
|
|
|
CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
void close();
|
|
|
|
bool isOpen() const;
|
|
|
|
template<class T>
|
|
|
|
CConnection &operator&(const T&);
|
2018-01-13 10:43:26 +02:00
|
|
|
virtual ~CConnection();
|
2016-09-10 02:28:11 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CPack * retrievePack();
|
|
|
|
void sendPack(const CPack * pack);
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
void disableStackSendingByID();
|
|
|
|
void enableStackSendingByID();
|
|
|
|
void disableSmartPointerSerialization();
|
2016-11-13 11:44:46 +02:00
|
|
|
void enableSmartPointerSerialization();
|
2016-09-10 02:28:11 +02:00
|
|
|
void disableSmartVectorMemberSerialization();
|
|
|
|
void enableSmartVectorMemberSerializatoin();
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void enterLobbyConnectionMode();
|
|
|
|
void enterGameplayConnectionMode(CGameState * gs);
|
2016-09-10 02:28:11 +02:00
|
|
|
|
2017-08-11 15:50:00 +02:00
|
|
|
std::string toString() const;
|
|
|
|
|
2016-09-10 02:28:11 +02:00
|
|
|
template<class T>
|
|
|
|
CConnection & operator>>(T &t)
|
|
|
|
{
|
|
|
|
iser & t;
|
|
|
|
return * this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
CConnection & operator<<(const T &t)
|
|
|
|
{
|
|
|
|
oser & t;
|
|
|
|
return * this;
|
|
|
|
}
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|