예제 #1
0
        private void OnReceive(IAsyncResult ar)
        {
            if (this.IsAlive)
            {
                try
                {
                    int received = this.Socket.EndReceive(ar);

                    if (received == 0)
                    {
                        this.Stop();
                    }
                    else
                    {
                        this.ReceivalBuffer.Limit += received;

                        if (this.Cryptograph is MapleCryptograph)
                        {
                            int  processed = 0;
                            bool reset     = false;

                            if (this.ReceivalBuffer.Remaining < 4)
                            {
                                Log.Error("TODO: Remaining < 4!");
                            }

                            while (this.ReceivalBuffer.Remaining >= 4)
                            {
                                int length = AesCryptograph.RetrieveLength(this.ReceivalBuffer.ReadBytes(4));

                                if (this.ReceivalBuffer.Remaining < length)
                                {
                                    this.ReceivalBuffer.Position -= 4;

                                    Buffer.BlockCopy(this.ReceivalBuffer.Array, this.ReceivalBuffer.Position, this.ReceivalBuffer.Array, 0, this.ReceivalBuffer.Remaining);

                                    reset = true;

                                    break;
                                }
                                else
                                {
                                    this.ReceivalBuffer.Position -= 4;

                                    this.Handle(this.ReceivalBuffer.ReadBytes(length + 4));

                                    processed += (length + 4);
                                }
                            }

                            this.ReceivalBuffer.Limit -= processed;

                            if (reset)
                            {
                                this.ReceivalBuffer.Position = 0;
                            }
                            else
                            {
                                this.ReceivalBuffer.Position = this.ReceivalBuffer.Limit;
                            }
                        }
                        else
                        {
                            this.Handle(this.ReceivalBuffer.GetContent());
                            this.ReceivalBuffer.Limit    = 0;
                            this.ReceivalBuffer.Position = 0;
                        }
                    }

                    this.ReceiveDone.Set();
                }
                catch (Exception e)
                {
                    Log.Error("Uncatched fatal error on {0}: ", e, this.Title.ToLower(), Thread.CurrentThread.ManagedThreadId);
                    this.Stop();
                }
            }
        }
예제 #2
0
        private void OnReceive(IAsyncResult ar)
        {
            if (!IsAlive)
            {
                return;
            }

            try
            {
                int received = Socket.EndReceive(ar);

                if (received != 0)
                {
                    ReceivalBuffer.Limit += received;

                    if (Cryptograph is MapleCryptograph)
                    {
                        int  processed = 0;
                        bool reset     = false;

                        if (ReceivalBuffer.Remaining < 4)
                        {
                            Log.Error("TODO: Remaining < 4!");
                        }

                        while (ReceivalBuffer.Remaining >= 4)
                        {
                            int length = AesCryptograph.RetrieveLength(ReceivalBuffer.ReadBytes(4));

                            if (ReceivalBuffer.Remaining < length)
                            {
                                ReceivalBuffer.Position -= 4;

                                try
                                {
                                    Buffer.BlockCopy(ReceivalBuffer.Array, ReceivalBuffer.Position,
                                                     ReceivalBuffer.Array, 0, ReceivalBuffer.Remaining);
                                }

                                catch (Exception e)
                                {
                                    Log.SkipLine();
                                    Tracer.TraceErrorMessage(e, "Failed to BlockCopy!");
                                    Log.SkipLine();
                                    throw;
                                }

                                reset = true;

                                break;
                            }

                            ReceivalBuffer.Position -= 4;

                            HandleIncomingPacket(ReceivalBuffer.ReadBytes(length + 4));

                            processed += (length + 4);
                        }

                        ReceivalBuffer.Limit -= processed;

                        ReceivalBuffer.Position = reset ? 0 : ReceivalBuffer.Limit;
                    }

                    else
                    {
                        HandleIncomingPacket(ReceivalBuffer.GetContent());
                        ReceivalBuffer.Limit    = 0;
                        ReceivalBuffer.Position = 0;
                    }
                }

                else
                {
                    Stop();
                }

                ReceiveDone.Set();
            }

            catch (ArgumentNullException ex)
            {
                Log.SkipLine();
                Tracer.TraceErrorMessage(ex, "Argument is null!");
                Log.SkipLine();
                throw;
            }

            catch (ArgumentOutOfRangeException ex)
            {
                Log.SkipLine();
                Tracer.TraceErrorMessage(ex, "Argument is out of range!");
                Log.SkipLine();
                throw;
            }

            catch (SocketException ex)
            {
                Log.SkipLine();
                Tracer.TraceErrorMessage(ex, "Socket exception!");
                Log.SkipLine();
                throw;
            }

            catch (Exception ex)
            {
                Log.SkipLine();
                Log.Error("Uncatched fatal error on {0}: ", ex, Title.ToLower(), Thread.CurrentThread.ManagedThreadId);
                Stop();
            }
        }