1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Multiplayer: gracefully handle player loss unless it's a host

We don't want server to shutdown after just one of players lost the game.
This commit is contained in:
Arseniy Shestakov 2016-10-30 02:43:06 +03:00
parent c66c66c5ae
commit db5a52a0f8
4 changed files with 14 additions and 3 deletions

View File

@ -391,7 +391,7 @@ void CClient::newGame( CConnection *con, StartInfo *si )
else else
{ {
serv = con; serv = con;
networkMode = (con->connectionID == 1) ? HOST : GUEST; networkMode = con->isHost() ? HOST : GUEST;
} }
CConnection &c = *serv; CConnection &c = *serv;
@ -693,7 +693,7 @@ void CClient::stopConnection()
{ {
terminate = true; terminate = true;
if (serv) //request closing connection if (serv && serv->isHost()) //request closing connection
{ {
logNetwork->infoStream() << "Connection has been requested to be closed."; logNetwork->infoStream() << "Connection has been requested to be closed.";
boost::unique_lock<boost::mutex>(*serv->wmx); boost::unique_lock<boost::mutex>(*serv->wmx);
@ -1075,6 +1075,7 @@ CConnection * CServerHandler::justConnectToServer(const std::string &host, const
ret = new CConnection( host.size() ? host : settings["server"]["server"].String(), ret = new CConnection( host.size() ? host : settings["server"]["server"].String(),
realPort, realPort,
NAME); NAME);
ret->connectionID = 1; // TODO: Refactoring for the server so IDs set outside of CConnection
} }
catch(...) catch(...)
{ {

View File

@ -201,6 +201,11 @@ bool CConnection::isOpen() const
return socket && connected; return socket && connected;
} }
bool CConnection::isHost() const
{
return connectionID == 1;
}
void CConnection::reportState(CLogger * out) void CConnection::reportState(CLogger * out)
{ {
out->debugStream() << "CConnection"; out->debugStream() << "CConnection";

View File

@ -74,6 +74,7 @@ public:
void close(); void close();
bool isOpen() const; bool isOpen() const;
bool isHost() const;
template<class T> template<class T>
CConnection &operator&(const T&); CConnection &operator&(const T&);
virtual ~CConnection(void); virtual ~CConnection(void);

View File

@ -1097,7 +1097,7 @@ void CGameHandler::handleConnection(std::set<PlayerColor> players, CConnection &
{ {
assert(!c.connected); //make sure that connection has been marked as broken assert(!c.connected); //make sure that connection has been marked as broken
logGlobal->error(e.what()); logGlobal->error(e.what());
end2 = true; conns -= &c;
} }
catch(...) catch(...)
{ {
@ -2631,6 +2631,9 @@ void CGameHandler::sendToAllClients(CPackForClient * info)
logNetwork->trace("Sending to all clients a package of type %s", typeid(*info).name()); logNetwork->trace("Sending to all clients a package of type %s", typeid(*info).name());
for (auto & elem : conns) for (auto & elem : conns)
{ {
if(!elem->isOpen())
continue;
boost::unique_lock<boost::mutex> lock(*(elem)->wmx); boost::unique_lock<boost::mutex> lock(*(elem)->wmx);
*elem << info; *elem << info;
} }
@ -2703,6 +2706,7 @@ void CGameHandler::close()
{ {
exit(0); exit(0);
} }
end2 = true;
//for (CConnection *cc : conns) //for (CConnection *cc : conns)
// if (cc && cc->socket && cc->socket->is_open()) // if (cc && cc->socket && cc->socket->is_open())