Further simplifying sending network data. This improves performance, because sending sessions no longer need to be looked up in a map.
This commit is contained in:
@@ -41,25 +41,17 @@ void
|
|||||||
SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
|
SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
|
||||||
{
|
{
|
||||||
if (packet && session) {
|
if (packet && session) {
|
||||||
boost::shared_ptr<SendDataManager> tmpManager;
|
SendDataManager &tmpManager = session->GetSendDataManager();
|
||||||
{
|
{
|
||||||
// First: lock map of all queues. Locate/insert queue.
|
// First: Add packet to specific queue.
|
||||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
boost::mutex::scoped_lock lock(tmpManager.dataMutex);
|
||||||
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
||||||
if (pos == m_sendQueueMap.end())
|
InternalStorePacket(tmpManager, packet);
|
||||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
|
|
||||||
tmpManager = pos->second;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
// Second: Add packet to specific queue.
|
|
||||||
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
|
|
||||||
if (tmpManager->GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
|
||||||
InternalStorePacket(*tmpManager, packet);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// Third: Activate async send, if needed.
|
// Second: Activate async send, if needed.
|
||||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,45 +60,27 @@ void
|
|||||||
SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
||||||
{
|
{
|
||||||
if (!packetList.empty() && session) {
|
if (!packetList.empty() && session) {
|
||||||
boost::shared_ptr<SendDataManager> tmpManager;
|
SendDataManager &tmpManager = session->GetSendDataManager();
|
||||||
{
|
{
|
||||||
// First: lock map of all queues. Locate/insert queue.
|
// First: Add packets to specific queue.
|
||||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
boost::mutex::scoped_lock lock(tmpManager.dataMutex);
|
||||||
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
if (tmpManager.GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
||||||
if (pos == m_sendQueueMap.end())
|
|
||||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
|
|
||||||
tmpManager = pos->second;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
// Second: Add packets to specific queue.
|
|
||||||
boost::mutex::scoped_lock lock(tmpManager->dataMutex);
|
|
||||||
if (tmpManager->GetAllocated() <= MAX_SEND_BUF_SIZE) {
|
|
||||||
NetPacketList::const_iterator i = packetList.begin();
|
NetPacketList::const_iterator i = packetList.begin();
|
||||||
NetPacketList::const_iterator end = packetList.end();
|
NetPacketList::const_iterator end = packetList.end();
|
||||||
while (i != end) {
|
while (i != end) {
|
||||||
if (*i)
|
if (*i)
|
||||||
InternalStorePacket(*tmpManager, *i);
|
InternalStorePacket(tmpManager, *i);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// Third: Activate async send, if needed.
|
// Second: Activate async send, if needed.
|
||||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
SenderHelper::SignalSessionTerminated(unsigned sessionId)
|
|
||||||
{
|
|
||||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
|
||||||
|
|
||||||
SendQueueMap::iterator pos = m_sendQueueMap.find(sessionId);
|
|
||||||
if (pos != m_sendQueueMap.end())
|
|
||||||
m_sendQueueMap.erase(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SenderHelper::InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet)
|
SenderHelper::InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -94,8 +94,7 @@ public:
|
|||||||
// A serious send error should trigger a read error or a read
|
// A serious send error should trigger a read error or a read
|
||||||
// returning 0 afterwards, and we will handle this error.
|
// returning 0 afterwards, and we will handle this error.
|
||||||
}
|
}
|
||||||
virtual void SignalSessionTerminated(unsigned session) {
|
virtual void SignalSessionTerminated(unsigned /*session*/) {
|
||||||
m_server.GetSender().SignalSessionTerminated(session);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void SignalChatBotMessage(const string &msg) {
|
virtual void SignalChatBotMessage(const string &msg) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include <net/sessiondata.h>
|
#include <net/sessiondata.h>
|
||||||
|
#include <net/senddatamanager.h>
|
||||||
#include <gsasl.h>
|
#include <gsasl.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -29,6 +30,7 @@ SessionData::SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, S
|
|||||||
m_autoDisconnectTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start),
|
m_autoDisconnectTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start),
|
||||||
m_callback(cb), m_authSession(NULL), m_curAuthStep(0)
|
m_callback(cb), m_authSession(NULL), m_curAuthStep(0)
|
||||||
{
|
{
|
||||||
|
m_sendDataManager.reset(new SendDataManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionData::~SessionData()
|
SessionData::~SessionData()
|
||||||
@@ -232,13 +234,6 @@ SessionData::SetClientAddr(const std::string &addr)
|
|||||||
m_clientAddr = addr;
|
m_clientAddr = addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReceiveBuffer &
|
|
||||||
SessionData::GetReceiveBuffer()
|
|
||||||
{
|
|
||||||
// mutex protection, if needed, within buffer.
|
|
||||||
return m_receiveBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SessionData::ResetActivityTimer()
|
SessionData::ResetActivityTimer()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -36,20 +36,13 @@ public:
|
|||||||
void Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
|
void Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
|
||||||
void Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList);
|
void Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList);
|
||||||
|
|
||||||
void SignalSessionTerminated(unsigned sessionId);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet);
|
void InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet);
|
||||||
|
|
||||||
typedef std::map<SessionId, boost::shared_ptr<SendDataManager> > SendQueueMap;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SenderCallback &m_callback;
|
SenderCallback &m_callback;
|
||||||
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
||||||
|
|
||||||
SendQueueMap m_sendQueueMap;
|
|
||||||
mutable boost::mutex m_sendQueueMapMutex;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+10
-1
@@ -38,6 +38,7 @@ typedef unsigned SessionId;
|
|||||||
|
|
||||||
struct Gsasl;
|
struct Gsasl;
|
||||||
struct Gsasl_session;
|
struct Gsasl_session;
|
||||||
|
class SendDataManager;
|
||||||
|
|
||||||
class SessionData
|
class SessionData
|
||||||
{
|
{
|
||||||
@@ -76,7 +77,12 @@ public:
|
|||||||
const std::string &GetClientAddr() const;
|
const std::string &GetClientAddr() const;
|
||||||
void SetClientAddr(const std::string &addr);
|
void SetClientAddr(const std::string &addr);
|
||||||
|
|
||||||
ReceiveBuffer &GetReceiveBuffer();
|
ReceiveBuffer &GetReceiveBuffer() {
|
||||||
|
return m_receiveBuffer;
|
||||||
|
}
|
||||||
|
SendDataManager &GetSendDataManager() {
|
||||||
|
return *m_sendDataManager;
|
||||||
|
}
|
||||||
|
|
||||||
void ResetActivityTimer();
|
void ResetActivityTimer();
|
||||||
unsigned GetActivityTimerElapsedSec() const;
|
unsigned GetActivityTimerElapsedSec() const;
|
||||||
@@ -85,6 +91,8 @@ public:
|
|||||||
unsigned GetAutoDisconnectTimerElapsedSec() const;
|
unsigned GetAutoDisconnectTimerElapsedSec() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
SessionData(const SessionData &other);
|
||||||
|
SessionData &operator=(const SessionData &other);
|
||||||
void InternalClearAuthSession();
|
void InternalClearAuthSession();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -94,6 +102,7 @@ private:
|
|||||||
State m_state;
|
State m_state;
|
||||||
std::string m_clientAddr;
|
std::string m_clientAddr;
|
||||||
ReceiveBuffer m_receiveBuffer;
|
ReceiveBuffer m_receiveBuffer;
|
||||||
|
boost::shared_ptr<SendDataManager> m_sendDataManager;
|
||||||
bool m_readyFlag;
|
bool m_readyFlag;
|
||||||
bool m_wantsLobbyMsg;
|
bool m_wantsLobbyMsg;
|
||||||
boost::timers::portable::microsec_timer m_activityTimer;
|
boost::timers::portable::microsec_timer m_activityTimer;
|
||||||
|
|||||||
Reference in New Issue
Block a user