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
#endif
struct ConnectionBuffers
{
boost::asio::streambuf readBuffer;
boost::asio::streambuf writeBuffer;
};
void CConnection::init()
{
enableBufferedWrite = false;
enableBufferedRead = false;
connectionBuffers = std::make_unique<ConnectionBuffers>();
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<const char *>(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<char *>(data), size);

View File

@ -21,8 +21,6 @@ namespace boost
{
namespace asio
{
class streambuf;
namespace ip
{
class tcp;
@ -54,6 +52,7 @@ typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::so
VCMI_LIB_NAMESPACE_BEGIN
struct CPack;
struct ConnectionBuffers;
/// Main class for network communication
/// 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
bool enableBufferedWrite;
boost::asio::streambuf writeBuffer;
bool enableBufferedRead;
boost::asio::streambuf readBuffer;
std::unique_ptr<ConnectionBuffers> connectionBuffers;
public:
BinaryDeserializer iser;