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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
#include <boost/asio.hpp>
|
2009-06-20 11:15:06 +00:00
|
|
|
#include <boost/bind.hpp>
|
2009-08-02 15:11:40 +00:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2011-02-18 18:36:30 +00:00
|
|
|
#include <boost/swap.hpp>
|
2009-06-20 11:15:06 +00:00
|
|
|
|
2009-05-10 22:21:20 +00:00
|
|
|
#include <net/senderhelper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/sendercallback.h>
|
2009-06-20 11:15:06 +00:00
|
|
|
#include <net/socket_helper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
#include <core/loghelper.h>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-02-18 18:36:30 +00:00
|
|
|
#define SEND_BUF_FIRST_ALLOC_CHUNKSIZE 4096
|
|
|
|
|
#define MAX_SEND_BUF_SIZE SEND_BUF_FIRST_ALLOC_CHUNKSIZE * 1000
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
|
2009-06-20 11:15:06 +00:00
|
|
|
class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
|
2009-01-31 21:03:34 +00:00
|
|
|
{
|
2011-02-11 20:02:08 +00:00
|
|
|
public:
|
|
|
|
|
SendDataManager()
|
2011-02-18 18:36:30 +00:00
|
|
|
: sendBuf(NULL), curWriteBuf(NULL), sendBufAllocated(0), sendBufUsed(0),
|
|
|
|
|
curWriteBufAllocated(0), curWriteBufUsed(0)
|
|
|
|
|
{
|
2011-02-11 20:02:08 +00:00
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
|
2011-02-18 18:36:30 +00:00
|
|
|
~SendDataManager()
|
|
|
|
|
{
|
|
|
|
|
free(sendBuf);
|
|
|
|
|
free(curWriteBuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_t GetSendBufLeft() const
|
|
|
|
|
{
|
|
|
|
|
int bytesLeft = sendBufAllocated - sendBufUsed;
|
|
|
|
|
return bytesLeft < 0 ? (size_t)0 : (size_t)bytesLeft;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_t GetAllocated() const
|
|
|
|
|
{
|
|
|
|
|
return sendBufAllocated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool ReallocSendBuf()
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
size_t allocAmount = sendBufAllocated * 2;
|
|
|
|
|
if (0 == allocAmount) {
|
|
|
|
|
allocAmount = (size_t)SEND_BUF_FIRST_ALLOC_CHUNKSIZE;
|
|
|
|
|
}
|
|
|
|
|
char *tempBuf = (char *)realloc(sendBuf, allocAmount);
|
|
|
|
|
if (tempBuf) {
|
|
|
|
|
sendBuf = tempBuf;
|
|
|
|
|
sendBufAllocated = allocAmount;
|
|
|
|
|
retVal = true;
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void AppendToSendBufWithoutCheck(const char *data, size_t size)
|
|
|
|
|
{
|
|
|
|
|
memcpy(sendBuf + sendBufUsed, data, size);
|
|
|
|
|
sendBufUsed += size;
|
2011-02-11 20:02:08 +00:00
|
|
|
}
|
2009-06-08 21:54:24 +00:00
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
|
2009-01-31 21:03:34 +00:00
|
|
|
|
2011-02-18 18:36:30 +00:00
|
|
|
void AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
2009-01-31 21:03:34 +00:00
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
mutable boost::mutex dataMutex;
|
2011-02-18 18:36:30 +00:00
|
|
|
|
|
|
|
|
private:
|
2011-02-11 20:02:08 +00:00
|
|
|
char *sendBuf;
|
2011-02-18 18:36:30 +00:00
|
|
|
char *curWriteBuf;
|
|
|
|
|
size_t sendBufAllocated;
|
|
|
|
|
size_t sendBufUsed;
|
|
|
|
|
size_t curWriteBufAllocated;
|
|
|
|
|
size_t curWriteBufUsed;
|
2009-01-31 21:03:34 +00:00
|
|
|
};
|
|
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
|
|
|
|
|
void
|
2009-06-09 23:04:04 +00:00
|
|
|
SendDataManager::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error)
|
2009-01-25 20:19:09 +00:00
|
|
|
{
|
2011-02-18 18:36:30 +00:00
|
|
|
if (!error) {
|
|
|
|
|
// Successfully sent the data.
|
|
|
|
|
curWriteBufUsed = 0;
|
|
|
|
|
// Send more data, if available.
|
|
|
|
|
AsyncSendNextPacket(socket);
|
|
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-02-18 18:36:30 +00:00
|
|
|
SendDataManager::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
2009-01-31 21:03:34 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(dataMutex);
|
2011-02-18 18:36:30 +00:00
|
|
|
if (!curWriteBufUsed) {
|
|
|
|
|
// Swap buffers and send data.
|
|
|
|
|
boost::swap(curWriteBuf, sendBuf);
|
|
|
|
|
boost::swap(curWriteBufAllocated, sendBufAllocated);
|
|
|
|
|
boost::swap(curWriteBufUsed, sendBufUsed);
|
|
|
|
|
if (curWriteBufUsed) {
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::asio::async_write(
|
2009-01-31 22:20:08 +00:00
|
|
|
*socket,
|
2011-02-18 18:36:30 +00:00
|
|
|
boost::asio::buffer(curWriteBuf, curWriteBufUsed),
|
2009-06-09 23:04:04 +00:00
|
|
|
boost::bind(&SendDataManager::HandleWrite,
|
2011-02-11 20:02:08 +00:00
|
|
|
shared_from_this(),
|
|
|
|
|
socket,
|
|
|
|
|
boost::asio::placeholders::error));
|
2011-02-18 18:36:30 +00:00
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
2009-01-25 20:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-10 22:21:20 +00:00
|
|
|
SenderHelper::SenderHelper(SenderCallback &cb, boost::shared_ptr<boost::asio::io_service> ioService)
|
2011-02-11 20:02:08 +00:00
|
|
|
: m_callback(cb), m_ioService(ioService)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-10 22:21:20 +00:00
|
|
|
SenderHelper::~SenderHelper()
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-05-10 22:21:20 +00:00
|
|
|
SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2011-02-11 20:02:08 +00:00
|
|
|
if (packet && session) {
|
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-06-09 23:04:04 +00:00
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).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);
|
2011-02-18 18:36:30 +00:00
|
|
|
if (tmpManager->GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
2009-08-02 15:11:40 +00:00
|
|
|
InternalStorePacket(*tmpManager, packet);
|
2009-06-07 08:51:43 +00:00
|
|
|
}
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
{
|
2009-06-07 08:51:43 +00:00
|
|
|
// Third: Activate async send, if needed.
|
2009-06-09 23:04:04 +00:00
|
|
|
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
2009-01-31 21:03:34 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-05-10 22:21:20 +00:00
|
|
|
SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!packetList.empty() && session) {
|
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-06-09 23:04:04 +00:00
|
|
|
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
|
2009-01-31 21:03:34 +00:00
|
|
|
tmpManager = pos->second;
|
|
|
|
|
}
|
|
|
|
|
{
|
2009-06-07 08:51:43 +00:00
|
|
|
// Second: Add packets to specific queue.
|
2009-01-31 21:03:34 +00:00
|
|
|
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
|
2011-02-18 18:36:30 +00:00
|
|
|
if (tmpManager->GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
2009-01-31 21:03:34 +00:00
|
|
|
NetPacketList::const_iterator i = packetList.begin();
|
|
|
|
|
NetPacketList::const_iterator end = packetList.end();
|
2011-02-11 20:02:08 +00:00
|
|
|
while (i != end) {
|
2009-07-22 20:19:56 +00:00
|
|
|
if (*i)
|
2009-08-02 15:11:40 +00:00
|
|
|
InternalStorePacket(*tmpManager, *i);
|
2009-01-31 21:03:34 +00:00
|
|
|
++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
|
|
|
{
|
2009-06-07 08:51:43 +00:00
|
|
|
// Third: Activate async send, if needed.
|
2009-06-09 23:04:04 +00:00
|
|
|
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
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
|
2009-05-10 22:21:20 +00:00
|
|
|
SenderHelper::SignalSessionTerminated(unsigned sessionId)
|
2009-01-26 20:57:39 +00:00
|
|
|
{
|
2009-06-07 08:51:43 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
2009-01-26 20:57:39 +00:00
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
SendQueueMap::iterator pos = m_sendQueueMap.find(sessionId);
|
|
|
|
|
if (pos != m_sendQueueMap.end())
|
|
|
|
|
m_sendQueueMap.erase(pos);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-18 18:36:30 +00:00
|
|
|
static int der_encode_to_buffer_cb(const void *data, size_t size, void *arg) {
|
|
|
|
|
SendDataManager *m = (SendDataManager *)arg;
|
|
|
|
|
|
|
|
|
|
// Realloc buffer if necessary.
|
|
|
|
|
while (m->GetSendBufLeft() < size) {
|
|
|
|
|
if (!m->ReallocSendBuf()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m->AppendToSendBufWithoutCheck((const char*)data, size);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
void
|
|
|
|
|
SenderHelper::InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet)
|
|
|
|
|
{
|
2011-02-18 18:36:30 +00:00
|
|
|
asn_enc_rval_t e = der_encode(&asn_DEF_PokerTHMessage, packet->GetMsg(), der_encode_to_buffer_cb, &tmpManager);
|
2009-08-03 20:42:32 +00:00
|
|
|
//cerr << "OUT:" << endl << packet->ToString() << endl;
|
2009-08-02 15:11:40 +00:00
|
|
|
if (e.encoded == -1)
|
|
|
|
|
LOG_ERROR("Failed to encode NetPacket: " << packet->GetMsg()->present);
|
|
|
|
|
}
|
|
|
|
|
|