Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void Main()
- {
- successfullyConnectedUsers = new List<ConnectedUser>();
- //Populate users.
- PopulateUsers();
- //Trigger the method PrintIncomingMessage when a packet of type 'LoginInfo' is received
- //We expect the incoming object to be a string which we state explicitly by using <string>
- NetworkComms.AppendGlobalIncomingPacketHandler<string>("LoginInfo", CheckLoginInfo);
- NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetKillRequest", GetKillRequest);
- //Start listening for incoming connections
- Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2341));
- //Print out the IPs and ports we are now listening on
- Console.WriteLine("Server listening for TCP connection on:");
- foreach (IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP))
- Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port);
- //Let the user close the server
- Console.WriteLine("\nPress any key to close server.");
- Console.ReadKey(true);
- //We have used NetworkComms so we should ensure that we correctly call shutdown
- NetworkComms.Shutdown();
- }
- private static void PopulateUsers()
- {
- try
- {
- UserList list = Desearilize<UserList>("Users.xml");
- users = list.Users;
- Console.WriteLine("Successfully populated users!");
- }
- catch(Exception e)
- {
- Console.WriteLine("Error reading users.");
- Console.WriteLine(e.Message);
- }
- }
- private static void GetKillRequest(PacketHeader header, Connection connection, string message)
- {
- if (!successfullyConnectedUsers.Select(x => TCPConnection.GetConnection(new ConnectionInfo(x.connection))).Contains(connection))
- {
- connection.SendObject("KillRecieve", "NotLoggedIn");
- return;
- }
- connection.SendObject("KillRecieve", GetUserFromConnection(connection).user.Kills.ToString());
- }
- private static void GetDeathsRequest(PacketHeader header, Connection connection, string message)
- {
- if (!successfullyConnectedUsers.Select(x => TCPConnection.GetConnection(new ConnectionInfo(x.connection))).Contains(connection))
- {
- connection.SendObject("DeathRecieve", "NotLoggedIn");
- return;
- }
- connection.SendObject("KillRecieve", GetUserFromConnection(connection).user.Kills.ToString());
- }
- private static void CheckLoginInfo(PacketHeader header, Connection connection, string message)
- {
- string username = message.Split(':').First();
- string password = message.Split(':').Last();
- foreach(User u in users)
- {
- if(u.Username == username && u.Password == password)
- {
- connection.SendObject("LoginRecieve", "Success");
- successfullyConnectedUsers.Add(new ConnectedUser(u, connection.ConnectionInfo.RemoteEndPoint));
- return;
- }
- else if(u.Username == username && u.Password != password)
- {
- connection.SendObject("LoginRecieve", "InvalidPassword");
- return;
- }
- }
- connection.SendObject("LoginRecieve", "UserNotFound");
- }
Add Comment
Please, Sign In to add comment