BytesRoad.Net.Sockets.Socket_HttpConnect.ReadReply_Recv_End C# (CSharp) Method

ReadReply_Recv_End() private method

private ReadReply_Recv_End ( IAsyncResult ar ) : void
ar IAsyncResult
return void
        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();
            }
        }