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>
|
|
|
|
|
|
|
|
|
|
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)
|
2009-01-07 21:52:16 +00:00
|
|
|
: m_callback(cb), m_bytesSent(0)
|
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
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
|
|
|
|
if (m_sendQueue.size() < SEND_QUEUE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
m_sendQueue.push_back(packet);
|
|
|
|
|
}
|
2008-04-23 21:19:00 +00:00
|
|
|
}
|
|
|
|
|
{
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
m_sessionSocket = session->GetSocket();
|
|
|
|
|
m_sessionId = session->GetId();
|
2008-04-23 21:19:00 +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-07 21:52:16 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
|
|
|
|
if (m_sendQueue.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)
|
|
|
|
|
{
|
|
|
|
|
m_sendQueue.push_back(*i);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
2008-04-23 21:19:00 +00:00
|
|
|
}
|
|
|
|
|
{
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
m_sessionSocket = session->GetSocket();
|
|
|
|
|
m_sessionId = session->GetId();
|
2008-04-23 21:19:00 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SenderThread::Main()
|
|
|
|
|
{
|
|
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
if (!m_curPacket)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
2007-11-20 23:58:24 +00:00
|
|
|
if (!m_sendQueue.empty())
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
m_curPacket = m_sendQueue.front();
|
2007-11-20 23:58:24 +00:00
|
|
|
m_sendQueue.pop_front();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
|
2009-01-07 21:52:16 +00:00
|
|
|
if (m_curPacket)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
const unsigned tmpLen = m_curPacket->GetLen();
|
|
|
|
|
if (tmpLen > MAX_PACKET_SIZE)
|
|
|
|
|
m_curPacket.reset(); // TODO log
|
|
|
|
|
else
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
SOCKET tmpSocket;
|
|
|
|
|
{
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
tmpSocket = m_sessionSocket;
|
2009-01-07 21:52:16 +00:00
|
|
|
}
|
2007-11-20 23:58:24 +00:00
|
|
|
|
|
|
|
|
// send next chunk of data
|
2009-01-07 21:52:16 +00:00
|
|
|
int bytesSent = send(tmpSocket, ((const char *)m_curPacket->GetRawData()) + m_bytesSent, tmpLen - m_bytesSent, SOCKET_SEND_FLAGS);
|
2007-11-20 23:58:24 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SELECT_FAILED, errCode);
|
2007-11-20 23:58:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-01-07 21:52:16 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
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)
|
2009-01-07 21:52:16 +00:00
|
|
|
{
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SEND_FAILED, errCode);
|
2009-01-07 21:52:16 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-01-07 21:52:16 +00:00
|
|
|
else if ((unsigned)bytesSent + m_bytesSent < tmpLen)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2007-11-20 23:58:24 +00:00
|
|
|
if (bytesSent)
|
|
|
|
|
{
|
2009-01-07 21:52:16 +00:00
|
|
|
// Send was partly successful.
|
|
|
|
|
m_bytesSent += bytesSent;
|
2007-11-20 23:58:24 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-01-07 21:52:16 +00:00
|
|
|
else //if ((unsigned)bytesSent + m_bytesSent == tmpLen)
|
|
|
|
|
{
|
|
|
|
|
m_curPacket.reset();
|
|
|
|
|
m_bytesSent = 0;
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-11-21 00:26:14 +00:00
|
|
|
else
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-01-11 20:12:26 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
|
|
|
|
if (m_sessionSocket != INVALID_SOCKET)
|
|
|
|
|
CLOSESOCKET(m_sessionSocket);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|