Example #1
0
        void ReadReply_Recv_End(IAsyncResult ar)
        {
            ReadReply_SO stateObj = (ReadReply_SO)ar.AsyncState;

            try
            {
                stateObj.UpdateContext();
                int num = EndReceive(ar);
                if (0 == num)
                {
                    stateObj.SetCompleted();
                }
                else
                {
                    stateObj.Reply.Add(stateObj.Buffer, 0, num);

                    // handle the end of reply
                    int afterEndPos = FindReplyEnd(
                        stateObj.Reply.Data,
                        stateObj.Reply.Size);

                    if (afterEndPos > 0)
                    {
                        if (afterEndPos < num) // read after reply finished?
                        {
                            // put data back into the buffer for further
                            // processing in receive functions
                            PutBufferData(stateObj.Buffer, afterEndPos, num - afterEndPos);
                            stateObj.Reply.CutTail(num - afterEndPos);
                        }

                        stateObj.SetCompleted();
                    }
                    else
                    {
                        if (stateObj.Reply.Size > _maxReplySize)
                        {
                            throw new ProtocolViolationException("Web proxy reply exceed maximum length.");
                        }

                        BeginReceive(
                            stateObj.Buffer,
                            0,
                            stateObj.Buffer.Length,
                            new AsyncCallback(ReadReply_Recv_End),
                            stateObj);
                    }
                }
            }
            catch (Exception e)
            {
                stateObj.Exception = e;
                stateObj.SetCompleted();
            }
        }
Example #2
0
        private void ReadReply_Recv_End(IAsyncResult ar)
        {
            ReadReply_SO asyncState = (ReadReply_SO)ar.AsyncState;

            try
            {
                asyncState.UpdateContext();
                int length = this.EndReceive(ar);
                if (length == 0)
                {
                    asyncState.SetCompleted();
                }
                else
                {
                    asyncState.Reply.Add(asyncState.Buffer, 0, length);
                    int offset = this.FindReplyEnd(asyncState.Reply.Data, asyncState.Reply.Size);
                    if (offset > 0)
                    {
                        if (offset < length)
                        {
                            this.PutBufferData(asyncState.Buffer, offset, length - offset);
                            asyncState.Reply.CutTail(length - offset);
                        }
                        asyncState.SetCompleted();
                    }
                    else
                    {
                        if (asyncState.Reply.Size > this._maxReplySize)
                        {
                            throw new ProtocolViolationException("Web proxy reply exceed maximum length.");
                        }
                        this.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, new AsyncCallback(this.ReadReply_Recv_End), asyncState);
                    }
                }
            }
            catch (Exception exception)
            {
                asyncState.Exception = exception;
                asyncState.SetCompleted();
            }
        }