From 938d13e31ae36d4b368923abe7fab1bc41986fb8 Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 8 Jun 2009 21:54:24 +0000 Subject: [PATCH] Send all available data in one chunk within the send handler. This improves speed. --- src/net/common/senderhelper.cpp | 42 ++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/net/common/senderhelper.cpp b/src/net/common/senderhelper.cpp index c86532f0..b4347ed1 100644 --- a/src/net/common/senderhelper.cpp +++ b/src/net/common/senderhelper.cpp @@ -44,10 +44,15 @@ class SendDataManager : public boost::enable_shared_from_this { public: SendDataManager(boost::shared_ptr s) - : socket(s), writeInProgress(false) + : socket(s), sendBuf(NULL), writeInProgress(false) { } + ~SendDataManager() + { + delete[] sendBuf; + } + void HandleWrite(const boost::system::error_code &error); void AsyncSendNextPacket(bool handlerMode = false); @@ -56,6 +61,7 @@ class SendDataManager : public boost::enable_shared_from_this mutable boost::mutex dataMutex; SendDataList list; + char *sendBuf; bool writeInProgress; }; @@ -64,7 +70,8 @@ void SendDataManager::HandleWrite(const boost::system::error_code &error) { // TODO error handling - AsyncSendNextPacket(true); + if (!error) + AsyncSendNextPacket(true); } void @@ -73,15 +80,34 @@ SendDataManager::AsyncSendNextPacket(bool handlerMode) boost::mutex::scoped_lock lock(dataMutex); if (!writeInProgress || handlerMode) { - if (handlerMode) - list.pop_front(); - if (!list.empty()) + delete[] sendBuf; + sendBuf = NULL; + + unsigned bufPos = 0; + unsigned bufSize = 0; + // Count required bytes. + SendDataList::iterator i = list.begin(); + SendDataList::iterator end = list.end(); + while (i != end) { - boost::shared_ptr nextPacket = list.front(); + bufSize += (*i)->GetLen(); + ++i; + } + if (bufSize) + { + sendBuf = new char[bufSize]; + i = list.begin(); + end = list.end(); + while (i != end) + { + memcpy(sendBuf + bufPos, (*i)->GetRawData(), (*i)->GetLen()); + bufPos += (*i)->GetLen(); + ++i; + } + list.clear(); boost::asio::async_write( *socket, - boost::asio::buffer(nextPacket->GetRawData(), - nextPacket->GetLen()), + boost::asio::buffer(sendBuf, bufSize), boost::bind(&SendDataManager::HandleWrite, shared_from_this(), boost::asio::placeholders::error)); writeInProgress = true;