Kicking players at the beginning of a network game now works.
This commit is contained in:
@@ -379,7 +379,15 @@ AbstractClientStateReceiving::Process(ClientThread &client)
|
||||
|
||||
if (tmpPacket.get())
|
||||
{
|
||||
if (tmpPacket->ToNetPacketChatText())
|
||||
if (tmpPacket->ToNetPacketError())
|
||||
{
|
||||
// Server reported an error.
|
||||
NetPacketError::Data errorData;
|
||||
tmpPacket->ToNetPacketError()->GetData(errorData);
|
||||
// Show the error.
|
||||
throw ClientException(errorData.errorCode, 0);
|
||||
}
|
||||
else if (tmpPacket->ToNetPacketChatText())
|
||||
{
|
||||
// Chat message - display it in the GUI.
|
||||
NetPacketChatText::Data chatData;
|
||||
@@ -464,14 +472,6 @@ ClientStateWaitSession::InternalProcess(ClientThread &client, boost::shared_ptr<
|
||||
client.SetState(ClientStateWaitGame::Instance());
|
||||
retVal = MSG_SOCK_SESSION_DONE;
|
||||
}
|
||||
else if (packet->ToNetPacketError())
|
||||
{
|
||||
// Server reported an error.
|
||||
NetPacketError::Data errorData;
|
||||
packet->ToNetPacketError()->GetData(errorData);
|
||||
// Show the error.
|
||||
throw ClientException(errorData.errorCode, 0);
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ using namespace std;
|
||||
#define NET_ERR_JOIN_GAME_INVALID_PLAYER_NAME 0x0006
|
||||
#define NET_ERR_GENERAL_INVALID_PACKET 0xFF01
|
||||
#define NET_ERR_GENERAL_INVALID_STATE 0xFF02
|
||||
#define NET_ERR_GENERAL_PLAYER_KICKED 0xFF03
|
||||
#define NET_ERR_OTHER 0xFFFF
|
||||
|
||||
#ifdef _MSC_VER
|
||||
@@ -2111,6 +2112,9 @@ NetPacketError::SetData(const NetPacketError::Data &inData)
|
||||
case ERR_SOCK_INVALID_STATE :
|
||||
tmpData->reason = htons(NET_ERR_GENERAL_INVALID_STATE);
|
||||
break;
|
||||
case ERR_NET_PLAYER_KICKED :
|
||||
tmpData->reason = htons(NET_ERR_GENERAL_PLAYER_KICKED);
|
||||
break;
|
||||
default :
|
||||
tmpData->reason = htons(NET_ERR_OTHER);
|
||||
break;
|
||||
@@ -2151,6 +2155,9 @@ NetPacketError::GetData(NetPacketError::Data &outData) const
|
||||
case NET_ERR_GENERAL_INVALID_STATE :
|
||||
outData.errorCode = ERR_SOCK_INVALID_STATE;
|
||||
break;
|
||||
case NET_ERR_GENERAL_PLAYER_KICKED :
|
||||
outData.errorCode = ERR_NET_PLAYER_KICKED;
|
||||
break;
|
||||
default :
|
||||
outData.errorCode = ERR_SOCK_INTERNAL;
|
||||
break;
|
||||
|
||||
@@ -82,10 +82,10 @@ ServerRecvThread::AddConnection(boost::shared_ptr<ConnectData> data)
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddNotification(unsigned message, unsigned param1, unsigned param2)
|
||||
ServerRecvThread::AddNotification(unsigned message, const string ¶m)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_notificationQueueMutex);
|
||||
m_notificationQueue.push_back(Notification(message, param1, param2));
|
||||
m_notificationQueue.push_back(Notification(message, param));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -145,6 +145,9 @@ ServerRecvThread::NotificationLoop()
|
||||
case NOTIFY_GAME_START:
|
||||
InternalStartGame();
|
||||
break;
|
||||
case NOTIFY_KICK_PLAYER:
|
||||
InternalKickPlayer(notification.param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,13 +290,24 @@ ServerRecvThread::InternalStartGame()
|
||||
m_game.reset(new Game(&gui, factory, playerData, GetGameData(), GetStartData(), m_curGameId++));
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::InternalKickPlayer(const string playerName)
|
||||
{
|
||||
if (!playerName.empty())
|
||||
{
|
||||
SessionWrapper tmpSession = GetSession(playerName);
|
||||
|
||||
SessionError(tmpSession, ERR_NET_PLAYER_KICKED);
|
||||
}
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
ServerRecvThread::GetSession(SOCKET sock)
|
||||
ServerRecvThread::GetSession(SOCKET sock) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::iterator pos = m_sessionMap.find(sock);
|
||||
SocketSessionMap::const_iterator pos = m_sessionMap.find(sock);
|
||||
if (pos != m_sessionMap.end())
|
||||
{
|
||||
tmpSession = pos->second;
|
||||
@@ -301,6 +315,34 @@ ServerRecvThread::GetSession(SOCKET sock)
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
ServerRecvThread::GetSession(const string playerName) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Check all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
if (tmpPlayer->GetName() == playerName)
|
||||
{
|
||||
tmpSession = session_i->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddSession(boost::shared_ptr<SessionData> sessionData)
|
||||
{
|
||||
@@ -415,25 +457,15 @@ ServerRecvThread::GetCurNumberOfPlayers() const
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::IsPlayerConnected(const std::string &playerName) const
|
||||
ServerRecvThread::IsPlayerConnected(const string &playerName) const
|
||||
{
|
||||
bool retVal = false;
|
||||
PlayerDataList playerList = GetPlayerDataList();
|
||||
|
||||
PlayerDataList::const_iterator player_i = playerList.begin();
|
||||
PlayerDataList::const_iterator player_end = playerList.end();
|
||||
SessionWrapper tmpSession = GetSession(playerName);
|
||||
|
||||
// Check by name - the name is unique.
|
||||
while (player_i != player_end)
|
||||
{
|
||||
if ((*player_i)->GetName() == playerName)
|
||||
{
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
retVal = true;
|
||||
|
||||
++player_i;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -612,7 +644,7 @@ ServerRecvThread::SetStartData(const StartData &startData)
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::CheckPassword(const std::string &password) const
|
||||
ServerRecvThread::CheckPassword(const string &password) const
|
||||
{
|
||||
return (password == m_password);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#define ACCEPT_TIMEOUT_MSEC 50
|
||||
#define NET_SERVER_LISTEN_BACKLOG 5
|
||||
|
||||
using namespace std;
|
||||
|
||||
ServerThread::ServerThread(GuiInterface &gui, ConfigFile *config)
|
||||
: m_gui(gui)
|
||||
@@ -63,7 +64,17 @@ ServerThread::StartGame()
|
||||
return; // TODO: throw exception
|
||||
|
||||
// Thread-safe notification.
|
||||
GetRecvThread().AddNotification(NOTIFY_GAME_START, 0, 0);
|
||||
GetRecvThread().AddNotification(NOTIFY_GAME_START, "");
|
||||
}
|
||||
|
||||
void
|
||||
ServerThread::KickPlayer(const string &playerName)
|
||||
{
|
||||
if (!IsRunning())
|
||||
return; // TODO: throw exception
|
||||
|
||||
// Thread-safe notification.
|
||||
GetRecvThread().AddNotification(NOTIFY_KICK_PLAYER, playerName);
|
||||
}
|
||||
|
||||
ServerCallback &
|
||||
|
||||
Reference in New Issue
Block a user