2007-02-25 23:16:26 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/senderthread.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/sendercallback.h>
|
2007-03-13 02:23:12 +00:00
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
2007-10-06 12:50:19 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
2007-03-13 02:23:12 +00:00
|
|
|
using namespace std;
|
2007-02-25 23:16:26 +00:00
|
|
|
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
SenderThread::SenderThread(SenderCallback &cb)
|
2007-10-28 12:53:54 +00:00
|
|
|
: m_curSession(INVALID_SESSION), m_tmpOutBufSize(0), m_callback(cb)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SenderThread::~SenderThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::Send(SessionId session, boost::shared_ptr<NetPacket> packet)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
if (packet.get() && session != INVALID_SESSION)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outBufMutex);
|
2007-10-28 12:53:54 +00:00
|
|
|
InternalStore(m_outBuf, SEND_QUEUE_SIZE, session, packet);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-06 12:50:19 +00:00
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::Send(SessionId session, const NetPacketList &packetList)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
if (!packetList.empty() && session != INVALID_SESSION)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outBufMutex);
|
2007-10-28 12:53:54 +00:00
|
|
|
InternalStore(m_outBuf, SEND_QUEUE_SIZE, session, packetList);
|
2007-10-06 12:50:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::SendLowPrio(SessionId session, boost::shared_ptr<NetPacket> packet)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
if (packet.get() && session != INVALID_SESSION)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_lowPrioOutBufMutex);
|
2007-10-28 12:53:54 +00:00
|
|
|
InternalStore(m_lowPrioOutBuf, SEND_LOW_PRIO_QUEUE_SIZE, session, packet);
|
2007-10-06 12:50:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::SendLowPrio(SessionId session, const NetPacketList &packetList)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
if (!packetList.empty() && session != INVALID_SESSION)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_lowPrioOutBufMutex);
|
2007-10-28 12:53:54 +00:00
|
|
|
InternalStore(m_lowPrioOutBuf, SEND_LOW_PRIO_QUEUE_SIZE, session, packetList);
|
2007-10-06 12:50:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, SessionId session, boost::shared_ptr<NetPacket> packet)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
|
|
|
|
if (sendQueue.size() < maxQueueSize) // Queue is limited in size.
|
2007-10-28 12:53:54 +00:00
|
|
|
sendQueue.push_back(std::make_pair(packet, session));
|
2007-10-06 12:50:19 +00:00
|
|
|
// TODO: Throw exception if failed.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-10-28 12:53:54 +00:00
|
|
|
SenderThread::InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, SessionId session, const NetPacketList &packetList)
|
2007-10-06 12:50:19 +00:00
|
|
|
{
|
|
|
|
|
if (sendQueue.size() + packetList.size() < maxQueueSize)
|
|
|
|
|
{
|
|
|
|
|
NetPacketList::const_iterator i = packetList.begin();
|
|
|
|
|
NetPacketList::const_iterator end = packetList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
sendQueue.push_back(std::make_pair(*i, session));
|
2007-10-06 12:50:19 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: Throw exception if failed.
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-25 23:16:26 +00:00
|
|
|
void
|
|
|
|
|
SenderThread::Main()
|
|
|
|
|
{
|
|
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
2007-03-13 02:23:12 +00:00
|
|
|
// Send remaining bytes of output buffer OR
|
|
|
|
|
// copy ONE packet to output buffer.
|
|
|
|
|
// For reasons of simplicity, only one packet is sent at a time.
|
|
|
|
|
if (!m_tmpOutBufSize)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
SendData tmpData;
|
2007-10-06 12:50:19 +00:00
|
|
|
// Check main queue first.
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 02:23:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_outBufMutex);
|
|
|
|
|
if (!m_outBuf.empty())
|
|
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
tmpData = m_outBuf.front();
|
2007-03-13 02:23:12 +00:00
|
|
|
m_outBuf.pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-10-06 12:50:19 +00:00
|
|
|
// Check low prio queue only if there is nothing in the main queue.
|
|
|
|
|
if (!tmpData.first.get())
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_lowPrioOutBufMutex);
|
|
|
|
|
if (!m_lowPrioOutBuf.empty())
|
|
|
|
|
{
|
|
|
|
|
tmpData = m_lowPrioOutBuf.front();
|
|
|
|
|
m_lowPrioOutBuf.pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
if (tmpData.first.get())
|
2007-03-13 02:23:12 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
if (tmpData.second != INVALID_SESSION)
|
|
|
|
|
m_curSession = tmpData.second;
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-04-08 16:18:20 +00:00
|
|
|
u_int16_t tmpLen = tmpData.first->GetLen();
|
2007-03-13 02:23:12 +00:00
|
|
|
if (tmpLen <= MAX_PACKET_SIZE)
|
|
|
|
|
{
|
|
|
|
|
m_tmpOutBufSize = tmpLen;
|
2007-04-08 16:18:20 +00:00
|
|
|
memcpy(m_tmpOutBuf, tmpData.first->GetRawData(), tmpLen);
|
2007-03-13 02:23:12 +00:00
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-03-13 02:23:12 +00:00
|
|
|
if (m_tmpOutBufSize)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
SOCKET tmpSocket;
|
|
|
|
|
if (!m_callback.GetSocketForSession(m_curSession, tmpSocket))
|
2007-03-13 02:23:12 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
// Invalid session - skip.
|
|
|
|
|
m_tmpOutBufSize = 0;
|
|
|
|
|
m_curSession = INVALID_SESSION;
|
2007-03-13 02:23:12 +00:00
|
|
|
}
|
2007-10-28 12:53:54 +00:00
|
|
|
else
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
fd_set writeSet;
|
|
|
|
|
struct timeval timeout;
|
2007-03-13 02:23:12 +00:00
|
|
|
|
2007-10-28 12:53:54 +00:00
|
|
|
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-03-13 02:23:12 +00:00
|
|
|
{
|
2007-06-15 20:56:31 +00:00
|
|
|
// Never assume that this is a fatal error.
|
|
|
|
|
int errCode = SOCKET_ERRNO();
|
|
|
|
|
if (errCode != SOCKET_ERR_WOULDBLOCK)
|
|
|
|
|
{
|
|
|
|
|
// 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)
|
2007-10-28 12:53:54 +00:00
|
|
|
m_callback.SignalNetError(m_curSession, ERR_SOCK_SELECT_FAILED, errCode);
|
2007-06-15 20:56:31 +00:00
|
|
|
m_tmpOutBufSize = 0;
|
2007-10-28 12:53:54 +00:00
|
|
|
m_curSession = INVALID_SESSION;
|
2007-06-15 20:56:31 +00:00
|
|
|
}
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
2007-03-13 02:23:12 +00:00
|
|
|
}
|
2007-10-28 12:53:54 +00:00
|
|
|
if (selectResult > 0) // send is possible
|
2007-03-13 02:23:12 +00:00
|
|
|
{
|
2007-10-28 12:53:54 +00:00
|
|
|
// send next chunk of data
|
|
|
|
|
int bytesSent = send(tmpSocket, m_tmpOutBuf, m_tmpOutBufSize, 0);
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_SEND(bytesSent))
|
|
|
|
|
{
|
|
|
|
|
// Never assume that this is a fatal error.
|
|
|
|
|
int errCode = SOCKET_ERRNO();
|
|
|
|
|
if (errCode != SOCKET_ERR_WOULDBLOCK)
|
|
|
|
|
{
|
|
|
|
|
// 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(m_curSession, ERR_SOCK_SEND_FAILED, errCode);
|
|
|
|
|
m_tmpOutBufSize = 0;
|
|
|
|
|
m_curSession = INVALID_SESSION;
|
|
|
|
|
}
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
|
|
|
|
}
|
|
|
|
|
else if ((unsigned)bytesSent < m_tmpOutBufSize)
|
|
|
|
|
{
|
|
|
|
|
m_tmpOutBufSize -= (unsigned)bytesSent;
|
|
|
|
|
memmove(m_tmpOutBuf, m_tmpOutBuf + bytesSent, m_tmpOutBufSize);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_tmpOutBufSize = 0;
|
|
|
|
|
m_curSession = INVALID_SESSION;
|
|
|
|
|
}
|
2007-03-13 02:23:12 +00:00
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Msleep(SEND_TIMEOUT_MSEC);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|