1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-29 23:07:48 +02:00

Simplify networking code on server

This commit is contained in:
Ivan Savenko
2023-12-26 18:29:13 +02:00
parent c9765a52ff
commit d6869160c5
3 changed files with 44 additions and 37 deletions

View File

@@ -208,12 +208,12 @@ void CVCMIServer::onConnectionEstablished(const std::shared_ptr<NetworkConnectio
void CVCMIServer::setState(EServerState value) void CVCMIServer::setState(EServerState value)
{ {
state.store(value); state = value;
} }
EServerState CVCMIServer::getState() const EServerState CVCMIServer::getState() const
{ {
return state.load(); return state;
} }
std::shared_ptr<CConnection> CVCMIServer::findConnection(const std::shared_ptr<NetworkConnection> & netConnection) std::shared_ptr<CConnection> CVCMIServer::findConnection(const std::shared_ptr<NetworkConnection> & netConnection)
@@ -256,6 +256,11 @@ void CVCMIServer::run()
} }
} }
void CVCMIServer::onTimer()
{
// FIXME: move GameHandler updates here
}
void CVCMIServer::establishOutgoingConnection() void CVCMIServer::establishOutgoingConnection()
{ {
if(!cmdLineOptions.count("lobby")) if(!cmdLineOptions.count("lobby"))
@@ -456,8 +461,10 @@ bool CVCMIServer::passHost(int toConnectionId)
void CVCMIServer::clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode) void CVCMIServer::clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode)
{ {
if(state == EServerState::LOBBY) if(state != EServerState::LOBBY)
c->connectionID = currentClientId++; throw std::runtime_error("CVCMIServer::clientConnected called while game is not accepting clients!");
c->connectionID = currentClientId++;
if(hostClientId == -1) if(hostClientId == -1)
{ {
@@ -467,27 +474,24 @@ void CVCMIServer::clientConnected(std::shared_ptr<CConnection> c, std::vector<st
logNetwork->info("Connection with client %d established. UUID: %s", c->connectionID, c->uuid); logNetwork->info("Connection with client %d established. UUID: %s", c->connectionID, c->uuid);
if(state == EServerState::LOBBY) for(auto & name : names)
{ {
for(auto & name : names) logNetwork->info("Client %d player: %s", c->connectionID, name);
ui8 id = currentPlayerId++;
ClientPlayer cp;
cp.connection = c->connectionID;
cp.name = name;
playerNames.insert(std::make_pair(id, cp));
announceTxt(boost::str(boost::format("%s (pid %d cid %d) joins the game") % name % id % c->connectionID));
//put new player in first slot with AI
for(auto & elem : si->playerInfos)
{ {
logNetwork->info("Client %d player: %s", c->connectionID, name); if(elem.second.isControlledByAI() && !elem.second.compOnly)
ui8 id = currentPlayerId++;
ClientPlayer cp;
cp.connection = c->connectionID;
cp.name = name;
playerNames.insert(std::make_pair(id, cp));
announceTxt(boost::str(boost::format("%s (pid %d cid %d) joins the game") % name % id % c->connectionID));
//put new player in first slot with AI
for(auto & elem : si->playerInfos)
{ {
if(elem.second.isControlledByAI() && !elem.second.compOnly) setPlayerConnectedId(elem.second, id);
{ break;
setPlayerConnectedId(elem.second, id);
break;
}
} }
} }
} }
@@ -1086,12 +1090,13 @@ int main(int argc, const char * argv[])
loadDLLClasses(); loadDLLClasses();
srand((ui32)time(nullptr)); srand((ui32)time(nullptr));
CVCMIServer server(opts);
#ifdef SINGLE_PROCESS_APP #ifdef SINGLE_PROCESS_APP
boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(const_cast<char *>(argv[0])); boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(const_cast<char *>(argv[0]));
cond->notify_one(); cond->notify_one();
#endif #endif
CVCMIServer server(opts);
server.run(); server.run();
#if VCMI_ANDROID_DUAL_PROCESS #if VCMI_ANDROID_DUAL_PROCESS

View File

@@ -63,12 +63,11 @@ private:
/// List of all connections that were closed (but can still reconnect later) /// List of all connections that were closed (but can still reconnect later)
std::vector<std::shared_ptr<CConnection>> inactiveConnections; std::vector<std::shared_ptr<CConnection>> inactiveConnections;
std::atomic<bool> restartGameplay; // FIXME: this is just a hack bool restartGameplay; // FIXME: this is just a hack
boost::recursive_mutex mx; boost::recursive_mutex mx;
std::shared_ptr<CApplier<CBaseForServerApply>> applier; std::shared_ptr<CApplier<CBaseForServerApply>> applier;
std::unique_ptr<boost::thread> remoteConnectionsThread; EServerState state;
std::atomic<EServerState> state;
// INetworkListener impl // INetworkListener impl
void onDisconnected(const std::shared_ptr<NetworkConnection> & connection) override; void onDisconnected(const std::shared_ptr<NetworkConnection> & connection) override;
@@ -76,13 +75,14 @@ private:
void onNewConnection(const std::shared_ptr<NetworkConnection> &) override; void onNewConnection(const std::shared_ptr<NetworkConnection> &) override;
void onConnectionFailed(const std::string & errorMessage) override; void onConnectionFailed(const std::string & errorMessage) override;
void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) override; void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) override;
void onTimer() override;
void establishOutgoingConnection(); void establishOutgoingConnection();
std::shared_ptr<CConnection> findConnection(const std::shared_ptr<NetworkConnection> &); std::shared_ptr<CConnection> findConnection(const std::shared_ptr<NetworkConnection> &);
std::atomic<int> currentClientId; int currentClientId;
std::atomic<ui8> currentPlayerId; ui8 currentPlayerId;
public: public:
std::shared_ptr<CGameHandler> gh; std::shared_ptr<CGameHandler> gh;

View File

@@ -63,15 +63,17 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyCl
// Until UUID set we only pass LobbyClientConnected to this client // Until UUID set we only pass LobbyClientConnected to this client
pack.c->uuid = pack.uuid; pack.c->uuid = pack.uuid;
srv.updateAndPropagateLobbyState(); srv.updateAndPropagateLobbyState();
if(srv.getState() == EServerState::GAMEPLAY)
{ // FIXME: what is this??? We do NOT support reconnection into ongoing game - at the very least queries and battles are NOT serialized
//immediately start game // if(srv.getState() == EServerState::GAMEPLAY)
std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame); // {
startGameForReconnectedPlayer->initializedStartInfo = srv.si; // //immediately start game
startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState(); // std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
startGameForReconnectedPlayer->clientId = pack.c->connectionID; // startGameForReconnectedPlayer->initializedStartInfo = srv.si;
srv.announcePack(std::move(startGameForReconnectedPlayer)); // startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
} // startGameForReconnectedPlayer->clientId = pack.c->connectionID;
// srv.announcePack(std::move(startGameForReconnectedPlayer));
// }
} }
void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack) void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)