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);
        }
        private 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)));
        }
        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;

                if (clientResult.XmlRpcRequest != null && returnType != null)
                {
                    clientResult.XmlRpcRequest.ReturnType = returnType;
                }

                var 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;
                }
#if (!COMPACT_FRAMEWORK && !FX1_0 && !SILVERLIGHT)
                responseStream = MaybeDecompressStream(webResp, responseStream);
#endif
                XmlRpcResponse resp = ReadResponse(clientResult.XmlRpcRequest,
                                                   webResp, responseStream);
                reto = resp.retVal;
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
            }
            return(reto);
        }
        static void ProcessAsyncException(XmlRpcAsyncResult clientResult, Exception ex)
        {
            if (ex is WebException webex && webex.Response != null)
            {
                clientResult.Response = webex.Response;
                return;
            }

            if (clientResult.IsCompleted)
            {
                throw new Exception("error during async processing");
            }

            clientResult.Complete(ex);
        }
 private 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);
     }
 }
        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);
#if (!COMPACT_FRAMEWORK && !SILVERLIGHT)
            SetClientCertificates(ClientCertificates, webReq);
#endif
            var asr = new XmlRpcAsyncResult(this, xmlRpcReq, XmlRpcFormatSettings,
                                            webReq, callback, outerAsyncState, 0);
            webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), asr);

            if (!asr.IsCompleted)
            {
                asr.CompletedSynchronously = false;
            }

            return(asr);
        }