コード例 #1
0
        /// <summary>
        /// Reads the response from the socket asynchronously.
        /// </summary>
        /// <param name="socket">The socket to read from.</param>
        /// <param name="next">The delegate which will continue processing the response. This is only called if the read completes asynchronoulsy.</param>
        /// <param name="ioPending">Set to true if the read is still pending when ReadASync returns. In this case 'next' will be called when the read is finished.</param>
        /// <returns>
        /// If the socket is already dead, ReadAsync returns false, next is not called, ioPending = false
        /// If the read completes synchronously (e.g. data is received from the buffer), it returns true/false depending on the StatusCode, and ioPending is set to true, 'next' will not be called.
        /// It returns true if it has to read from the socket, so the operation will complate asynchronously at a later time. ioPending will be true, and 'next' will be called to handle the data
        /// </returns>
        public bool ReadAsync(PooledSocket socket, Action <bool> next, out bool ioPending)
        {
            this.StatusCode  = -1;
            this._socket     = socket;
            this._nextAction = next;

            var asyncEvent = new AsyncIOArgs
            {
                Count = BinaryResponse.HeaderLength,
                Next  = this.DoDecodeHeaderAsync
            };

            this._shouldCallNext = true;
            if (socket.ReceiveAsync(asyncEvent))
            {
                ioPending = true;
                return(true);
            }

            ioPending            = false;
            this._shouldCallNext = false;

            return(asyncEvent.Fail
                                ? false
                                : this.DoDecodeHeader(asyncEvent, out ioPending));
        }
コード例 #2
0
        private void DoDecodeHeaderAsync(AsyncIOArgs asyncEvent)
        {
            this.shouldCallNext = true;
            bool tmp;

            this.DoDecodeHeader(asyncEvent, out tmp);
        }
コード例 #3
0
        /// <summary>
        /// Reads the response from the socket asynchronously.
        /// </summary>
        /// <param name="socket">The socket to read from.</param>
        /// <param name="next">The delegate whihc will continue processing the response. This is only called if the read completes asynchronoulsy.</param>
        /// <param name="ioPending">Set totrue if the read is still pending when ReadASync returns. In this case 'next' will be called when the read is finished.</param>
        /// <returns>
        /// If the socket is already dead, ReadAsync returns false, next is not called, ioPending = false
        /// If the read completes synchronously (e.g. data is received from the buffer), it returns true/false depending on the StatusCode, and ioPending is set to true, 'next' will not be called.
        /// It returns true if it has to read from the socket, so the operation will complate asynchronously at a later time. ioPending will be true, and 'next' will be called to handle the data
        /// </returns>
        public bool ReadAsync(PooledSocket socket, Action <bool> next, out bool ioPending)
        {
            this.StatusCode    = -1;
            this.currentSocket = socket;
            this.next          = next;

            var asyncEvent = new AsyncIOArgs();

            asyncEvent.Count = HeaderLength;
            asyncEvent.Next  = this.DoDecodeHeaderAsync;

            this.shouldCallNext = true;

            if (socket.ReceiveAsync(asyncEvent))
            {
                ioPending = true;
                return(true);
            }

            ioPending           = false;
            this.shouldCallNext = false;

            return(asyncEvent.Fail
                                        ? false
                                        : this.DoDecodeHeader(asyncEvent, out ioPending));
        }
コード例 #4
0
        public bool ReceiveAsync(AsyncIOArgs p)
        {
            CheckDisposed();

            if (!IsAlive)
            {
                p.Fail   = true;
                p.Result = null;
                return(false);
            }
            if (_helper2 == null)
            {
                _helper2 = new AsyncSocketHelper2(this);
            }
            return(_helper2.Read(p));
        }
コード例 #5
0
        private bool DoDecodeHeader(AsyncIOArgs asyncEvent, out bool pendingIO)
        {
            pendingIO = false;

            if (asyncEvent.Fail)
            {
                if (this.shouldCallNext)
                {
                    this.next(false);
                }

                return(false);
            }

            this.DeserializeHeader(asyncEvent.Result, out this.dataLength, out this.extraLength);
            var retval = this.StatusCode == 0;

            if (this.dataLength == 0)
            {
                if (this.shouldCallNext)
                {
                    this.next(retval);
                }
            }
            else
            {
                asyncEvent.Count = this.dataLength;
                asyncEvent.Next  = this.DoDecodeBodyAsync;

                if (this.currentSocket.ReceiveAsync(asyncEvent))
                {
                    pendingIO = true;
                }
                else
                {
                    if (asyncEvent.Fail)
                    {
                        return(false);
                    }

                    this.DoDecodeBody(asyncEvent);
                }
            }

            return(retval);
        }
コード例 #6
0
        void DoDecodeBody(AsyncIOArgs asyncEvent)
        {
            if (asyncEvent.Fail)
            {
                if (this._shouldCallNext)
                {
                    this._nextAction(false);
                }
                return;
            }

            this.Extra = new ArraySegment <byte>(asyncEvent.Result, 0, this._extraLength);
            this.Data  = new ArraySegment <byte>(asyncEvent.Result, this._extraLength, this._dataLength - this._extraLength);

            if (this._shouldCallNext)
            {
                this._nextAction(true);
            }
        }
コード例 #7
0
        /// <summary>
        /// returns true if io is pending
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool Read(AsyncIOArgs p)
        {
            var count = p.Count;

            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("count", "count must be > 0");
            }
#if DEBUG_IO
            if (Interlocked.CompareExchange(ref this.doingIO, 1, 0) != 0)
            {
                throw new InvalidOperationException("Receive is already in progress");
            }
#endif
            this.expectedToRead = p.Count;
            this.pendingArgs    = p;

            p.Fail   = false;
            p.Result = null;

            if (this.asyncBuffer.Available >= count)
            {
                PublishResult(false);

                return(false);
            }
            else
            {
                this.remainingRead = count - this.asyncBuffer.Available;
                this.isAborted     = 0;

                this.BeginReceive();

                return(true);
            }
        }
コード例 #8
0
 void DoDecodeBodyAsync(AsyncIOArgs asyncEvent)
 {
     this._shouldCallNext = true;
     this.DoDecodeBody(asyncEvent);
 }
コード例 #9
0
 void DoDecodeHeaderAsync(AsyncIOArgs asyncEvent)
 {
     this._shouldCallNext = true;
     this.DoDecodeHeader(asyncEvent, out var tmp);
 }
コード例 #10
0
        /// <summary>
        /// Reads the response from the socket asynchronously.
        /// </summary>
        /// <param name="socket">The socket to read from.</param>
        /// <param name="next">The delegate whihc will continue processing the response. This is only called if the read completes asynchronoulsy.</param>
        /// <param name="ioPending">Set totrue if the read is still pending when ReadASync returns. In this case 'next' will be called when the read is finished.</param>
        /// <returns>
        /// If the socket is already dead, ReadAsync returns false, next is not called, ioPending = false
        /// If the read completes synchronously (e.g. data is received from the buffer), it returns true/false depending on the StatusCode, and ioPending is set to true, 'next' will not be called.
        /// It returns true if it has to read from the socket, so the operation will complate asynchronously at a later time. ioPending will be true, and 'next' will be called to handle the data
        /// </returns>
        public bool ReadAsync(PooledSocket socket, Action<bool> next, out bool ioPending)
        {
            this.StatusCode = -1;
            this.currentSocket = socket;
            this.next = next;

            var asyncEvent = new AsyncIOArgs();

            asyncEvent.Count = HeaderLength;
            asyncEvent.Next = this.DoDecodeHeaderAsync;

            this.shouldCallNext = true;

            if (socket.ReceiveAsync(asyncEvent))
            {
                ioPending = true;
                return true;
            }

            ioPending = false;
            this.shouldCallNext = false;

            return asyncEvent.Fail
                    ? false
                    : this.DoDecodeHeader(asyncEvent, out ioPending);
        }
コード例 #11
0
        private void DoDecodeHeaderAsync(AsyncIOArgs asyncEvent)
        {
            this.shouldCallNext = true;
            bool tmp;

            this.DoDecodeHeader(asyncEvent, out tmp);
        }
コード例 #12
0
        private bool DoDecodeHeader(AsyncIOArgs asyncEvent, out bool pendingIO)
        {
            pendingIO = false;

            if (asyncEvent.Fail)
            {
                if (this.shouldCallNext) this.next(false);

                return false;
            }

            this.DeserializeHeader(asyncEvent.Result, out this.dataLength, out this.extraLength);
            var retval = this.StatusCode == 0;

            if (this.dataLength == 0)
            {
                if (this.shouldCallNext) this.next(retval);
            }
            else
            {
                asyncEvent.Count = this.dataLength;
                asyncEvent.Next = this.DoDecodeBodyAsync;

                if (this.currentSocket.ReceiveAsync(asyncEvent))
                {
                    pendingIO = true;
                }
                else
                {
                    if (asyncEvent.Fail) return false;

                    this.DoDecodeBody(asyncEvent);
                }
            }

            return retval;
        }
コード例 #13
0
 private void DoDecodeBodyAsync(AsyncIOArgs asyncEvent)
 {
     this.shouldCallNext = true;
     DoDecodeBody(asyncEvent);
 }
コード例 #14
0
        private void DoDecodeBody(AsyncIOArgs asyncEvent)
        {
            if (asyncEvent.Fail)
            {
                if (this.shouldCallNext) this.next(false);

                return;
            }

            this.Extra = new ArraySegment<byte>(asyncEvent.Result, 0, this.extraLength);
            this.Data = new ArraySegment<byte>(asyncEvent.Result, this.extraLength, this.dataLength - this.extraLength);

            if (this.shouldCallNext) this.next(true);
        }
コード例 #15
0
 private void DoDecodeBodyAsync(AsyncIOArgs asyncEvent)
 {
     this.shouldCallNext = true;
     DoDecodeBody(asyncEvent);
 }