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"
|
|
|
|
|
|
|
|
struct CPack;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/// 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;
|
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
|
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
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CConnection(std::string host, ui16 port, std::string Name, std::string UUID);
|
|
|
|
CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|