2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
2009-01-07 19:37:05 +00:00
|
|
|
* Copyright (C) 2007-2009 by Lothar May *
|
2007-11-16 15:24:25 +00:00
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2008-04-16 20:38:11 +00:00
|
|
|
#include <net/socket_helper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/senderthread.h>
|
|
|
|
|
#include <net/sendercallback.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
#include <core/loghelper.h>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2009-01-25 20:19:09 +00:00
|
|
|
#include <boost/asio.hpp>
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
2009-01-25 20:19:09 +00:00
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
#define SEND_ERROR_TIMEOUT_MSEC 20000
|
|
|
|
|
#define SEND_TIMEOUT_MSEC 10
|
2007-11-20 23:58:24 +00:00
|
|
|
#define SEND_QUEUE_SIZE 10000000
|
2008-03-20 00:06:19 +00:00
|
|
|
#define SEND_LOG_INTERVAL_SEC 60
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::SendDataManager::HandleWrite(const boost::system::error_code& error)
|
|
|
|
|
{
|
|
|
|
|
SetWriteInProgress(false);
|
|
|
|
|
SetCompleted(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
SenderThread::SenderThread(SenderCallback &cb)
|
2009-01-25 20:19:09 +00:00
|
|
|
: m_callback(cb)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SenderThread::~SenderThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-07 19:37:05 +00:00
|
|
|
void
|
|
|
|
|
SenderThread::Start()
|
|
|
|
|
{
|
|
|
|
|
Run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::SignalStop()
|
|
|
|
|
{
|
|
|
|
|
SignalTermination();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::WaitStop()
|
|
|
|
|
{
|
|
|
|
|
Join(SENDER_THREAD_TERMINATE_TIMEOUT);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void
|
|
|
|
|
SenderThread::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
|
|
|
|
|
{
|
|
|
|
|
if (packet.get() && session.get())
|
|
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
|
|
|
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
|
|
|
|
if (pos == m_sendQueueMap.end())
|
|
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session, m_ioService)))).first;
|
|
|
|
|
if (pos->second->list.size() < SEND_QUEUE_SIZE)
|
|
|
|
|
pos->second->list.push_back(packet);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
|
|
|
|
{
|
|
|
|
|
if (!packetList.empty() && session.get())
|
|
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
|
|
|
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
|
|
|
|
if (pos == m_sendQueueMap.end())
|
|
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session, m_ioService)))).first;
|
|
|
|
|
if (pos->second->list.size() + packetList.size() <= SEND_QUEUE_SIZE)
|
2008-04-23 21:19:00 +00:00
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
NetPacketList::const_iterator i = packetList.begin();
|
|
|
|
|
NetPacketList::const_iterator end = packetList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
pos->second->list.push_back(*i);
|
2009-01-07 21:52:16 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
2008-04-23 21:19:00 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::Main()
|
|
|
|
|
{
|
|
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
|
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
|
|
|
SendQueueMap::iterator i = m_sendQueueMap.begin();
|
|
|
|
|
SendQueueMap::iterator end = m_sendQueueMap.end();
|
|
|
|
|
while (i != end)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
SendQueueMap::iterator next = i;
|
|
|
|
|
++next;
|
|
|
|
|
boost::shared_ptr<SendDataManager> tmpManager = i->second;
|
|
|
|
|
if (tmpManager->list.empty())
|
|
|
|
|
m_sendQueueMap.erase(i);
|
|
|
|
|
else
|
2009-01-07 21:52:16 +00:00
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
if (!tmpManager->IsWriteInProgress())
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
if (tmpManager->IsCompleted())
|
|
|
|
|
tmpManager->list.pop_front();
|
|
|
|
|
else
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-25 20:19:09 +00:00
|
|
|
boost::shared_ptr<NetPacket> tmpPacket = tmpManager->list.front();
|
|
|
|
|
boost::asio::async_write(
|
|
|
|
|
*tmpManager->socket,
|
|
|
|
|
boost::asio::buffer(tmpPacket->GetRawData(),
|
|
|
|
|
tmpPacket->GetLen()),
|
|
|
|
|
boost::bind(&SendDataManager::HandleWrite, tmpManager,
|
|
|
|
|
boost::asio::placeholders::error));
|
|
|
|
|
tmpManager->SetWriteInProgress(true);
|
2007-11-20 23:58:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-01-07 21:52:16 +00:00
|
|
|
}
|
2009-01-25 20:19:09 +00:00
|
|
|
i = next;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2007-11-21 00:26:14 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2009-01-25 20:19:09 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|