Example #1
0
        private void ReceiveData(IAsyncResult ar)
        {
            try
            {
                this.nlock.AcquireWriterLock(Timeout.Infinite);
                if (this.isDisconnected)
                {
                    this.nlock.ReleaseWriterLock();
                    return;
                }
                if (!sock.Connected)
                {
                    this.nlock.ReleaseWriterLock();
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                try { stream.EndRead(ar); }
                catch (Exception)
                {
                    this.nlock.ReleaseWriterLock();
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                byte[] raw = (byte[])ar.AsyncState;
                if (this.isGateway)
                {
                    raw = Encryption.Decrypt(raw, 2, this.ClientKey);
                }
                if (!isGateway)
                {
                    ushort messageID = (ushort)(raw[7] + (raw[6] << 8));

                    if (!this.commandTable.ContainsKey((messageID)))
                    {
                        if (!this.fullHeader)
                        {
                            Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null);
                        }
                        else
                        {
                            if (commandTable.ContainsKey((ushort)0xFFFF))
                            {
                                if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length))
                                {
                                    Packet p = this.commandTable[(ushort)0xFFFF].New();
                                    p.data = raw;
                                    p.size = (ushort)(raw.Length);

                                    ClientManager.EnterCriticalArea();
                                    try
                                    {
                                        p.Parse(this.client);
                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.ShowError(ex);
                                    }
                                    ClientManager.LeaveCriticalArea();
                                }
                                else
                                {
                                    string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                    Console.WriteLine(error);
                                    Log.WriteLog(error);
                                    ClientManager.EnterCriticalArea();
                                    this.Disconnect();
                                    ClientManager.LeaveCriticalArea();
                                    return;
                                }
                            }
                            else
                            {
                                Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null);
                            }
                        }
                    }
                    else
                    {
                        if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length))
                        {
                            Packet p = this.commandTable[messageID].New();
                            p.data = raw;
                            p.size = (ushort)(raw.Length);
                            Client client;
                            if (p.SessionID != 0)
                            {
                                client = this.currentClientManager.GetClient(p.SessionID);
                                if (client == null)
                                {
                                    client = this.client;
                                }
                            }
                            else
                            {
                                client = this.client;
                            }
                            ClientManager.EnterCriticalArea();
                            try
                            {
                                if (client.netIO == null)
                                {
                                    client.netIO = this;
                                }
                                p.Parse(client);
                            }
                            catch (Exception ex)
                            {
                                Logger.ShowError(ex);
                            }
                            ClientManager.LeaveCriticalArea();
                        }
                        else
                        {
                            string error = string.Format("Invalid packet size(Packet:{0:X4}) from client {1}", messageID, sock.RemoteEndPoint.ToString());
                            Console.WriteLine(error);
                            Log.WriteLog(error);
                            this.nlock.ReleaseWriterLock();
                            ClientManager.EnterCriticalArea();
                            this.Disconnect();
                            ClientManager.LeaveCriticalArea();
                            return;
                        }
                    }
                }
                else
                {
                    ushort messageID;
                    if (!fullHeader)
                    {
                        messageID = (ushort)(raw[7] + (raw[6] << 8));

                        if (!this.commandTable.ContainsKey((messageID)))
                        {
                            Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null);
                        }
                        else
                        {
                            if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length))
                            {
                                Packet p = this.commandTable[messageID].New();
                                p.data      = raw;
                                p.size      = (ushort)(raw.Length);
                                p.isGateway = this.isGateway;
                                ClientManager.EnterCriticalArea();
                                try
                                {
                                    p.Parse(this.client);
                                }
                                catch (Exception ex)
                                {
                                    Logger.ShowError(ex);
                                }
                                ClientManager.LeaveCriticalArea();
                            }
                            else
                            {
                                string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                Console.WriteLine(error);
                                Log.WriteLog(error);
                                this.nlock.ReleaseWriterLock();
                                ClientManager.EnterCriticalArea();
                                this.Disconnect();
                                ClientManager.LeaveCriticalArea();
                                return;
                            }
                        }
                    }
                    else
                    {
                        if (commandTable.ContainsKey((ushort)0xFFFF))
                        {
                            if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length))
                            {
                                Packet p = this.commandTable[(ushort)0xFFFF].New();
                                p.data      = raw;
                                p.size      = (ushort)(raw.Length);
                                p.isGateway = this.isGateway;
                                ClientManager.EnterCriticalArea();
                                try
                                {
                                    p.Parse(this.client);
                                }
                                catch (Exception ex)
                                {
                                    Logger.ShowError(ex);
                                }
                                ClientManager.LeaveCriticalArea();
                            }
                            else
                            {
                                string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                Console.WriteLine(error);
                                Log.WriteLog(error);
                                this.nlock.ReleaseWriterLock();
                                ClientManager.EnterCriticalArea();
                                this.Disconnect();
                                ClientManager.LeaveCriticalArea();
                                return;
                            }
                        }
                        else
                        {
                            Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null);
                        }
                    }
                }
                try { this.nlock.ReleaseWriterLock(); stream.BeginRead(buffer, 0, 2, this.callbackSize, null); }
                catch (Exception)
                {
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
            }
            catch (Exception e) { Logger.ShowError(e, null); }
        }