private void OnClientConnected(object sender, Utility.EventArg <EndPoint> e) { lock (Lock) { LogInfo($"{nameof(OnClientConnected)} {e.Value}"); var player = new PlayerData { Name = nameGen.Next(), IsReady = false, Room = LobbyRoomName, Role = 0 // Lobby dose not care about player's role }; var res = players.TryAdd(e.Value, player); Debug.Assert(res); FrontendServer.InvokeTo(e.Value).OnConnected(player.Name, player.Room); ForOthers(e.Value, (p) => { // Tell new player other players' info FrontendServer.InvokeTo(e.Value).OnPlayerJoined(p.Value.Name, p.Value.Room, p.Value.Role); // Tell other player new player's info FrontendServer.InvokeTo(p.Key).OnPlayerJoined(player.Name, LobbyRoomName, player.Role); }); } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { FrontendServer.ClientConnected -= OnClientConnected; FrontendServer.ClientDisconnected -= OnClientDisconnected; FrontendServer.ClientSendFailed -= OnClientSendFailed; FrontendServer.Dispose(); BackendServer.Dispose(); } disposedValue = true; } }
public void Start(int frontendPort, int backendPort, string localIp = null, string serverPath = null) { LogInfo($"Lobby starting..."); FrontendServer.Start(frontendPort); BackendServer.Start(backendPort); if (localIp != null) { this.localIp = localIp; } if (serverPath != null) { this.serverPath = serverPath; } LogInfo($"Lobby started with ip={this.localIp}, path={this.serverPath}"); }
private void OnClientDisconnected(object sender, Utility.EventArg <EndPoint> e) { lock (Lock) { LogInfo($"{nameof(OnClientDisconnected)} {e.Value}"); if (!players.ContainsKey(e.Value)) { return; } var player = players[e.Value]; ForOthers(e.Value, (p) => { FrontendServer.InvokeTo(p.Key).OnPlayerLeaved(player.Name); }); players.Remove(e.Value); } }