예제 #1
0
        /// <summary>
        /// Despatches received data from the arg through to terrarias internal buffers
        /// </summary>
        protected virtual void DespatchData(ReceiveArgs args)
        {
            var           id  = this.RemoteClient.Id;
            MessageBuffer obj = NetMessage.buffer[id];

            lock (obj)
            {
                if (!this.RemoteClient.IsActive)
                {
                    this.RemoteClient.IsActive = true;
                    this.RemoteClient.State    = 0;
                }

                Buffer.BlockCopy(args.Buffer, args.Offset, NetMessage.buffer[id].readBuffer, NetMessage.buffer[id].totalData, recvBytes);
                NetMessage.buffer[id].totalData += recvBytes;
                NetMessage.buffer[id].checkBytes = true;

                recvBytes = 0;
            }
        }
예제 #2
0
        public virtual void ReceiveCompleted(ReceiveArgs args)
        {
            try
            {
                bool release = false;

                if (args.SocketError != SocketError.Success)
                {
                    release = true;
                    HandleError(args.SocketError);
                }
                else if (args.BytesTransferred == 0)
                {
                    release = true;
                    HandleError(SocketError.Disconnecting);
                }
                else
                {
                    var receiving = false;
                    while (!receiving)
                    {
                        recvBytes += args.BytesTransferred;

                        this.DespatchData(args);

                        var left = args.Buffer.Length - recvBytes;
                        if (left <= 0)
                        {
                            return;
                        }

                        args.SetBuffer(args.Buffer, recvBytes, left);
                        try
                        {
                            receiving = args.origin.ReceiveAsync(args);
                        }
                        catch (ObjectDisposedException)
                        {
                            receiving = false;
                        }
                    }

                    if (!receiving)
                    {
                        release = true;
                    }
                }

                if (release)
                {
                    args.conn   = null;
                    args.origin = null;
                    Server.ReceiveSocketPool.PushBack(args);
                }

                this.RemoteClient.IsReading = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }