Files
pokerth/src/net/common/senderhelper.cpp
T

203 lines
5.9 KiB
C++
Raw Normal View History

/***************************************************************************
2009-01-07 19:37:05 +00:00
* Copyright (C) 2007-2009 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2009-06-20 11:15:06 +00:00
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>\
2009-05-10 22:21:20 +00:00
#include <net/senderhelper.h>
#include <net/sendercallback.h>
2009-06-20 11:15:06 +00:00
#include <net/socket_helper.h>
#include <net/socket_msg.h>
#include <core/loghelper.h>
#include <cstring>
#include <cassert>
using namespace std;
using boost::asio::ip::tcp;
#define SEND_ERROR_TIMEOUT_MSEC 20000
#define SEND_TIMEOUT_MSEC 10
#define SEND_QUEUE_SIZE 10000000
2008-03-20 00:06:19 +00:00
#define SEND_LOG_INTERVAL_SEC 60
typedef std::list<boost::shared_ptr<NetPacket> > SendDataList;
2009-06-20 11:15:06 +00:00
class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
{
public:
2009-06-09 23:04:04 +00:00
SendDataManager()
: sendBuf(NULL), writeInProgress(false)
{
}
~SendDataManager()
{
delete[] sendBuf;
}
2009-06-09 23:04:04 +00:00
void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
2009-06-09 23:04:04 +00:00
void AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, bool handlerMode = false);
mutable boost::mutex dataMutex;
SendDataList list;
char *sendBuf;
bool writeInProgress;
};
void
2009-06-09 23:04:04 +00:00
SendDataManager::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error)
{
if (!error)
2009-06-09 23:04:04 +00:00
AsyncSendNextPacket(socket, true);
}
void
2009-06-09 23:04:04 +00:00
SendDataManager::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, bool handlerMode)
{
boost::mutex::scoped_lock lock(dataMutex);
if (!writeInProgress || handlerMode)
{
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)
{
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(sendBuf, bufSize),
2009-06-09 23:04:04 +00:00
boost::bind(&SendDataManager::HandleWrite,
2009-06-20 11:15:06 +00:00
shared_from_this(),
2009-06-09 23:04:04 +00:00
socket,
boost::asio::placeholders::error));
writeInProgress = true;
}
else
writeInProgress = false;
}
}
2009-05-10 22:21:20 +00:00
SenderHelper::SenderHelper(SenderCallback &cb, boost::shared_ptr<boost::asio::io_service> ioService)
: m_callback(cb), m_ioService(ioService)
{
}
2009-05-10 22:21:20 +00:00
SenderHelper::~SenderHelper()
{
}
void
2009-05-10 22:21:20 +00:00
SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
{
2009-07-22 20:19:56 +00:00
if (packet && session)
{
boost::shared_ptr<SendDataManager> tmpManager;
{
// First: lock map of all queues. Locate/insert queue.
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
if (pos == m_sendQueueMap.end())
2009-06-09 23:04:04 +00:00
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
tmpManager = pos->second;
}
{
// Second: Add packet to specific queue.
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
if (tmpManager->list.size() < SEND_QUEUE_SIZE)
{
tmpManager->list.push_back(packet);
}
}
{
// Third: Activate async send, if needed.
2009-06-09 23:04:04 +00:00
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
}
}
}
void
2009-05-10 22:21:20 +00:00
SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
{
2009-07-22 20:19:56 +00:00
if (!packetList.empty() && session)
{
boost::shared_ptr<SendDataManager> tmpManager;
{
// First: lock map of all queues. Locate/insert queue.
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
if (pos == m_sendQueueMap.end())
2009-06-09 23:04:04 +00:00
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
tmpManager = pos->second;
}
{
// Second: Add packets to specific queue.
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
if (tmpManager->list.size() + packetList.size() <= SEND_QUEUE_SIZE)
{
NetPacketList::const_iterator i = packetList.begin();
NetPacketList::const_iterator end = packetList.end();
while (i != end)
{
2009-07-22 20:19:56 +00:00
if (*i)
tmpManager->list.push_back(*i);
++i;
}
}
}
{
// Third: Activate async send, if needed.
2009-06-09 23:04:04 +00:00
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
}
}
}
void
2009-05-10 22:21:20 +00:00
SenderHelper::SignalSessionTerminated(unsigned sessionId)
2009-01-26 20:57:39 +00:00
{
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
2009-01-26 20:57:39 +00:00
SendQueueMap::iterator pos = m_sendQueueMap.find(sessionId);
if (pos != m_sendQueueMap.end())
m_sendQueueMap.erase(pos);
}