/// <summary> /// Callback function for when a Server thread exits. /// </summary> /// <param name="server">The server that is no longer running.</param> public void CloseServer(Server server) { servers.Remove(server); uint numPlayersLoggedIn = 0; foreach (var p in players.GetPlayers()) if (p.LoggedIn) numPlayersLoggedIn++; Console.WriteLine("Removed server. {0} remaining servers, {1} players logged in.", servers.Count, numPlayersLoggedIn); }
/// <summary> /// Runs the ListenServer. /// </summary> public void Run() { String ip = "127.0.0.1"; //String ip = "128.220.251.35"; //String ip = "128.220.70.65"; IPAddress ipAddress = IPAddress.Parse(ip); IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); try { TcpListener listener = new TcpListener(ipLocalEndPoint); listener.Start(); while (true) { while (servers.Count < MAX_PLAYERS) { Console.WriteLine("Waiting for connections..."); TcpClient client = listener.AcceptTcpClient(); //var task = listener.AcceptTcpClientAsync(); //if (!task.Wait(LISTEN_LENGTH)) // continue; //TcpClient client = task.Result; Server server = new Server(client, new ListenServer.CloseServerDelegate(CloseServer)); servers.Add(server); //int playerID = servers.IndexOf(server); //server.playerID = playerID; Console.WriteLine("{0} players connected", servers.Count); server.Start(); } //System.Threading.Thread.Sleep(1000); } } catch (SocketException e) { //Continue if there is a socket exception Console.WriteLine(e.ToString()); } catch (Exception e) { //Crash if there is any other exception Console.WriteLine(e.ToString()); Console.Read(); } }
public NetworkState Send(Server server) { if (loggedIn) { Console.WriteLine("{0} successfully logged in with {1}", server.player.Name, server.player.Password); server.Write(BitConverter.GetBytes(LOGIN_SUCCESSFUL_VALUE), 0, 4); return new PositionState(); } else //This should retry in the future, rather than closing the server throw new ApplicationException("Login failed"); }
/// <summary> /// Default constructor. /// </summary> /// <param name="server">The server that holds this state.</param> public StateContext(Server server) { this.server = server; }
public void Receive(Server server) { try { int length = server.ReadInt(); if (length > Player.MAX_PLAYER_NAME_LENGTH) throw new ApplicationException(String.Format("Name too long: {0} characters", length)); String name = server.ReadString((uint)length); Console.WriteLine("Player attempted to join with name: " + name); server.player = players.FindPlayer(name); if (server.player == null) throw new ApplicationException("Player not found"); if (server.player.LoggedIn) throw new ApplicationException("Player " + server.player.Name + " already logged in"); String password = server.ReadString(128); if (!server.player.Password.Equals(password)) throw new ApplicationException(String.Format("Player gave invalid password {0}.", password)); Player updatedPlayer = new Player(server.player); updatedPlayer.LoggedIn = true; if (players.TryUpdate(name, updatedPlayer, server.player)) loggedIn = true; } catch (ApplicationException e) { Console.WriteLine(e.Message); } }
private void WritePlayerData(Server server, IList<Player> otherPlayers) { byte[] buffer = new byte[otherPlayers.Count * 8]; int index = 0; //Write the data for each player into a buffer foreach (Player p in otherPlayers) { Buffer.BlockCopy(BitConverter.GetBytes(p.Position.x), 0, buffer, index, 4); Buffer.BlockCopy(BitConverter.GetBytes(p.Position.y), 0, buffer, index + 4, 4); //Console.Write("({0}, {1})",p.Position.x, p.Position.y); index += 8; //Check for buffer full } //Write the buffer of player data server.Write(buffer, 0, index); }
public NetworkState Send(Server server) { //Find the first other player. List<Player> otherPlayers = players.GetNearbyPlayers(server.player.Name); int numNearbyPlayers = otherPlayers.Count; //Write the number of players for which we are going to send data server.Write(BitConverter.GetBytes(numNearbyPlayers), 0, 4); WritePlayerData(server, otherPlayers); return this; }
public void Receive(Server server) { Position newPos; newPos.x = server.ReadSingle(); newPos.y = server.ReadSingle(); server.player.Position = newPos; //Console.WriteLine("Read position ({0},{1})",newPos.x, newPos.y); }
public NetworkState Send(Server server) { server.Write(BitConverter.GetBytes(AUTHENTICATION_SERVER_VALUE), 0, 4); //Console.WriteLine("Authentication successful!"); return new UsernameAndPasswordState(); }
public void Receive(Server server) { int value = server.ReadInt(); Console.WriteLine("Player authenticated with {0}", value); if (value != AUTHENTICATION_CLIENT_VALUE) throw new ApplicationException("Player authenticated with invalid value."); }