More work on sending/receiving avatars. Introduces low priority send queue, so that the server send thread is not stalled when sending loads of avatars. Not functional yet.

This commit is contained in:
lotodore
2007-10-06 12:50:19 +00:00
parent 7dd1013396
commit 3bb28de5ae
7 changed files with 147 additions and 25 deletions
+11 -1
View File
@@ -32,6 +32,8 @@
#include <game.h>
#include <playerinterface.h>
#include <boost/bind.hpp>
#include <sstream>
using namespace std;
@@ -529,10 +531,18 @@ ClientStateWaitSession::InternalProcess(ClientThread &client, boost::shared_ptr<
}
else if (packet->ToNetPacketRetrieveAvatar())
{
// Before letting us join the lobby, the server requests our avatar.
NetPacketRetrieveAvatar::Data retrieveAvatarData;
packet->ToNetPacketRetrieveAvatar()->GetData(retrieveAvatarData);
// Before letting us join the lobby, the server requests our avatar.
NetPacketList tmpList;
if (client.GetAvatarManager().AvatarFileToNetPackets(
client.GetContext().GetAvatarFile(),
retrieveAvatarData.requestId,
tmpList))
{
client.GetSender().SendLowPrio(client.GetContext().GetSocket(), tmpList);
}
}
return retVal;
+69 -3
View File
@@ -22,6 +22,8 @@
#include <net/socket_msg.h>
#include <cstring>
#include <boost/bind.hpp>
using namespace std;
@@ -40,12 +42,64 @@ SenderThread::Send(SOCKET sock, boost::shared_ptr<NetPacket> packet)
if (packet.get() && IS_VALID_SOCKET(sock))
{
boost::mutex::scoped_lock lock(m_outBufMutex);
if (m_outBuf.size() < SEND_QUEUE_SIZE) // Queue is limited in size.
m_outBuf.push_back(std::make_pair(packet, sock));
// TODO: Throw exception if failed.
InternalStore(m_outBuf, SEND_QUEUE_SIZE, sock, packet);
}
}
void
SenderThread::Send(SOCKET sock, const NetPacketList &packetList)
{
if (!packetList.empty() && IS_VALID_SOCKET(sock))
{
boost::mutex::scoped_lock lock(m_outBufMutex);
InternalStore(m_outBuf, SEND_QUEUE_SIZE, sock, packetList);
}
}
void
SenderThread::SendLowPrio(SOCKET sock, boost::shared_ptr<NetPacket> packet)
{
if (packet.get() && IS_VALID_SOCKET(sock))
{
boost::mutex::scoped_lock lock(m_lowPrioOutBufMutex);
InternalStore(m_lowPrioOutBuf, SEND_LOW_PRIO_QUEUE_SIZE, sock, packet);
}
}
void
SenderThread::SendLowPrio(SOCKET sock, const NetPacketList &packetList)
{
if (!packetList.empty() && IS_VALID_SOCKET(sock))
{
boost::mutex::scoped_lock lock(m_lowPrioOutBufMutex);
InternalStore(m_lowPrioOutBuf, SEND_LOW_PRIO_QUEUE_SIZE, sock, packetList);
}
}
void
SenderThread::InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, SOCKET sock, boost::shared_ptr<NetPacket> packet)
{
if (sendQueue.size() < maxQueueSize) // Queue is limited in size.
sendQueue.push_back(std::make_pair(packet, sock));
// TODO: Throw exception if failed.
}
void
SenderThread::InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, SOCKET sock, const NetPacketList &packetList)
{
if (sendQueue.size() + packetList.size() < maxQueueSize)
{
NetPacketList::const_iterator i = packetList.begin();
NetPacketList::const_iterator end = packetList.end();
while (i != end)
{
sendQueue.push_back(std::make_pair(*i, sock));
++i;
}
}
// TODO: Throw exception if failed.
}
void
SenderThread::Main()
{
@@ -57,6 +111,7 @@ SenderThread::Main()
if (!m_tmpOutBufSize)
{
SendData tmpData;
// Check main queue first.
{
boost::mutex::scoped_lock lock(m_outBufMutex);
if (!m_outBuf.empty())
@@ -66,6 +121,17 @@ SenderThread::Main()
}
}
// 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();
}
}
if (tmpData.first.get())
{
if (IS_VALID_SOCKET(tmpData.second))