static bool ProcessAsyncResponseStreamResult(XmlRpcAsyncResult result,
                                                     IAsyncResult asyncResult)
        {
            int  endReadLen = result.ResponseStream.EndRead(asyncResult);
            long contLen    = result.Response.ContentLength;
            bool completed;

            if (endReadLen == 0)
            {
                completed = true;
            }
            else if (contLen > 0 && endReadLen == contLen)
            {
                result.ResponseBufferedStream = new MemoryStream(result.Buffer);
                completed = true;
            }
            else
            {
                if (result.ResponseBufferedStream == null)
                {
                    result.ResponseBufferedStream = new MemoryStream(result.Buffer.Length);
                }
                result.ResponseBufferedStream.Write(result.Buffer, 0, endReadLen);
                completed = false;
            }
            if (completed)
            {
                result.Complete();
            }
            return(completed);
        }
        static void ReadAsyncResponseStream(XmlRpcAsyncResult result)
        {
            IAsyncResult asyncResult;

            do
            {
                byte[] buff    = result.Buffer;
                long   contLen = result.Response.ContentLength;
                if (buff == null)
                {
                    if (contLen == -1)
                    {
                        result.Buffer = new Byte[1024];
                    }
                    else
                    {
                        result.Buffer = new Byte[contLen];
                    }
                }
                else
                {
                    if (contLen != -1 && contLen > result.Buffer.Length)
                    {
                        result.Buffer = new Byte[contLen];
                    }
                }
                buff        = result.Buffer;
                asyncResult = result.ResponseStream.BeginRead(buff, 0, buff.Length,
                                                              new AsyncCallback(ReadResponseCallback), result);
                if (!asyncResult.CompletedSynchronously)
                {
                    return;
                }
            }while (!(ProcessAsyncResponseStreamResult(result, asyncResult)));
        }
        static void GetRequestStreamCallback(IAsyncResult asyncResult)
        {
            XmlRpcAsyncResult clientResult
                = (XmlRpcAsyncResult)asyncResult.AsyncState;

            clientResult.CompletedSynchronously = asyncResult.CompletedSynchronously;
            try
            {
                Stream serStream = null;
                Stream reqStream = null;
                bool   logging   = (clientResult.ClientProtocol.RequestEvent != null);
                if (!logging)
                {
                    serStream             = reqStream
                                          = clientResult.Request.EndGetRequestStream(asyncResult);
                }
                else
                {
                    serStream = new MemoryStream(2000);
                }
                try
                {
                    XmlRpcRequest    req        = clientResult.XmlRpcRequest;
                    XmlRpcSerializer serializer = new XmlRpcSerializer();
                    if (clientResult.XmlEncoding != null)
                    {
                        serializer.XmlEncoding = clientResult.XmlEncoding;
                    }
                    serializer.UseEmptyParamsTag = clientResult.UseEmptyParamsTag;
                    serializer.UseIndentation    = clientResult.UseIndentation;
                    serializer.Indentation       = clientResult.Indentation;
                    serializer.UseIntTag         = clientResult.UseIntTag;
                    serializer.UseStringTag      = clientResult.UseStringTag;
                    serializer.SerializeRequest(serStream, req);
                    if (logging)
                    {
                        reqStream          = clientResult.Request.EndGetRequestStream(asyncResult);
                        serStream.Position = 0;
                        Util.CopyStream(serStream, reqStream);
                        reqStream.Flush();
                        serStream.Position = 0;
                        clientResult.ClientProtocol.OnRequest(
                            new XmlRpcRequestEventArgs(req.proxyId, req.number, serStream));
                    }
                }
                finally
                {
                    if (reqStream != null)
                    {
                        reqStream.Close();
                    }
                }
                clientResult.Request.BeginGetResponse(
                    new AsyncCallback(GetResponseCallback), clientResult);
            }
            catch (Exception ex)
            {
                ProcessAsyncException(clientResult, ex);
            }
        }
        public IAsyncResult BeginInvoke(
            MethodInfo mi,
            object[] parameters,
            object clientObj,
            AsyncCallback callback,
            object outerAsyncState)
        {
            string        useUrl    = GetEffectiveUrl(clientObj);
            WebRequest    webReq    = GetWebRequest(new Uri(useUrl));
            XmlRpcRequest xmlRpcReq = MakeXmlRpcRequest(webReq, mi,
                                                        parameters, clientObj, _xmlRpcMethod, _id);

            SetProperties(webReq);
            SetRequestHeaders(_headers, webReq);

            SetClientCertificates(_clientCertificates, webReq);
            Encoding useEncoding = null;

            if (_xmlEncoding != null)
            {
                useEncoding = _xmlEncoding;
            }
            XmlRpcAsyncResult asr = new XmlRpcAsyncResult(this, xmlRpcReq,
                                                          useEncoding, _useEmptyParamsTag, _useIndentation, _indentation,
                                                          _useIntTag, _useStringTag, webReq, callback, outerAsyncState, 0);

            webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),
                                         asr);
            if (!asr.IsCompleted)
            {
                asr.CompletedSynchronously = false;
            }
            return(asr);
        }
        public object EndInvoke(
            IAsyncResult asr,
            Type returnType)
        {
            object reto           = null;
            Stream responseStream = null;

            try
            {
                XmlRpcAsyncResult clientResult = (XmlRpcAsyncResult)asr;
                if (clientResult.Exception != null)
                {
                    throw clientResult.Exception;
                }
                if (clientResult.EndSendCalled)
                {
                    throw new Exception("dup call to EndSend");
                }
                clientResult.EndSendCalled = true;
                HttpWebResponse webResp = (HttpWebResponse)clientResult.WaitForResponse();

                clientResult._responseCookies = webResp.Cookies;
                clientResult._responseHeaders = webResp.Headers;

                responseStream = clientResult.ResponseBufferedStream;
                if (ResponseEvent != null)
                {
                    OnResponse(new XmlRpcResponseEventArgs(
                                   clientResult.XmlRpcRequest.proxyId,
                                   clientResult.XmlRpcRequest.number,
                                   responseStream));
                    responseStream.Position = 0;
                }

                responseStream = MaybeDecompressStream((HttpWebResponse)webResp,
                                                       responseStream);

                XmlRpcResponse resp = ReadResponse(clientResult.XmlRpcRequest,
                                                   webResp, responseStream, returnType);
                reto = resp.retVal;
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
            }
            return(reto);
        }
        static void ProcessAsyncException(XmlRpcAsyncResult clientResult,
                                          Exception ex)
        {
            WebException webex = ex as WebException;

            if (webex != null && webex.Response != null)
            {
                clientResult.Response = webex.Response;
                return;
            }
            if (clientResult.IsCompleted)
            {
                throw new Exception("error during async processing");
            }
            clientResult.Complete(ex);
        }
 static void ReadAsyncResponse(XmlRpcAsyncResult result)
 {
     if (result.Response.ContentLength == 0)
     {
         result.Complete();
         return;
     }
     try
     {
         result.ResponseStream = result.Response.GetResponseStream();
         ReadAsyncResponseStream(result);
     }
     catch (Exception ex)
     {
         ProcessAsyncException(result, ex);
     }
 }
        static void GetResponseCallback(IAsyncResult asyncResult)
        {
            XmlRpcAsyncResult result = (XmlRpcAsyncResult)asyncResult.AsyncState;

            result.CompletedSynchronously = asyncResult.CompletedSynchronously;
            try
            {
                result.Response = result.ClientProtocol.GetWebResponse(result.Request,
                                                                       asyncResult);
            }
            catch (Exception ex)
            {
                ProcessAsyncException(result, ex);
                if (result.Response == null)
                {
                    return;
                }
            }
            ReadAsyncResponse(result);
        }
        static void ReadResponseCallback(IAsyncResult asyncResult)
        {
            XmlRpcAsyncResult result = (XmlRpcAsyncResult)asyncResult.AsyncState;

            result.CompletedSynchronously = asyncResult.CompletedSynchronously;
            if (asyncResult.CompletedSynchronously)
            {
                return;
            }
            try
            {
                bool completed = ProcessAsyncResponseStreamResult(result, asyncResult);
                if (!completed)
                {
                    ReadAsyncResponseStream(result);
                }
            }
            catch (Exception ex)
            {
                ProcessAsyncException(result, ex);
            }
        }