/// <summary> /// Method used to determine how a packet is handled by the client. /// </summary> /// <param name="client">ClientHandler that contains packet data.</param> public static void HandlePacket(ClientHandler client) { var type = (PacketType) BitConverter.ToInt16(client.PacketBuffer, 0); switch (type) { case PacketType.LoginResponse: client.LoginUi.Invoke( (MethodInvoker) (() => client.LoginUi.ReciveResponse(new LoginResponse(client.PacketBuffer)))); break; case PacketType.PlayerAssociation: if(client.OnMenu == null) client.LoginUi.Invoke( (MethodInvoker)(() => client.LoginUi.ReciveAssociation(new PlayerAssociation(client.PacketBuffer)))); else client.LoginUi.Invoke( (MethodInvoker)(() => client.OnMenu.ReciveAssociation(new PlayerAssociation(client.PacketBuffer)))); break; case PacketType.PlayerData: client.LoginUi.Invoke( (MethodInvoker)(() => client.LoginUi.RecivePlayerData(new PlayerData(client.PacketBuffer)))); break; case PacketType.GameInvite: client.OnMenu.Invoke( (MethodInvoker) (() => client.OnMenu.ReciveGameInvite(new GameInvite(client.PacketBuffer)))); break; default: Console.WriteLine("Unknown packet type: {0}, with the length of {1}", type, client.PacketBuffer.Length); break; } }
/// <summary> /// Constructor called for starting the Login process. /// The client handler is sent so that the loginUi can be controlled with in the socket. /// </summary> /// <param name="client">The new or used client handler.</param> public Login(ClientHandler client) { InitializeComponent(); SetVisiblePane("Login"); _clientHandler = client; _clientHandler.LoginUi = this; }
/// <summary> /// Method used to connect a clientHandler to the server in the settings. /// </summary> /// <param name="clientHandler">Client handler with unused Socket</param> public void ConnectClient(ClientHandler clientHandler) { try { IPAddress ipAddress = IPAddress.Parse(Properties.Settings.Default.IP); IPEndPoint remoteEp = new IPEndPoint(ipAddress, Properties.Settings.Default.Port); clientHandler.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. clientHandler.ClientSocket.BeginConnect(remoteEp, ConnectCallback, clientHandler); } catch (Exception e) { //TODO: Add a error mode for developers using this. Console.WriteLine(e.ToString()); } }