When creating a network game, a client is also started.

Network errors are handled globally.
Fixed last network thread issue by using a notification queue, should work fine now.
This commit is contained in:
lotodore
2007-03-20 14:05:16 +00:00
parent 7a704c2995
commit 62e72617ba
9 changed files with 172 additions and 100 deletions
+31 -8
View File
@@ -54,13 +54,6 @@ ServerRecvThread::~ServerRecvThread()
CleanupSessionMap();
}
void
ServerRecvThread::StartGame()
{
// TODO: not thread safe. use flag or something.
SetState(SERVER_START_GAME_STATE::Instance());
}
void
ServerRecvThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet)
{
@@ -85,6 +78,13 @@ ServerRecvThread::AddConnection(boost::shared_ptr<ConnectData> data)
m_connectQueue.push_back(data);
}
void
ServerRecvThread::AddNotification(unsigned notification)
{
boost::mutex::scoped_lock lock(m_notificationQueueMutex);
m_notificationQueue.push_back(notification);
}
void
ServerRecvThread::Main()
{
@@ -96,6 +96,7 @@ ServerRecvThread::Main()
while (!ShouldTerminate())
{
{
// Handle one incoming connection at a time.
boost::shared_ptr<ConnectData> tmpData;
{
boost::mutex::scoped_lock lock(m_connectQueueMutex);
@@ -108,7 +109,10 @@ ServerRecvThread::Main()
if (tmpData.get())
GetState().HandleNewConnection(*this, tmpData);
}
// Process current state.
GetState().Process(*this);
// Process thread-safe notifications.
NotificationLoop();
}
} catch (const NetException &)
{
@@ -121,6 +125,25 @@ ServerRecvThread::Main()
CleanupSessionMap();
}
void
ServerRecvThread::NotificationLoop()
{
boost::mutex::scoped_lock lock(m_notificationQueueMutex);
// Process all notifications.
while (!m_notificationQueue.empty())
{
unsigned notification = m_notificationQueue.front();
m_notificationQueue.pop_front();
switch(notification)
{
case NOTIFY_GAME_START:
SetState(SERVER_START_GAME_STATE::Instance());
break;
}
}
}
SOCKET
ServerRecvThread::Select()
{
@@ -139,7 +162,7 @@ ServerRecvThread::Select()
{
SOCKET tmpSock = i->first;
FD_SET(tmpSock, &rdset);
if (tmpSock > maxSock)
if (tmpSock > maxSock || maxSock == INVALID_SOCKET)
maxSock = tmpSock;
++i;
}