private void HandleInitResponse(ServerInitializeGameResponse initResponse) { if (initResponse.m_players == null || initResponse.m_ball == null || initResponse.m_field == null) { IntiializeDelayedCancel(); StatusLobby.SetStatus("Invalid server response!"); return; } ParentControl.GameControl.InitializeGame(initResponse.m_players, initResponse.m_field, initResponse.m_ball); ParentControl.SwitchMode(GameMode.Game); }
static void proGamerClient() { IPEndPoint server = new IPEndPoint(IPAddress.Parse("127.0.0.1"), NetworkConstants.SERVER_PORT); Socket connectionSocket = new Socket(server.AddressFamily, SocketType.Stream, ProtocolType.Tcp); connectionSocket.Connect(server); TCPPacketConnection conn = new TCPPacketConnection(connectionSocket); connections.Add(conn); open8PlayersGame(conn); NetworkConnection networkConn = new NetworkConnection(conn); UDPConnection c = new UDPConnection(conn.Local); c.InitializeReceiving(); networkConn.SetUDPConnection(c); List <int> playerIDs = new List <int>(); while (true) { PackageInterface p = networkConn.ReadTCP(); if (p != null && p.PackageType == PackageType.ServerPlayerIDResponse) { ServerInitializeGameResponse ps = p as ServerInitializeGameResponse; foreach (Player pl in ps.m_players) { if (pl.Controllable) { playerIDs.Add(pl.ID); } } break; } else { Thread.Sleep(100); } } while (true) { foreach (int playerID in playerIDs) { sendMovementToMatchBall(networkConn, playerID); } Thread.Sleep(100); } }
private void SendServerInitResponse(Client client) { ServerInitializeGameResponse packet = new ServerInitializeGameResponse(); packet.m_field = new GameField(); packet.m_ball = new Ball(); packet.m_players = new Player[GameStructure.PlayersCount]; Array.Copy(GameStructure.GetAllPlayers(), packet.m_players, GameStructure.PlayersCount); foreach (Player player in packet.m_players) { player.Controllable = client.Players.Contains(player); } Network.SendTCPPackageToClient(packet, client.SessionID); }
public bool RejoinClient(NetworkConnection client) { bool couldRejoin = false; // rejoin is only justified if the client connection died. If it's still connected we want to avoid rejoin since this would get messy bool rejoinJustified = !Network.ClientStillConnected(client.ClientSession.SessionID); Client correctClient = null; foreach (Client c in Clients) { if (c.SessionID == client.ClientSession.SessionID) { correctClient = c; } } Logger.GameLog("Client rejoin was requested: " + client.ClientSession.SessionID.ToString()); if (GameState != GameStates.Finished && rejoinJustified && correctClient != null) { ServerInitializeGameResponse packet = new ServerInitializeGameResponse(); packet.m_field = GameStructure.GameField; packet.m_ball = GameStructure.Ball; packet.m_players = new Player[GameStructure.PlayersCount]; Array.Copy(GameStructure.GetAllPlayers(), packet.m_players, GameStructure.PlayersCount); foreach (Player p in packet.m_players) { foreach (Player player in correctClient.Players) { p.Controllable = (player.ID == p.ID); } } Logger.GameLog("Rejoin succeeded sending the ServerSessionResponse with GameReconnect Flag set to true to the Client"); ServerSessionResponse response = new ServerSessionResponse(); response.ClientSessionID = client.ClientSession.SessionID; response.GameReconnect = true; client.SendTCP(response); Logger.GameLog("Rejoin succeeded sending the ServerInitializeGameResponse to the Client"); client.SendTCP(packet); couldRejoin = true; Logger.GameLog("Since Client just rejoined he isn't aware of the current score, therefore sending a score package"); client.SendTCP(GenerateScorePackage()); Network.AddClientConnection(client); } return(couldRejoin); }
public bool AddObserver(NetworkConnection connection) { bool added = false; if (GameState != GameStates.Finished) { ServerInitializeGameResponse packet = new ServerInitializeGameResponse(); packet.m_field = GameStructure.GameField; packet.m_ball = GameStructure.Ball; packet.m_players = new Player[GameStructure.PlayersCount]; Array.Copy(GameStructure.GetAllPlayers(), packet.m_players, GameStructure.PlayersCount); foreach (Player player in packet.m_players) { player.Controllable = false; } connection.SendTCP(packet); connection.SendTCP(GenerateScorePackage()); Network.AddObserver(connection); added = true; } return(added); }