2007-11-16 15:24:25 +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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
/* Network sender thread. */
|
|
|
|
|
|
|
|
|
|
#ifndef _SENDERTHREAD_H_
|
|
|
|
|
#define _SENDERTHREAD_H_
|
|
|
|
|
|
|
|
|
|
#include <core/thread.h>
|
2009-01-07 19:37:05 +00:00
|
|
|
#include <net/senderinterface.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/netpacket.h>
|
|
|
|
|
#include <net/sendercallback.h>
|
|
|
|
|
|
2007-11-20 23:58:24 +00:00
|
|
|
#include <list>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
2009-01-11 20:12:26 +00:00
|
|
|
class SessionData;
|
2007-11-16 15:24:25 +00:00
|
|
|
#define SENDER_THREAD_TERMINATE_TIMEOUT THREAD_WAIT_INFINITE
|
|
|
|
|
|
2009-01-07 19:37:05 +00:00
|
|
|
class SenderThread : public Thread, public SenderInterface
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2009-01-26 20:57:39 +00:00
|
|
|
SenderThread(SenderCallback &cb);
|
2007-11-16 15:24:25 +00:00
|
|
|
virtual ~SenderThread();
|
|
|
|
|
|
2009-01-07 19:37:05 +00:00
|
|
|
virtual void Start();
|
|
|
|
|
virtual void SignalStop();
|
|
|
|
|
virtual void WaitStop();
|
|
|
|
|
|
|
|
|
|
virtual void Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
|
|
|
|
|
virtual void Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-01-26 20:57:39 +00:00
|
|
|
boost::shared_ptr<boost::asio::io_service> GetIOService();
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
protected:
|
2009-01-07 21:52:16 +00:00
|
|
|
typedef std::list<boost::shared_ptr<NetPacket> > SendDataList;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
class SendDataManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
2009-01-25 21:56:43 +00:00
|
|
|
SendDataManager(boost::shared_ptr<SessionData> s)
|
2009-01-25 20:19:09 +00:00
|
|
|
: session(s), m_writeInProgress(false), m_completed(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleWrite(const boost::system::error_code& error);
|
|
|
|
|
|
|
|
|
|
bool IsWriteInProgress() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
return m_writeInProgress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetWriteInProgress(bool v)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
m_writeInProgress = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsCompleted() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
return m_completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetCompleted(bool v)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
m_completed = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<SessionData> session;
|
|
|
|
|
SendDataList list;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
mutable boost::mutex m_mutex;
|
|
|
|
|
bool m_writeInProgress;
|
|
|
|
|
bool m_completed;
|
|
|
|
|
};
|
|
|
|
|
typedef std::map<SessionId, boost::shared_ptr<SendDataManager> > SendQueueMap;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
// Main function of the thread.
|
|
|
|
|
virtual void Main();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
SendQueueMap m_sendQueueMap;
|
|
|
|
|
mutable boost::mutex m_sendQueueMapMutex;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
SenderCallback &m_callback;
|
2009-01-26 20:57:39 +00:00
|
|
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
|
|
|
|
|
|
|
|
|
mutable boost::shared_ptr<boost::barrier> m_ioServiceBarrier;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|