Improving possible multithreading in new send code.

This commit is contained in:
lotodore
2011-02-19 15:01:50 +00:00
parent 8279868417
commit f7315cd9e5
2 changed files with 18 additions and 26 deletions
+1 -1
View File
@@ -44,6 +44,7 @@ SendDataManager::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> soc
{ {
if (!error) { if (!error) {
// Successfully sent the data. // Successfully sent the data.
boost::mutex::scoped_lock lock(dataMutex);
curWriteBufUsed = 0; curWriteBufUsed = 0;
// Send more data, if available. // Send more data, if available.
AsyncSendNextPacket(socket); AsyncSendNextPacket(socket);
@@ -53,7 +54,6 @@ SendDataManager::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> soc
void void
SendDataManager::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket) SendDataManager::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket)
{ {
boost::mutex::scoped_lock lock(dataMutex);
if (!curWriteBufUsed) { if (!curWriteBufUsed) {
// Swap buffers and send data. // Swap buffers and send data.
boost::swap(curWriteBuf, sendBuf); boost::swap(curWriteBuf, sendBuf);
+17 -25
View File
@@ -42,17 +42,13 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
{ {
if (packet && session) { if (packet && session) {
SendDataManager &tmpManager = session->GetSendDataManager(); SendDataManager &tmpManager = session->GetSendDataManager();
{ // Add packet to specific queue.
// First: Add packet to specific queue. boost::mutex::scoped_lock lock(tmpManager.dataMutex);
boost::mutex::scoped_lock lock(tmpManager.dataMutex); if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) {
if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) { InternalStorePacket(tmpManager, packet);
InternalStorePacket(tmpManager, packet);
}
}
{
// Second: Activate async send, if needed.
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
} }
// Activate async send, if needed.
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
} }
} }
@@ -61,23 +57,19 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
{ {
if (!packetList.empty() && session) { if (!packetList.empty() && session) {
SendDataManager &tmpManager = session->GetSendDataManager(); SendDataManager &tmpManager = session->GetSendDataManager();
{ // Add packets to specific queue.
// First: Add packets to specific queue. boost::mutex::scoped_lock lock(tmpManager.dataMutex);
boost::mutex::scoped_lock lock(tmpManager.dataMutex); if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) {
if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) { NetPacketList::const_iterator i = packetList.begin();
NetPacketList::const_iterator i = packetList.begin(); NetPacketList::const_iterator end = packetList.end();
NetPacketList::const_iterator end = packetList.end(); while (i != end) {
while (i != end) { if (*i)
if (*i) InternalStorePacket(tmpManager, *i);
InternalStorePacket(tmpManager, *i); ++i;
++i;
}
} }
} }
{ // Activate async send, if needed.
// Second: Activate async send, if needed. tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
}
} }
} }