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-31 21:03:34 +00:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
2009-01-25 21:12:33 +00:00
|
|
|
using boost::asio::ip::tcp;
|
2009-01-25 20:19:09 +00:00
|
|
|
|
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
|
|
|
|
2009-01-31 21:03:34 +00:00
|
|
|
typedef std::list<boost::shared_ptr<NetPacket> > SendDataList;
|
|
|
|
|
|
|
|
|
|
class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
|
|
|
|
|
{
|
|
|
|
|
public:
|
2009-01-31 22:20:08 +00:00
|
|
|
SendDataManager(boost::shared_ptr<boost::asio::ip::tcp::socket> s)
|
|
|
|
|
: socket(s), writeInProgress(false)
|
2009-01-31 21:03:34 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleWrite(const boost::system::error_code& error);
|
|
|
|
|
|
|
|
|
|
void AsyncSendNextPacket(bool handlerMode = false);
|
|
|
|
|
|
2009-01-31 22:20:08 +00:00
|
|
|
boost::shared_ptr<boost::asio::ip::tcp::socket> socket;
|
2009-01-31 21:03:34 +00:00
|
|
|
|
|
|
|
|
mutable boost::mutex dataMutex;
|
|
|
|
|
SendDataList list;
|
|
|
|
|
bool writeInProgress;
|
|
|
|
|
};
|
|
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
|
|
|
|
|
void
|
2009-01-31 21:03:34 +00:00
|
|
|
SendDataManager::HandleWrite(const boost::system::error_code& error)
|
2009-01-25 20:19:09 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
// TODO error handling
|
|
|
|
|
AsyncSendNextPacket(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SendDataManager::AsyncSendNextPacket(bool handlerMode)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(dataMutex);
|
|
|
|
|
if (!writeInProgress || handlerMode)
|
|
|
|
|
{
|
|
|
|
|
if (handlerMode)
|
|
|
|
|
list.pop_front();
|
|
|
|
|
if (!list.empty())
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> nextPacket = list.front();
|
|
|
|
|
boost::asio::async_write(
|
2009-01-31 22:20:08 +00:00
|
|
|
*socket,
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::asio::buffer(nextPacket->GetRawData(),
|
|
|
|
|
nextPacket->GetLen()),
|
|
|
|
|
boost::bind(&SendDataManager::HandleWrite, shared_from_this(),
|
|
|
|
|
boost::asio::placeholders::error));
|
|
|
|
|
writeInProgress = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
writeInProgress = false;
|
|
|
|
|
}
|
2009-01-25 20:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-31 22:20:08 +00:00
|
|
|
SenderThread::SenderThread(SenderCallback &cb, boost::shared_ptr<boost::asio::io_service> ioService)
|
|
|
|
|
: m_callback(cb), m_ioService(ioService)
|
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-31 21:03:34 +00:00
|
|
|
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-01-31 22:20:08 +00:00
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session->GetAsioSocket())))).first;
|
2009-01-31 21:03:34 +00:00
|
|
|
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: Update notification list.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_changedSessionsMutex);
|
2009-01-31 22:20:08 +00:00
|
|
|
m_changedSessions.push_back(session->GetId());
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
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-31 21:03:34 +00:00
|
|
|
boost::shared_ptr<SendDataManager> tmpManager;
|
2008-04-23 21:19:00 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
// 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-01-31 22:20:08 +00:00
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session->GetAsioSocket())))).first;
|
2009-01-31 21:03:34 +00:00
|
|
|
tmpManager = pos->second;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
// Second: Add packet to specific queue.
|
|
|
|
|
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
|
|
|
|
|
if (tmpManager->list.size() + packetList.size() <= SEND_QUEUE_SIZE)
|
2009-01-07 21:52:16 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
NetPacketList::const_iterator i = packetList.begin();
|
|
|
|
|
NetPacketList::const_iterator end = packetList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
tmpManager->list.push_back(*i);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
2009-01-07 21:52:16 +00:00
|
|
|
}
|
2008-04-23 21:19:00 +00:00
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
{
|
|
|
|
|
// Third: Update notification list.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_changedSessionsMutex);
|
2009-01-31 22:20:08 +00:00
|
|
|
m_changedSessions.push_back(session->GetId());
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-31 22:20:08 +00:00
|
|
|
void
|
|
|
|
|
SenderThread::SignalSessionTerminated(unsigned sessionId)
|
2009-01-26 20:57:39 +00:00
|
|
|
{
|
2009-01-31 22:20:08 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_removedSessionsMutex);
|
|
|
|
|
m_removedSessions.push_back(sessionId);
|
2009-01-26 20:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void
|
|
|
|
|
SenderThread::Main()
|
|
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::asio::io_service::work ioWork(*m_ioService);
|
2007-11-16 15:24:25 +00:00
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
2009-01-31 22:20:08 +00:00
|
|
|
// Close sessions if they were destructed.
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_removedSessionsMutex);
|
|
|
|
|
if (!m_removedSessions.empty())
|
|
|
|
|
{
|
|
|
|
|
SessionIdList newRemovedSessions;
|
|
|
|
|
SessionIdList::iterator i = m_removedSessions.begin();
|
|
|
|
|
SessionIdList::iterator end = m_removedSessions.end();
|
|
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
SendQueueMap::iterator pos = m_sendQueueMap.find(*i);
|
|
|
|
|
if (pos != m_sendQueueMap.end())
|
|
|
|
|
{
|
|
|
|
|
// Remove session if no write is in progress, else wait.
|
|
|
|
|
if (!pos->second->writeInProgress)
|
|
|
|
|
m_sendQueueMap.erase(pos);
|
|
|
|
|
else
|
|
|
|
|
newRemovedSessions.push_back(*i);
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
m_removedSessions = newRemovedSessions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Iterate through all changed sessions, and send data if needed.
|
2009-01-31 21:03:34 +00:00
|
|
|
bool sessionValid;
|
|
|
|
|
do
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
sessionValid = false;
|
|
|
|
|
unsigned sessionId;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_changedSessionsMutex);
|
|
|
|
|
if (!m_changedSessions.empty())
|
2009-01-07 21:52:16 +00:00
|
|
|
{
|
2009-01-31 21:03:34 +00:00
|
|
|
sessionId = m_changedSessions.front();
|
|
|
|
|
m_changedSessions.pop_front();
|
|
|
|
|
sessionValid = true;
|
2009-01-07 21:52:16 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::shared_ptr<SendDataManager> tmpManager;
|
|
|
|
|
if (sessionValid)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
|
|
|
SendQueueMap::iterator pos = m_sendQueueMap.find(sessionId);
|
|
|
|
|
if (pos != m_sendQueueMap.end())
|
|
|
|
|
tmpManager = pos->second;
|
|
|
|
|
}
|
|
|
|
|
if (tmpManager)
|
|
|
|
|
tmpManager->AsyncSendNextPacket();
|
|
|
|
|
} while (sessionValid);
|
|
|
|
|
|
2009-01-26 20:57:39 +00:00
|
|
|
m_ioService->poll();
|
2009-01-25 21:56:43 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|