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>
|
|
|
|
|
|
2008-03-23 10:36:17 +00:00
|
|
|
#include <third_party/boost/timers.hpp>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
SenderThread::SenderThread(SenderCallback &cb)
|
2007-11-20 23:58:24 +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())
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock0(m_sessionsStalledMutex);
|
|
|
|
|
if (find(m_sessionsStalled.begin(), m_sessionsStalled.end(), session->GetId()) == m_sessionsStalled.end())
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
|
|
|
|
InternalStore(m_sendQueue, SEND_QUEUE_SIZE, session, packet);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// This session is stalled.
|
|
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
|
|
|
|
InternalStore(m_stalledQueue, SEND_QUEUE_SIZE, session, 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())
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock0(m_sessionsStalledMutex);
|
|
|
|
|
if (find(m_sessionsStalled.begin(), m_sessionsStalled.end(), session->GetId()) == m_sessionsStalled.end())
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
|
|
|
|
InternalStore(m_sendQueue, SEND_QUEUE_SIZE, session, packetList);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// This session is stalled.
|
|
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
|
|
|
|
InternalStore(m_stalledQueue, SEND_QUEUE_SIZE, session, packetList);
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
SenderThread::GetNumPacketsInQueue() const
|
|
|
|
|
{
|
|
|
|
|
unsigned numPackets;
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2007-11-20 23:58:24 +00:00
|
|
|
numPackets = m_sendQueue.size();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
2007-11-20 23:58:24 +00:00
|
|
|
numPackets += m_stalledQueue.size();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
return numPackets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
SenderThread::operator<(const SenderThread &other) const
|
|
|
|
|
{
|
|
|
|
|
return GetNumPacketsInQueue() < other.GetNumPacketsInQueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-11-20 23:58:24 +00:00
|
|
|
SenderThread::InternalStore(SendDataList &sendQueue, unsigned maxQueueSize, boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
if (sendQueue.size() < maxQueueSize) // Queue is limited in size.
|
2007-11-20 23:58:24 +00:00
|
|
|
sendQueue.push_back(SendData(packet, session));
|
2007-11-16 15:24:25 +00:00
|
|
|
// TODO: Throw exception if failed.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-11-20 23:58:24 +00:00
|
|
|
SenderThread::InternalStore(SendDataList &sendQueue, unsigned maxQueueSize, boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
if (sendQueue.size() + packetList.size() <= maxQueueSize)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
NetPacketList::const_iterator i = packetList.begin();
|
|
|
|
|
NetPacketList::const_iterator end = packetList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
sendQueue.push_back(SendData(*i, session));
|
2007-11-16 15:24:25 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: Throw exception if failed.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::Main()
|
|
|
|
|
{
|
|
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
/*
|
|
|
|
|
* To prevent stalling of the sender, keeping the order
|
|
|
|
|
* of the packets in the sender queue is not guaranteed.
|
|
|
|
|
* Instead, only the order of the packets for a single
|
|
|
|
|
* target session is maintained.
|
|
|
|
|
*
|
|
|
|
|
* This could also be done by using one sender thread
|
|
|
|
|
* for each session, but that would require too many
|
|
|
|
|
* resources.
|
|
|
|
|
*
|
|
|
|
|
* The send queue is a list of packets. Whenever a
|
|
|
|
|
* select timeout occurs, the session of the current
|
|
|
|
|
* packet is placed in the stalled list, and all other
|
|
|
|
|
* packets from the queue for that session are also
|
|
|
|
|
* attached to the stalled list. This process is
|
|
|
|
|
* continued with the next packet in the send queue.
|
|
|
|
|
*
|
|
|
|
|
* When the send queue is empty, the list of stalled
|
|
|
|
|
* packets is copied back to the send queue, and
|
|
|
|
|
* everything starts from the beginning.
|
|
|
|
|
*
|
|
|
|
|
* No packets are lost in this algorithm, and at the same
|
|
|
|
|
* time, the send process is never fully stalled,
|
|
|
|
|
* except when only packets for stalled sessions are
|
|
|
|
|
* present.
|
|
|
|
|
*
|
|
|
|
|
* Note: If someone keeps putting in packets to send, the
|
|
|
|
|
* stalled packets will never be sent, but this is
|
|
|
|
|
* considered more a theoretical problem.
|
|
|
|
|
*/
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
// For reasons of simplicity, only one packet is sent at a time.
|
2007-11-20 23:58:24 +00:00
|
|
|
SendData tmpData;
|
|
|
|
|
// Check main queue.
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock0(m_sessionsStalledMutex);
|
|
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2007-11-20 23:58:24 +00:00
|
|
|
if (m_sendQueue.empty())
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
// Check stalled queue.
|
2008-04-23 21:19:00 +00:00
|
|
|
// Attention: TRIPLE lock (on purpose).
|
2007-11-20 23:58:24 +00:00
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
|
|
|
|
if (!m_stalledQueue.empty())
|
2008-04-23 21:19:00 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
m_sendQueue.swap(m_stalledQueue);
|
2008-04-23 21:19:00 +00:00
|
|
|
m_sessionsStalled.clear(); // No more sessions stalled, all in send list.
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
if (!m_sendQueue.empty())
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
tmpData = m_sendQueue.front();
|
|
|
|
|
m_sendQueue.pop_front();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
|
|
|
|
|
if (tmpData.packet && tmpData.session)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
const unsigned tmpLen = tmpData.packet->GetLen();
|
|
|
|
|
if (tmpLen <= MAX_PACKET_SIZE)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
SOCKET tmpSocket = tmpData.session->GetSocket();
|
|
|
|
|
|
|
|
|
|
// send next chunk of data
|
|
|
|
|
int bytesSent = send(tmpSocket, ((const char *)tmpData.packet->GetRawData()) + tmpData.bytesSent, tmpLen - tmpData.bytesSent, SOCKET_SEND_FLAGS);
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_SEND(bytesSent))
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
// Never assume that this is a fatal error.
|
|
|
|
|
int errCode = SOCKET_ERRNO();
|
|
|
|
|
if (IS_SOCKET_ERR_WOULDBLOCK(errCode))
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
fd_set writeSet;
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
|
|
FD_ZERO(&writeSet);
|
|
|
|
|
FD_SET(tmpSocket, &writeSet);
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
|
timeout.tv_usec = SEND_TIMEOUT_MSEC * 1000;
|
|
|
|
|
int selectResult = select(tmpSocket + 1, NULL, &writeSet, NULL, &timeout);
|
|
|
|
|
if (!IS_VALID_SELECT(selectResult))
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
// Never assume that this is a fatal error.
|
|
|
|
|
int errCode = SOCKET_ERRNO();
|
|
|
|
|
if (!IS_SOCKET_ERR_WOULDBLOCK(errCode))
|
|
|
|
|
{
|
|
|
|
|
// Skip this packet - this is bad, and is therefore reported.
|
|
|
|
|
// Ignore invalid or not connected sockets.
|
|
|
|
|
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
|
|
|
|
m_callback.SignalNetError(tmpData.session->GetId(), ERR_SOCK_SELECT_FAILED, errCode);
|
|
|
|
|
}
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
else if (selectResult == 0)
|
|
|
|
|
{
|
|
|
|
|
// A timeout occured - don't block the thread.
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
// Attention: TRIPLE lock (on purpose).
|
2007-11-20 23:58:24 +00:00
|
|
|
// Stall all packets for that sender.
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock0(m_sessionsStalledMutex);
|
|
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2007-11-20 23:58:24 +00:00
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
|
|
|
|
m_stalledQueue.push_back(tmpData);
|
2008-04-23 21:19:00 +00:00
|
|
|
m_sessionsStalled.push_back(tmpData.session->GetId());
|
2007-11-20 23:58:24 +00:00
|
|
|
SendDataList::iterator i = m_sendQueue.begin();
|
|
|
|
|
SendDataList::iterator end = m_sendQueue.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
SendDataList::iterator next = i;
|
|
|
|
|
++next;
|
|
|
|
|
if ((*i).session && (*i).session->GetId() == tmpData.session->GetId())
|
|
|
|
|
{
|
|
|
|
|
m_stalledQueue.push_back(*i);
|
|
|
|
|
m_sendQueue.erase(i);
|
|
|
|
|
}
|
|
|
|
|
i = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Select was successful - store the packet back in the main queue.
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2007-11-22 20:18:19 +00:00
|
|
|
m_sendQueue.push_front(tmpData);
|
2007-11-20 23:58:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else // other errors than would block
|
|
|
|
|
{
|
|
|
|
|
// Skip this packet - this is bad, and is therefore reported.
|
|
|
|
|
// Ignore invalid or not connected sockets.
|
|
|
|
|
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
|
|
|
|
m_callback.SignalNetError(tmpData.session->GetId(), ERR_SOCK_SEND_FAILED, errCode);
|
2007-11-16 15:24:25 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
else if ((unsigned)bytesSent + tmpData.bytesSent < tmpLen)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
if (bytesSent)
|
|
|
|
|
{
|
|
|
|
|
tmpData.bytesSent += bytesSent;
|
|
|
|
|
// Send was partly successful - store the packet back in the main queue.
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2007-11-22 20:18:19 +00:00
|
|
|
m_sendQueue.push_front(tmpData);
|
2007-11-20 23:58:24 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2007-11-20 23:58:24 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2007-11-21 00:26:14 +00:00
|
|
|
else
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2008-03-20 00:06:19 +00:00
|
|
|
|
|
|
|
|
if (m_logTimer.elapsed().total_seconds() >= SEND_LOG_INTERVAL_SEC)
|
|
|
|
|
{
|
|
|
|
|
unsigned sendQueueSize = 0;
|
|
|
|
|
unsigned stalledQueueSize = 0;
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock1(m_sendQueueMutex);
|
2008-03-20 00:06:19 +00:00
|
|
|
sendQueueSize = m_sendQueue.size();
|
|
|
|
|
}
|
|
|
|
|
{
|
2008-04-23 21:19:00 +00:00
|
|
|
boost::mutex::scoped_lock lock2(m_stalledQueueMutex);
|
2008-03-20 00:06:19 +00:00
|
|
|
stalledQueueSize = m_stalledQueue.size();
|
|
|
|
|
}
|
|
|
|
|
LOG_VERBOSE("Sender TICK - send queue " << sendQueueSize << ", stalled " << stalledQueueSize << ".");
|
|
|
|
|
m_logTimer.reset();
|
|
|
|
|
m_logTimer.start();
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|