mirror of
https://github.com/vcmi/vcmi.git
synced 2025-03-21 21:17:49 +02:00
Socket buffered write. Write full cpack at once
This commit is contained in:
parent
33602c879d
commit
cb13064a62
@ -34,6 +34,7 @@ using namespace boost::asio::ip;
|
|||||||
|
|
||||||
void CConnection::init()
|
void CConnection::init()
|
||||||
{
|
{
|
||||||
|
enableBufferedWrite = false;
|
||||||
socket->set_option(boost::asio::ip::tcp::no_delay(true));
|
socket->set_option(boost::asio::ip::tcp::no_delay(true));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -72,6 +73,7 @@ CConnection::CConnection(std::string host, ui16 port, std::string Name, std::str
|
|||||||
int i;
|
int i;
|
||||||
boost::system::error_code error = asio::error::host_not_found;
|
boost::system::error_code error = asio::error::host_not_found;
|
||||||
socket = std::make_shared<tcp::socket>(*io_service);
|
socket = std::make_shared<tcp::socket>(*io_service);
|
||||||
|
|
||||||
tcp::resolver resolver(*io_service);
|
tcp::resolver resolver(*io_service);
|
||||||
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)),error);
|
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)),error);
|
||||||
if(error)
|
if(error)
|
||||||
@ -138,8 +140,37 @@ CConnection::CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<bo
|
|||||||
}
|
}
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CConnection::flushBuffers()
|
||||||
|
{
|
||||||
|
if(!enableBufferedWrite)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
asio::write(*socket, writeBuffer);
|
||||||
|
}
|
||||||
|
catch(...)
|
||||||
|
{
|
||||||
|
//connection has been lost
|
||||||
|
connected = false;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
enableBufferedWrite = false;
|
||||||
|
}
|
||||||
|
|
||||||
int CConnection::write(const void * data, unsigned size)
|
int CConnection::write(const void * data, unsigned size)
|
||||||
{
|
{
|
||||||
|
if(enableBufferedWrite)
|
||||||
|
{
|
||||||
|
std::ostream ostream(&writeBuffer);
|
||||||
|
|
||||||
|
ostream.write(static_cast<const char *>(data), size);
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@ -153,6 +184,7 @@ int CConnection::write(const void * data, unsigned size)
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int CConnection::read(void * data, unsigned size)
|
int CConnection::read(void * data, unsigned size)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -167,6 +199,7 @@ int CConnection::read(void * data, unsigned size)
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CConnection::~CConnection()
|
CConnection::~CConnection()
|
||||||
{
|
{
|
||||||
if(handler)
|
if(handler)
|
||||||
@ -229,7 +262,12 @@ void CConnection::sendPack(const CPack * pack)
|
|||||||
{
|
{
|
||||||
boost::unique_lock<boost::mutex> lock(*mutexWrite);
|
boost::unique_lock<boost::mutex> lock(*mutexWrite);
|
||||||
logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
|
logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
|
||||||
|
|
||||||
|
enableBufferedWrite = true;
|
||||||
|
|
||||||
oser & pack;
|
oser & pack;
|
||||||
|
|
||||||
|
flushBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CConnection::disableStackSendingByID()
|
void CConnection::disableStackSendingByID()
|
||||||
|
@ -63,8 +63,13 @@ class DLL_LINKAGE CConnection
|
|||||||
|
|
||||||
int write(const void * data, unsigned size) override;
|
int write(const void * data, unsigned size) override;
|
||||||
int read(void * data, unsigned size) override;
|
int read(void * data, unsigned size) override;
|
||||||
|
void flushBuffers();
|
||||||
|
|
||||||
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;
|
||||||
|
boost::asio::streambuf writeBuffer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BinaryDeserializer iser;
|
BinaryDeserializer iser;
|
||||||
BinarySerializer oser;
|
BinarySerializer oser;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user