Trying to fix strange timer crashes.
This commit is contained in:
@@ -38,6 +38,7 @@ TimerManager::RegisterTimer(unsigned timeoutMsec, boost::function<void()> timerH
|
||||
boost::shared_ptr<TimerData> data(new TimerData);
|
||||
data->timer.reset(
|
||||
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(timeoutMsec)));
|
||||
data->id = id;
|
||||
data->userHandler = timerHandler;
|
||||
data->durationMsec = timeoutMsec;
|
||||
data->autoRestart = autoRestart;
|
||||
@@ -48,23 +49,6 @@ TimerManager::RegisterTimer(unsigned timeoutMsec, boost::function<void()> timerH
|
||||
return id;
|
||||
}
|
||||
|
||||
bool
|
||||
TimerManager::AddTimer(unsigned timerId, unsigned timeoutMsec, boost::function<void()> timerHandler)
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
||||
bool restarted = false;
|
||||
TimerMap::iterator pos = m_timerMap.find(timerId);
|
||||
if (pos != m_timerMap.end())
|
||||
{
|
||||
pos->second->userHandler = timerHandler;
|
||||
pos->second->timer.reset(
|
||||
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(timeoutMsec)));
|
||||
pos->second->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, pos->second));
|
||||
restarted = true;
|
||||
}
|
||||
return restarted;
|
||||
}
|
||||
|
||||
bool
|
||||
TimerManager::UnregisterTimer(unsigned timerId)
|
||||
{
|
||||
@@ -94,6 +78,8 @@ TimerManager::Handler(const boost::system::error_code &ec, boost::shared_ptr<Tim
|
||||
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(data->durationMsec)));
|
||||
data->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, data));
|
||||
}
|
||||
else
|
||||
UnregisterTimer(data->id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,13 +32,13 @@ public:
|
||||
TimerManager(boost::shared_ptr<boost::asio::io_service> ioService);
|
||||
|
||||
unsigned RegisterTimer(unsigned timeoutMsec, boost::function<void()> timerHandler, bool autoRestart = false);
|
||||
bool AddTimer(unsigned timerId, unsigned timeoutMsec, boost::function<void()> timerHandler);
|
||||
bool UnregisterTimer(unsigned timerId);
|
||||
|
||||
protected:
|
||||
|
||||
struct TimerData
|
||||
{
|
||||
unsigned id;
|
||||
boost::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
boost::function<void()> userHandler;
|
||||
unsigned durationMsec;
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#include <cassert>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
|
||||
using namespace std;
|
||||
using boost::asio::ip::tcp;
|
||||
@@ -40,11 +39,11 @@ using boost::asio::ip::tcp;
|
||||
|
||||
typedef std::list<boost::shared_ptr<NetPacket> > SendDataList;
|
||||
|
||||
class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
|
||||
class SendDataManager
|
||||
{
|
||||
public:
|
||||
SendDataManager(boost::shared_ptr<boost::asio::ip::tcp::socket> s)
|
||||
: socket(s), sendBuf(NULL), writeInProgress(false)
|
||||
SendDataManager()
|
||||
: sendBuf(NULL), writeInProgress(false)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -53,11 +52,9 @@ class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
|
||||
delete[] sendBuf;
|
||||
}
|
||||
|
||||
void HandleWrite(const boost::system::error_code &error);
|
||||
void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
|
||||
|
||||
void AsyncSendNextPacket(bool handlerMode = false);
|
||||
|
||||
boost::shared_ptr<boost::asio::ip::tcp::socket> socket;
|
||||
void AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, bool handlerMode = false);
|
||||
|
||||
mutable boost::mutex dataMutex;
|
||||
SendDataList list;
|
||||
@@ -67,15 +64,15 @@ class SendDataManager : public boost::enable_shared_from_this<SendDataManager>
|
||||
|
||||
|
||||
void
|
||||
SendDataManager::HandleWrite(const boost::system::error_code &error)
|
||||
SendDataManager::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error)
|
||||
{
|
||||
// TODO error handling
|
||||
if (!error)
|
||||
AsyncSendNextPacket(true);
|
||||
AsyncSendNextPacket(socket, true);
|
||||
}
|
||||
|
||||
void
|
||||
SendDataManager::AsyncSendNextPacket(bool handlerMode)
|
||||
SendDataManager::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, bool handlerMode)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(dataMutex);
|
||||
if (!writeInProgress || handlerMode)
|
||||
@@ -108,7 +105,9 @@ SendDataManager::AsyncSendNextPacket(bool handlerMode)
|
||||
boost::asio::async_write(
|
||||
*socket,
|
||||
boost::asio::buffer(sendBuf, bufSize),
|
||||
boost::bind(&SendDataManager::HandleWrite, shared_from_this(),
|
||||
boost::bind(&SendDataManager::HandleWrite,
|
||||
this,
|
||||
socket,
|
||||
boost::asio::placeholders::error));
|
||||
writeInProgress = true;
|
||||
}
|
||||
@@ -137,7 +136,7 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
|
||||
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(session->GetAsioSocket())))).first;
|
||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
|
||||
tmpManager = pos->second;
|
||||
}
|
||||
{
|
||||
@@ -150,7 +149,7 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
|
||||
}
|
||||
{
|
||||
// Third: Activate async send, if needed.
|
||||
tmpManager->AsyncSendNextPacket();
|
||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,7 +165,7 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
|
||||
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(session->GetAsioSocket())))).first;
|
||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager))).first;
|
||||
tmpManager = pos->second;
|
||||
}
|
||||
{
|
||||
@@ -185,7 +184,7 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
|
||||
}
|
||||
{
|
||||
// Third: Activate async send, if needed.
|
||||
tmpManager->AsyncSendNextPacket();
|
||||
tmpManager->AsyncSendNextPacket(session->GetAsioSocket());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,10 +400,10 @@ ServerGameStateInit::TimerAdminWarning(ServerGame &server)
|
||||
server.GetLobbyThread().GetSender().Send(session.sessionData, warning);
|
||||
}
|
||||
// Start timeout timer.
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_GAME_ADMIN_WARNING_REMAINING_SEC * 1000,
|
||||
boost::bind(&ServerGameStateInit::TimerAdminTimeout, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_GAME_ADMIN_WARNING_REMAINING_SEC * 1000,
|
||||
boost::bind(&ServerGameStateInit::TimerAdminTimeout, this, boost::ref(server))));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -701,19 +701,19 @@ ServerGameStateHand::TimerLoop(ServerGame &server)
|
||||
server.SendToAllPlayers(allIn, SessionData::Game);
|
||||
curGame.getCurrentHand()->setCardsShown(true);
|
||||
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_SHOW_CARDS_DELAY_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_SHOW_CARDS_DELAY_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server))));
|
||||
}
|
||||
else
|
||||
{
|
||||
SendNewRoundCards(server, curGame, newRound);
|
||||
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
GetDealCardsDelaySec(server) * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
GetDealCardsDelaySec(server) * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server))));
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -741,19 +741,19 @@ ServerGameStateHand::TimerLoop(ServerGame &server)
|
||||
// If the player is computer controlled, let the engine act.
|
||||
if (curPlayer->getMyType() == PLAYER_TYPE_COMPUTER)
|
||||
{
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_COMPUTER_ACTION_DELAY_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerComputerAction, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_COMPUTER_ACTION_DELAY_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerComputerAction, this, boost::ref(server))));
|
||||
}
|
||||
// If the player we are waiting for left, continue without him.
|
||||
else if (!server.GetSessionManager().IsPlayerConnected(curPlayer->getMyName()))
|
||||
{
|
||||
PerformPlayerAction(server, curPlayer, PLAYER_ACTION_FOLD, 0);
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_LOOP_DELAY_MSEC,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_LOOP_DELAY_MSEC,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server))));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -833,17 +833,17 @@ ServerGameStateHand::TimerLoop(ServerGame &server)
|
||||
else if (playersWithCash.size() == 1)
|
||||
{
|
||||
// View a dialog for a new game - delayed.
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_DELAY_NEXT_GAME_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerNextGame, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_DELAY_NEXT_GAME_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerNextGame, this, boost::ref(server))));
|
||||
}
|
||||
else
|
||||
{
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
SERVER_DELAY_NEXT_HAND_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerNextHand, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
SERVER_DELAY_NEXT_HAND_SEC * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerNextHand, this, boost::ref(server))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -855,10 +855,10 @@ ServerGameStateHand::TimerShowCards(ServerGame &server)
|
||||
Game &curGame = server.GetGame();
|
||||
SendNewRoundCards(server, curGame, curGame.getCurrentHand()->getCurrentRound());
|
||||
|
||||
server.GetLobbyThread().GetTimerManager().AddTimer(
|
||||
server.GetStateTimerId(),
|
||||
GetDealCardsDelaySec(server) * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server)));
|
||||
server.SetStateTimerId(
|
||||
server.GetLobbyThread().GetTimerManager().RegisterTimer(
|
||||
GetDealCardsDelaySec(server) * 1000,
|
||||
boost::bind(&ServerGameStateHand::TimerLoop, this, boost::ref(server))));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user