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)
|
||||
{
|
||||
if (packet && session) {
|
||||
boost::shared_ptr<SendDataManager> tmpManager;
|
||||
SendDataManager &tmpManager = session->GetSendDataManager();
|
||||
{
|
||||
// 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())
|
||||
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);
|
||||
// First: 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.
|
||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
||||
// Second: Activate async send, if needed.
|
||||
tmpManager.AsyncSendNextPacket(session->GetAsioSocket());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,45 +60,27 @@ void
|
||||
SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &packetList)
|
||||
{
|
||||
if (!packetList.empty() && session) {
|
||||
boost::shared_ptr<SendDataManager> tmpManager;
|
||||
SendDataManager &tmpManager = session->GetSendDataManager();
|
||||
{
|
||||
// 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())
|
||||
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) {
|
||||
// First: 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 end = packetList.end();
|
||||
while (i != end) {
|
||||
if (*i)
|
||||
InternalStorePacket(*tmpManager, *i);
|
||||
InternalStorePacket(tmpManager, *i);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
// Third: Activate async send, if needed.
|
||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
||||
// Second: Activate async send, if needed.
|
||||
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
|
||||
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
|
||||
// returning 0 afterwards, and we will handle this error.
|
||||
}
|
||||
virtual void SignalSessionTerminated(unsigned session) {
|
||||
m_server.GetSender().SignalSessionTerminated(session);
|
||||
virtual void SignalSessionTerminated(unsigned /*session*/) {
|
||||
}
|
||||
|
||||
virtual void SignalChatBotMessage(const string &msg) {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
#include <net/senddatamanager.h>
|
||||
#include <gsasl.h>
|
||||
|
||||
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_callback(cb), m_authSession(NULL), m_curAuthStep(0)
|
||||
{
|
||||
m_sendDataManager.reset(new SendDataManager);
|
||||
}
|
||||
|
||||
SessionData::~SessionData()
|
||||
@@ -232,13 +234,6 @@ SessionData::SetClientAddr(const std::string &addr)
|
||||
m_clientAddr = addr;
|
||||
}
|
||||
|
||||
ReceiveBuffer &
|
||||
SessionData::GetReceiveBuffer()
|
||||
{
|
||||
// mutex protection, if needed, within buffer.
|
||||
return m_receiveBuffer;
|
||||
}
|
||||
|
||||
void
|
||||
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, const NetPacketList &packetList);
|
||||
|
||||
void SignalSessionTerminated(unsigned sessionId);
|
||||
|
||||
protected:
|
||||
void InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr<NetPacket> packet);
|
||||
|
||||
typedef std::map<SessionId, boost::shared_ptr<SendDataManager> > SendQueueMap;
|
||||
|
||||
private:
|
||||
|
||||
SenderCallback &m_callback;
|
||||
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
||||
|
||||
SendQueueMap m_sendQueueMap;
|
||||
mutable boost::mutex m_sendQueueMapMutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+10
-1
@@ -38,6 +38,7 @@ typedef unsigned SessionId;
|
||||
|
||||
struct Gsasl;
|
||||
struct Gsasl_session;
|
||||
class SendDataManager;
|
||||
|
||||
class SessionData
|
||||
{
|
||||
@@ -76,7 +77,12 @@ public:
|
||||
const std::string &GetClientAddr() const;
|
||||
void SetClientAddr(const std::string &addr);
|
||||
|
||||
ReceiveBuffer &GetReceiveBuffer();
|
||||
ReceiveBuffer &GetReceiveBuffer() {
|
||||
return m_receiveBuffer;
|
||||
}
|
||||
SendDataManager &GetSendDataManager() {
|
||||
return *m_sendDataManager;
|
||||
}
|
||||
|
||||
void ResetActivityTimer();
|
||||
unsigned GetActivityTimerElapsedSec() const;
|
||||
@@ -85,6 +91,8 @@ public:
|
||||
unsigned GetAutoDisconnectTimerElapsedSec() const;
|
||||
|
||||
protected:
|
||||
SessionData(const SessionData &other);
|
||||
SessionData &operator=(const SessionData &other);
|
||||
void InternalClearAuthSession();
|
||||
|
||||
private:
|
||||
@@ -94,6 +102,7 @@ private:
|
||||
State m_state;
|
||||
std::string m_clientAddr;
|
||||
ReceiveBuffer m_receiveBuffer;
|
||||
boost::shared_ptr<SendDataManager> m_sendDataManager;
|
||||
bool m_readyFlag;
|
||||
bool m_wantsLobbyMsg;
|
||||
boost::timers::portable::microsec_timer m_activityTimer;
|
||||
|
||||
Reference in New Issue
Block a user