mirror of
https://github.com/vcmi/vcmi.git
synced 2025-02-03 13:01:33 +02:00
Simplify networking code on server
This commit is contained in:
parent
c9765a52ff
commit
d6869160c5
@ -208,12 +208,12 @@ void CVCMIServer::onConnectionEstablished(const std::shared_ptr<NetworkConnectio
|
||||
|
||||
void CVCMIServer::setState(EServerState value)
|
||||
{
|
||||
state.store(value);
|
||||
state = value;
|
||||
}
|
||||
|
||||
EServerState CVCMIServer::getState() const
|
||||
{
|
||||
return state.load();
|
||||
return state;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
if(state == EServerState::LOBBY)
|
||||
c->connectionID = currentClientId++;
|
||||
if(state != EServerState::LOBBY)
|
||||
throw std::runtime_error("CVCMIServer::clientConnected called while game is not accepting clients!");
|
||||
|
||||
c->connectionID = currentClientId++;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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();
|
||||
srand((ui32)time(nullptr));
|
||||
|
||||
CVCMIServer server(opts);
|
||||
|
||||
#ifdef SINGLE_PROCESS_APP
|
||||
boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(const_cast<char *>(argv[0]));
|
||||
cond->notify_one();
|
||||
#endif
|
||||
|
||||
CVCMIServer server(opts);
|
||||
server.run();
|
||||
|
||||
#if VCMI_ANDROID_DUAL_PROCESS
|
||||
|
@ -63,12 +63,11 @@ private:
|
||||
/// List of all connections that were closed (but can still reconnect later)
|
||||
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;
|
||||
std::shared_ptr<CApplier<CBaseForServerApply>> applier;
|
||||
std::unique_ptr<boost::thread> remoteConnectionsThread;
|
||||
std::atomic<EServerState> state;
|
||||
EServerState state;
|
||||
|
||||
// INetworkListener impl
|
||||
void onDisconnected(const std::shared_ptr<NetworkConnection> & connection) override;
|
||||
@ -76,13 +75,14 @@ private:
|
||||
void onNewConnection(const std::shared_ptr<NetworkConnection> &) override;
|
||||
void onConnectionFailed(const std::string & errorMessage) override;
|
||||
void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) override;
|
||||
void onTimer() override;
|
||||
|
||||
void establishOutgoingConnection();
|
||||
|
||||
std::shared_ptr<CConnection> findConnection(const std::shared_ptr<NetworkConnection> &);
|
||||
|
||||
std::atomic<int> currentClientId;
|
||||
std::atomic<ui8> currentPlayerId;
|
||||
int currentClientId;
|
||||
ui8 currentPlayerId;
|
||||
|
||||
public:
|
||||
std::shared_ptr<CGameHandler> gh;
|
||||
|
@ -63,15 +63,17 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyCl
|
||||
// Until UUID set we only pass LobbyClientConnected to this client
|
||||
pack.c->uuid = pack.uuid;
|
||||
srv.updateAndPropagateLobbyState();
|
||||
if(srv.getState() == EServerState::GAMEPLAY)
|
||||
{
|
||||
//immediately start game
|
||||
std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
|
||||
startGameForReconnectedPlayer->initializedStartInfo = srv.si;
|
||||
startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
|
||||
startGameForReconnectedPlayer->clientId = pack.c->connectionID;
|
||||
srv.announcePack(std::move(startGameForReconnectedPlayer));
|
||||
}
|
||||
|
||||
// FIXME: what is this??? We do NOT support reconnection into ongoing game - at the very least queries and battles are NOT serialized
|
||||
// if(srv.getState() == EServerState::GAMEPLAY)
|
||||
// {
|
||||
// //immediately start game
|
||||
// std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
|
||||
// startGameForReconnectedPlayer->initializedStartInfo = srv.si;
|
||||
// startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
|
||||
// startGameForReconnectedPlayer->clientId = pack.c->connectionID;
|
||||
// srv.announcePack(std::move(startGameForReconnectedPlayer));
|
||||
// }
|
||||
}
|
||||
|
||||
void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
|
||||
|
Loading…
x
Reference in New Issue
Block a user