1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Made intermediate class to hide some implementation details from header.

This commit is contained in:
Andrii Danylchenko 2022-12-27 12:55:31 +02:00
parent c271f9187f
commit a954cc08fb
2 changed files with 17 additions and 12 deletions

View File

@ -31,10 +31,18 @@ using namespace boost::asio::ip;
#define LIL_ENDIAN #define LIL_ENDIAN
#endif #endif
struct ConnectionBuffers
{
boost::asio::streambuf readBuffer;
boost::asio::streambuf writeBuffer;
};
void CConnection::init() void CConnection::init()
{ {
enableBufferedWrite = false; enableBufferedWrite = false;
enableBufferedRead = false;
connectionBuffers = std::make_unique<ConnectionBuffers>();
socket->set_option(boost::asio::ip::tcp::no_delay(true)); socket->set_option(boost::asio::ip::tcp::no_delay(true));
try try
{ {
@ -148,7 +156,7 @@ void CConnection::flushBuffers()
try try
{ {
asio::write(*socket, writeBuffer); asio::write(*socket, connectionBuffers->writeBuffer);
} }
catch(...) catch(...)
{ {
@ -166,7 +174,7 @@ int CConnection::write(const void * data, unsigned size)
{ {
if(enableBufferedWrite) if(enableBufferedWrite)
{ {
std::ostream ostream(&writeBuffer); std::ostream ostream(&connectionBuffers->writeBuffer);
ostream.write(static_cast<const char *>(data), size); ostream.write(static_cast<const char *>(data), size);
@ -191,16 +199,16 @@ int CConnection::read(void * data, unsigned size)
{ {
if(enableBufferedRead) if(enableBufferedRead)
{ {
auto available = readBuffer.size(); auto available = connectionBuffers->readBuffer.size();
while(available < size) while(available < size)
{ {
auto bytesRead = socket->read_some(readBuffer.prepare(1024)); auto bytesRead = socket->read_some(connectionBuffers->readBuffer.prepare(1024));
readBuffer.commit(bytesRead); connectionBuffers->readBuffer.commit(bytesRead);
available = readBuffer.size(); available = connectionBuffers->readBuffer.size();
} }
std::istream istream(&readBuffer); std::istream istream(&connectionBuffers->readBuffer);
istream.read(static_cast<char *>(data), size); istream.read(static_cast<char *>(data), size);

View File

@ -21,8 +21,6 @@ namespace boost
{ {
namespace asio namespace asio
{ {
class streambuf;
namespace ip namespace ip
{ {
class tcp; class tcp;
@ -54,6 +52,7 @@ typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::so
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
struct CPack; struct CPack;
struct ConnectionBuffers;
/// Main class for network communication /// Main class for network communication
/// Allows establishing connection and bidirectional read-write /// Allows establishing connection and bidirectional read-write
@ -70,10 +69,8 @@ class DLL_LINKAGE CConnection
std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket
bool enableBufferedWrite; bool enableBufferedWrite;
boost::asio::streambuf writeBuffer;
bool enableBufferedRead; bool enableBufferedRead;
boost::asio::streambuf readBuffer; std::unique_ptr<ConnectionBuffers> connectionBuffers;
public: public:
BinaryDeserializer iser; BinaryDeserializer iser;