From a954cc08fbf32d535e37b81ab8262a742e5f00cb Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Tue, 27 Dec 2022 12:55:31 +0200 Subject: [PATCH] Made intermediate class to hide some implementation details from header. --- lib/serializer/Connection.cpp | 22 +++++++++++++++------- lib/serializer/Connection.h | 7 ++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/serializer/Connection.cpp b/lib/serializer/Connection.cpp index dcad536a8..f84b0c985 100644 --- a/lib/serializer/Connection.cpp +++ b/lib/serializer/Connection.cpp @@ -31,10 +31,18 @@ using namespace boost::asio::ip; #define LIL_ENDIAN #endif +struct ConnectionBuffers +{ + boost::asio::streambuf readBuffer; + boost::asio::streambuf writeBuffer; +}; void CConnection::init() { enableBufferedWrite = false; + enableBufferedRead = false; + connectionBuffers = std::make_unique(); + socket->set_option(boost::asio::ip::tcp::no_delay(true)); try { @@ -148,7 +156,7 @@ void CConnection::flushBuffers() try { - asio::write(*socket, writeBuffer); + asio::write(*socket, connectionBuffers->writeBuffer); } catch(...) { @@ -166,7 +174,7 @@ int CConnection::write(const void * data, unsigned size) { if(enableBufferedWrite) { - std::ostream ostream(&writeBuffer); + std::ostream ostream(&connectionBuffers->writeBuffer); ostream.write(static_cast(data), size); @@ -191,16 +199,16 @@ int CConnection::read(void * data, unsigned size) { if(enableBufferedRead) { - auto available = readBuffer.size(); + auto available = connectionBuffers->readBuffer.size(); while(available < size) { - auto bytesRead = socket->read_some(readBuffer.prepare(1024)); - readBuffer.commit(bytesRead); - available = readBuffer.size(); + auto bytesRead = socket->read_some(connectionBuffers->readBuffer.prepare(1024)); + connectionBuffers->readBuffer.commit(bytesRead); + available = connectionBuffers->readBuffer.size(); } - std::istream istream(&readBuffer); + std::istream istream(&connectionBuffers->readBuffer); istream.read(static_cast(data), size); diff --git a/lib/serializer/Connection.h b/lib/serializer/Connection.h index bbe79d6a0..478822885 100644 --- a/lib/serializer/Connection.h +++ b/lib/serializer/Connection.h @@ -21,8 +21,6 @@ namespace boost { namespace asio { - class streambuf; - namespace ip { class tcp; @@ -54,6 +52,7 @@ typedef boost::asio::basic_socket_acceptor io_service; //can be empty if connection made from socket bool enableBufferedWrite; - boost::asio::streambuf writeBuffer; - bool enableBufferedRead; - boost::asio::streambuf readBuffer; + std::unique_ptr connectionBuffers; public: BinaryDeserializer iser;