Ejemplo n.º 1
0
        protected virtual void AsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred <= 0)
            {
                Offline();
                return;
            }



            if (e.SocketError != SocketError.Success)
            {
                Offline();
                return;
            }

            ReceivedBytes += e.BytesTransferred - e.Offset;

            for (int i = e.Offset; i < e.Offset + e.BytesTransferred; i++)
            {
                frameStream.WriteByte(e.Buffer[i]);
                frameStream.Flush();
                if (isNoCheck && noCheckCount > 0)
                {
                    noCheckCount--;
                    continue;
                }
                else if (isNoCheck)
                {
                    isNoCheck    = false;
                    noCheckCount = 0;
                }

                if (Protocol.IsFrameEnd(frameStream))
                {
                    frameStream.Position = 0;
                    INetCommand command = Protocol.GetCommand(this, frameStream);
                    if (command != null)
                    {
                        frameStream.Position = 0;
                        frameStream.SetLength(0);
                        command = command.Execute(this);
                    }
                    Protocol.WriteCommand(command, this);
                }
            }



            if (!isClosed)
            {
                Protocol.TryGetCommand(this);
            }
        }
        public virtual void Start()
        {
            OnSessionStarted();

            while (true)
            {
                if (isClosed)
                {
                    break;
                }
                INetCommand command = protocol.GetCommand(this);
                if (isClosed)
                {
                    break;
                }
                if (command != null)
                {
                    command = command.Execute(this);
                }
                protocol.WriteCommand(command, this);
            }
        }