Removing a buffered delay when the client sends data (no longer necessary with asio).
This commit is contained in:
@@ -107,8 +107,7 @@ protected:
|
|||||||
void CancelTimers();
|
void CancelTimers();
|
||||||
void InitGame();
|
void InitGame();
|
||||||
|
|
||||||
void AddPacket(boost::shared_ptr<NetPacket> packet);
|
void SendPacket(boost::shared_ptr<NetPacket> packet);
|
||||||
void TimerSendPacketLoop(const boost::system::error_code &ec);
|
|
||||||
|
|
||||||
bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const;
|
bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const;
|
||||||
void RequestPlayerInfo(unsigned id, bool requestAvatar = false);
|
void RequestPlayerInfo(unsigned id, bool requestAvatar = false);
|
||||||
@@ -194,7 +193,6 @@ private:
|
|||||||
boost::shared_ptr<ClientSenderCallback> m_senderCallback;
|
boost::shared_ptr<ClientSenderCallback> m_senderCallback;
|
||||||
|
|
||||||
NetPacketList m_outPacketList;
|
NetPacketList m_outPacketList;
|
||||||
mutable boost::mutex m_outPacketListMutex;
|
|
||||||
|
|
||||||
boost::shared_ptr<ClientContext> m_context;
|
boost::shared_ptr<ClientContext> m_context;
|
||||||
ClientState *m_curState;
|
ClientState *m_curState;
|
||||||
@@ -246,7 +244,6 @@ private:
|
|||||||
|
|
||||||
boost::asio::deadline_timer m_stateTimer;
|
boost::asio::deadline_timer m_stateTimer;
|
||||||
boost::asio::deadline_timer m_avatarTimer;
|
boost::asio::deadline_timer m_avatarTimer;
|
||||||
boost::asio::deadline_timer m_sendTimer;
|
|
||||||
|
|
||||||
friend class AbstractClientStateReceiving;
|
friend class AbstractClientStateReceiving;
|
||||||
friend class ClientStateInit;
|
friend class ClientStateInit;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager)
|
|||||||
: m_ioService(new boost::asio::io_service), m_curState(NULL), m_gui(gui),
|
: m_ioService(new boost::asio::io_service), m_curState(NULL), m_gui(gui),
|
||||||
m_avatarManager(avatarManager), m_isServerSelected(false),
|
m_avatarManager(avatarManager), m_isServerSelected(false),
|
||||||
m_curGameId(0), m_curGameNum(1), m_guiPlayerId(0), m_sessionEstablished(false),
|
m_curGameId(0), m_curGameNum(1), m_guiPlayerId(0), m_sessionEstablished(false),
|
||||||
m_stateTimer(*m_ioService), m_avatarTimer(*m_ioService), m_sendTimer(*m_ioService)
|
m_stateTimer(*m_ioService), m_avatarTimer(*m_ioService)
|
||||||
{
|
{
|
||||||
m_context.reset(new ClientContext);
|
m_context.reset(new ClientContext);
|
||||||
m_receiver.reset(new ReceiverHelper);
|
m_receiver.reset(new ReceiverHelper);
|
||||||
@@ -123,16 +123,14 @@ ClientThread::SendKickPlayer(unsigned playerId)
|
|||||||
NetPacketKickPlayer::Data requestData;
|
NetPacketKickPlayer::Data requestData;
|
||||||
requestData.playerId = playerId;
|
requestData.playerId = playerId;
|
||||||
static_cast<NetPacketKickPlayer *>(request.get())->SetData(requestData);
|
static_cast<NetPacketKickPlayer *>(request.get())->SetData(requestData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), request));
|
||||||
m_outPacketList.push_back(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ClientThread::SendLeaveCurrentGame()
|
ClientThread::SendLeaveCurrentGame()
|
||||||
{
|
{
|
||||||
boost::shared_ptr<NetPacket> request(new NetPacketLeaveCurrentGame);
|
boost::shared_ptr<NetPacket> request(new NetPacketLeaveCurrentGame);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), request));
|
||||||
m_outPacketList.push_back(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -146,8 +144,7 @@ ClientThread::SendStartEvent(bool fillUpWithCpuPlayers)
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
static_cast<NetPacketStartEvent *>(start.get())->SetData(startData);
|
static_cast<NetPacketStartEvent *>(start.get())->SetData(startData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), start));
|
||||||
m_outPacketList.push_back(start);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendStartEvent: " << e.what());
|
LOG_ERROR("ClientThread::SendStartEvent: " << e.what());
|
||||||
@@ -173,8 +170,7 @@ ClientThread::SendPlayerAction()
|
|||||||
{
|
{
|
||||||
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
|
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
|
||||||
// Just dump the packet.
|
// Just dump the packet.
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), action));
|
||||||
m_outPacketList.push_back(action);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendPlayerAction: " << e.what());
|
LOG_ERROR("ClientThread::SendPlayerAction: " << e.what());
|
||||||
@@ -193,8 +189,7 @@ ClientThread::SendChatMessage(const std::string &msg)
|
|||||||
{
|
{
|
||||||
static_cast<NetPacketSendChatText *>(chat.get())->SetData(chatData);
|
static_cast<NetPacketSendChatText *>(chat.get())->SetData(chatData);
|
||||||
// Just dump the packet.
|
// Just dump the packet.
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), chat));
|
||||||
m_outPacketList.push_back(chat);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendChatMessage: " << e.what());
|
LOG_ERROR("ClientThread::SendChatMessage: " << e.what());
|
||||||
@@ -213,8 +208,7 @@ ClientThread::SendJoinFirstGame(const std::string &password)
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), join));
|
||||||
m_outPacketList.push_back(join);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendJoinFirstGame: " << e.what());
|
LOG_ERROR("ClientThread::SendJoinFirstGame: " << e.what());
|
||||||
@@ -233,8 +227,7 @@ ClientThread::SendJoinGame(unsigned gameId, const std::string &password)
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), join));
|
||||||
m_outPacketList.push_back(join);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendJoinGame: " << e.what());
|
LOG_ERROR("ClientThread::SendJoinGame: " << e.what());
|
||||||
@@ -254,8 +247,7 @@ ClientThread::SendCreateGame(const GameData &gameData, const std::string &name,
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
static_cast<NetPacketCreateGame *>(create.get())->SetData(createData);
|
static_cast<NetPacketCreateGame *>(create.get())->SetData(createData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), create));
|
||||||
m_outPacketList.push_back(create);
|
|
||||||
} catch (const NetException &e)
|
} catch (const NetException &e)
|
||||||
{
|
{
|
||||||
LOG_ERROR("ClientThread::SendCreateGame: " << e.what());
|
LOG_ERROR("ClientThread::SendCreateGame: " << e.what());
|
||||||
@@ -266,8 +258,7 @@ void
|
|||||||
ClientThread::SendResetTimeout()
|
ClientThread::SendResetTimeout()
|
||||||
{
|
{
|
||||||
boost::shared_ptr<NetPacket> reset(new NetPacketResetTimeout);
|
boost::shared_ptr<NetPacket> reset(new NetPacketResetTimeout);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), reset));
|
||||||
m_outPacketList.push_back(reset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -277,8 +268,7 @@ ClientThread::SendAskKickPlayer(unsigned playerId)
|
|||||||
NetPacketAskKickPlayer::Data askData;
|
NetPacketAskKickPlayer::Data askData;
|
||||||
askData.playerId = playerId;
|
askData.playerId = playerId;
|
||||||
static_cast<NetPacketAskKickPlayer *>(ask.get())->SetData(askData);
|
static_cast<NetPacketAskKickPlayer *>(ask.get())->SetData(askData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), ask));
|
||||||
m_outPacketList.push_back(ask);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -292,8 +282,7 @@ ClientThread::SendVoteKick(bool doKick)
|
|||||||
}
|
}
|
||||||
voteData.vote = doKick ? KICK_VOTE_IN_FAVOUR : KICK_VOTE_AGAINST;
|
voteData.vote = doKick ? KICK_VOTE_IN_FAVOUR : KICK_VOTE_AGAINST;
|
||||||
static_cast<NetPacketVoteKickPlayer *>(vote.get())->SetData(voteData);
|
static_cast<NetPacketVoteKickPlayer *>(vote.get())->SetData(voteData);
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
m_ioService->post(boost::bind(&ClientThread::SendPacket, shared_from_this(), vote));
|
||||||
m_outPacketList.push_back(vote);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -467,20 +456,12 @@ ClientThread::RegisterTimers()
|
|||||||
m_avatarTimer.async_wait(
|
m_avatarTimer.async_wait(
|
||||||
boost::bind(
|
boost::bind(
|
||||||
&ClientThread::TimerCheckAvatarDownloads, shared_from_this(), boost::asio::placeholders::error));
|
&ClientThread::TimerCheckAvatarDownloads, shared_from_this(), boost::asio::placeholders::error));
|
||||||
|
|
||||||
// TODO: send directly without additional buffer.
|
|
||||||
m_sendTimer.expires_from_now(
|
|
||||||
boost::posix_time::milliseconds(CLIENT_SEND_LOOP_MSEC));
|
|
||||||
m_sendTimer.async_wait(
|
|
||||||
boost::bind(
|
|
||||||
&ClientThread::TimerSendPacketLoop, shared_from_this(), boost::asio::placeholders::error));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ClientThread::CancelTimers()
|
ClientThread::CancelTimers()
|
||||||
{
|
{
|
||||||
m_avatarTimer.cancel();
|
m_avatarTimer.cancel();
|
||||||
m_sendTimer.cancel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -500,40 +481,27 @@ ClientThread::InitGame()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet)
|
ClientThread::SendPacket(boost::shared_ptr<NetPacket> packet)
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
// Put packets in a buffer until the session is established.
|
||||||
m_outPacketList.push_back(packet);
|
if (IsSessionEstablished())
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ClientThread::TimerSendPacketLoop(const boost::system::error_code &ec)
|
|
||||||
{
|
|
||||||
if (!ec)
|
|
||||||
{
|
{
|
||||||
if (IsSessionEstablished())
|
if (!m_outPacketList.empty())
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
NetPacketList::iterator i = m_outPacketList.begin();
|
||||||
|
NetPacketList::iterator end = m_outPacketList.end();
|
||||||
|
|
||||||
if (!m_outPacketList.empty())
|
while (i != end)
|
||||||
{
|
{
|
||||||
NetPacketList::iterator i = m_outPacketList.begin();
|
GetSender().Send(GetContext().GetSessionData(), *i);
|
||||||
NetPacketList::iterator end = m_outPacketList.end();
|
++i;
|
||||||
|
|
||||||
while (i != end)
|
|
||||||
{
|
|
||||||
GetSender().Send(GetContext().GetSessionData(), *i);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
m_outPacketList.clear();
|
|
||||||
}
|
}
|
||||||
|
m_outPacketList.clear();
|
||||||
}
|
}
|
||||||
m_sendTimer.expires_from_now(
|
GetSender().Send(GetContext().GetSessionData(), packet);
|
||||||
boost::posix_time::milliseconds(CLIENT_SEND_LOOP_MSEC));
|
|
||||||
m_sendTimer.async_wait(
|
|
||||||
boost::bind(
|
|
||||||
&ClientThread::TimerSendPacketLoop, shared_from_this(), boost::asio::placeholders::error));
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
m_outPacketList.push_back(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|||||||
Reference in New Issue
Block a user