public void Run(Server server, NetIncomingMessage im, PlayerAndConnection playerAndConnection, GameMap gameMap) { Console.WriteLine("New connection attempt..."); var data = im.ReadByte(); if (data == (byte)PacketType.Login) { Console.WriteLine(" .. approved."); // Create the player and add it to the map var packetData = new LoginPacketIO().ReadRequest(im); playerAndConnection = CreatePlayer(im, packetData.Username, gameMap); // Create the hail message // it contains the player id and all the players already connected var hmsg = CreateHailMessage(server, playerAndConnection, gameMap); im.SenderConnection.Approve(hmsg); // Add the player to the map gameMap.AddPlayer(playerAndConnection); server.SendNewPlayerBroadcast(playerAndConnection); } else { im.SenderConnection.Deny("Bad packet"); } }
private bool EstabilishConnection() { var time = DateTime.Now; NetIncomingMessage im; while (DateTime.Now.Subtract(time).Seconds < 5) { if ((im = _client.ReadMessage()) == null) { continue; } switch (im.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(im.ReadString()); break; case NetIncomingMessageType.StatusChanged: if (im.SenderConnection.Status == NetConnectionStatus.Connected) { Console.WriteLine("Connection accepted"); var hmsg = im.SenderConnection.RemoteHailMessage; var data = new LoginPacketIO().ReadResponse(hmsg); PlayerData = data.Player; Console.WriteLine($"Our id is '{PlayerData.Id}'"); foreach (var player in data.OtherPlayers) { Players[player.Id] = player; Console.WriteLine($"Already connected player with id {player.Id}"); OnPlayerAdded?.Invoke(Players[player.Id]); } OnConnected?.Invoke(PlayerData); Active = true; return(true); } break; case NetIncomingMessageType.ConnectionApproval: Console.WriteLine($"Connection approval: {im.ReadString()}"); break; default: Console.WriteLine($"not handled: {im.MessageType} | {(PacketType)im.ReadByte()}"); break; } } return(false); }