Example #1
0
 public PdServer()
 {
     _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
     {
         NoDelay = true
     };
     Client = new PdClient();
 }
        public void Handle(PdClient client, Packet packet)
        {
            PacketHandlerFunc handler;
            if (!_handlers.TryGetValue(packet.Op, out handler))
            {
                this.UnknownPacket(client, packet);
                return;
            }

            handler(client, packet);
        }
 public void UnknownPacket(PdClient client, Packet packet)
 {
     Log.Unimplemented("PacketHandlerManager: Handler for '{0:X4}'", packet.Op);
     Log.Debug(packet);
 }
Example #4
0
 protected virtual void OnServerLostConnection(PdClient client)
 {
     if (ServerLostConnection == null)
     {
         this.OnServerDisconnected(client);
         return;
     }
     ServerLostConnection(client);
 }
Example #5
0
 protected virtual void OnServerDisconnected(PdClient client)
 {
     if (ServerDisconnected == null) return;
     ServerDisconnected(client);
 }
Example #6
0
 private void RemoveClient(PdClient client)
 {
     Client = null;
 }
Example #7
0
 private void OnConnect(IAsyncResult ar)
 {
     PdClient instance = new PdClient();
     try
     {
         instance.Socket = (Socket) ar.AsyncState;
         instance.Socket.EndConnect(ar);
         instance.Socket.BeginReceive(instance.Buffer, 0, instance.Buffer.Length, SocketFlags.None, OnReceive,
             instance);
         Client = instance;
         OnServerConnected(instance);
     }
     catch (ObjectDisposedException ex)
     {
     }
     catch (Exception ex)
     {
         MessageBox.Show(string.Format("Error occured when attempting to connect to the server.\r\n{0}\r\n{1}", ex.Message, ex.StackTrace));
         Log.Exception(ex);
     }
 }
Example #8
0
 private void KillAndRemoveClient(PdClient client)
 {
     client.Kill();
     RemoveClient(client);
 }
Example #9
0
        private void HandleBuffer(PdClient client, byte[] buffer)
        {
            var length = buffer.Length;

            Array.Resize(ref buffer, length -= 4);

            BitConverter.GetBytes(length).CopyTo(buffer, 1);

            if (buffer[5] == 0x01)
            {
                BitConverter.GetBytes(BitConverter.ToUInt32(buffer, 6) ^ 0x98BADCFE).CopyTo(buffer, 6);
                client.Socket.Send(buffer);
            }
            else
            {
                // First packet, skip challenge and send success msg.
                if (client.State == ClientState.BeingChecked)
                {
                    client.Send(new byte[] {0x88, 0x07, 0x00, 0x00, 0x00, 0x00, 0x07});

                    client.State = ClientState.LoggingIn;
                }
                // Actual packets
                else
                {
                    // Start reading after the protocol header
                    var packet = new Packet(buffer, 6);

                    //Logger.Debug(packet);

                    try
                    {
                        this.Handlers.Handle(client, packet);
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex, "There has been a problem while handling '{0:X4}'.", packet.Op);
                    }
                }
            }
        }