Guest User

Untitled

a guest
Oct 22nd, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. public static void Main()
  2.         {
  3.             successfullyConnectedUsers = new List<ConnectedUser>();
  4.  
  5.             //Populate users.
  6.             PopulateUsers();
  7.  
  8.             //Trigger the method PrintIncomingMessage when a packet of type 'LoginInfo' is received
  9.             //We expect the incoming object to be a string which we state explicitly by using <string>
  10.             NetworkComms.AppendGlobalIncomingPacketHandler<string>("LoginInfo", CheckLoginInfo);
  11.             NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetKillRequest", GetKillRequest);
  12.             //Start listening for incoming connections
  13.             Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2341));
  14.  
  15.             //Print out the IPs and ports we are now listening on
  16.             Console.WriteLine("Server listening for TCP connection on:");
  17.             foreach (IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP))
  18.                 Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port);
  19.  
  20.             //Let the user close the server
  21.             Console.WriteLine("\nPress any key to close server.");
  22.             Console.ReadKey(true);
  23.  
  24.             //We have used NetworkComms so we should ensure that we correctly call shutdown
  25.             NetworkComms.Shutdown();
  26.         }
  27.  
  28.         private static void PopulateUsers()
  29.         {
  30.             try
  31.             {
  32.                 UserList list = Desearilize<UserList>("Users.xml");
  33.                 users = list.Users;
  34.                 Console.WriteLine("Successfully populated users!");
  35.             }
  36.             catch(Exception e)
  37.             {
  38.                 Console.WriteLine("Error reading users.");
  39.                 Console.WriteLine(e.Message);
  40.             }
  41.         }
  42.  
  43.         private static void GetKillRequest(PacketHeader header, Connection connection, string message)
  44.         {
  45.             if (!successfullyConnectedUsers.Select(x => TCPConnection.GetConnection(new ConnectionInfo(x.connection))).Contains(connection))
  46.             {
  47.                 connection.SendObject("KillRecieve", "NotLoggedIn");
  48.                 return;
  49.             }
  50.  
  51.             connection.SendObject("KillRecieve", GetUserFromConnection(connection).user.Kills.ToString());
  52.         }
  53.  
  54.         private static void GetDeathsRequest(PacketHeader header, Connection connection, string message)
  55.         {
  56.             if (!successfullyConnectedUsers.Select(x => TCPConnection.GetConnection(new ConnectionInfo(x.connection))).Contains(connection))
  57.             {
  58.                 connection.SendObject("DeathRecieve", "NotLoggedIn");
  59.                 return;
  60.             }
  61.  
  62.             connection.SendObject("KillRecieve", GetUserFromConnection(connection).user.Kills.ToString());
  63.         }
  64.  
  65.         private static void CheckLoginInfo(PacketHeader header, Connection connection, string message)
  66.         {
  67.             string username = message.Split(':').First();
  68.             string password = message.Split(':').Last();
  69.  
  70.             foreach(User u in users)
  71.             {
  72.                 if(u.Username == username && u.Password == password)
  73.                 {
  74.                     connection.SendObject("LoginRecieve", "Success");
  75.                     successfullyConnectedUsers.Add(new ConnectedUser(u, connection.ConnectionInfo.RemoteEndPoint));
  76.                     return;
  77.                 }
  78.                 else if(u.Username == username && u.Password != password)
  79.                 {
  80.                     connection.SendObject("LoginRecieve", "InvalidPassword");
  81.                     return;
  82.                 }
  83.             }
  84.  
  85.             connection.SendObject("LoginRecieve", "UserNotFound");
  86.         }
Add Comment
Please, Sign In to add comment