Пример #1
0
        private ResponseAccessor <TReturnType> EndGetRequest <TReturnType>(ManualAsyncResult result)
        {
            try
            {
                result.TimeOutWait();

                if (result.Error != null)
                {
                    if (Con.AutoHost)
                    {
                        result = result.RequestRetryHandler.RetryRequest();
                    }
                }

                var responseStream = result.Response.GetResponseStream();

                var responseAccesor = ReadStream(result, responseStream, 0);

                return(new ResponseAccessor <TReturnType>()
                {
                    Content = JsonConvert.DeserializeObject <TReturnType>(responseAccesor.Content),
                });
            }
            catch (Exception ex)
            {
                return(new ResponseAccessor <TReturnType>()
                {
                    Ex = ex
                });
            }
        }
Пример #2
0
        // ----------------------- post ---------------------- //

        internal ManualAsyncResult RequestPost(ManualAsyncResult result)
        {
            try
            {
                result.HttpWebRequest.BeginGetRequestStream(result.WrapHandler(ar =>
                {
                    try
                    {
                        var stream = result.HttpWebRequest.EndGetRequestStream(ar);

                        stream.Write(result.Bytes, 0, result.Bytes.Length);
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                    }
                }), null);
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }
        internal AsyncRequestRetryHandler(HttpAsyncConnector httpConnector, ManualAsyncResult result)
        {
            RequestRetries = 0;

            HttpAsyncConnector = httpConnector;

            Result = result;
        }
Пример #4
0
        private ResponseAccessor <TReturnType> EndPostRequest <TReturnType>(ManualAsyncResult result)
        {
            try
            {
                result.TimeOutWait();

                if (result.Error != null)
                {
                    if (Con.AutoHost)
                    {
                        result = result.RequestRetryHandler.RetryRequest();
                    }
                }

                Stream responseStream = null;

                var temp = new ManualAsyncResult();

                result.HttpWebRequest.BeginGetResponse(temp.WrapHandler(ar =>
                {
                    try
                    {
                        var response = result.HttpWebRequest.EndGetResponse(ar);

                        responseStream = response.GetResponseStream();
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                    }
                }), null);

                temp.TimeOutWait();

                if (result.Error != null)
                {
                    return(new ResponseAccessor <TReturnType>()
                    {
                        Ex = result.Error
                    });
                }

                var responseAccessor = ReadStream(result, responseStream, 0);

                return(new ResponseAccessor <TReturnType>()
                {
                    Content = JsonConvert.DeserializeObject <TReturnType>(responseAccessor.Content),
                });
            }
            catch (Exception ex)
            {
                return(new ResponseAccessor <TReturnType>()
                {
                    Ex = ex
                });
            }
        }
Пример #5
0
        internal TReturnType CompletePostRequest <TReturnType>(ManualAsyncResult result)
        {
            var response = EndPostRequest <TReturnType>(result);

            if (response.Ex != null)
            {
                throw response.Ex;
            }

            return(response.Content);
        }
Пример #6
0
        private ManualAsyncResult SetUpHttpWebRequest(ManualAsyncResult result, string requestType, int timeout)
        {
            var http = (HttpWebRequest)WebRequest.Create(Con.GetUri(result.Path, result.Query).Uri);

            http.Accept           = "application/json";
            http.ContentType      = "application/json";
            http.Method           = requestType;
            http.ReadWriteTimeout = timeout;
            http.Timeout          = timeout;

            result.HttpWebRequest = http;

            return(result);
        }
Пример #7
0
        // ----------------------- get ----------------------- //

        internal ManualAsyncResult RequestGet(ManualAsyncResult result)
        {
            try
            {
                result.HttpWebRequest.BeginGetResponse(result.WrapHandler(ar =>
                {
                    result.Response = result.HttpWebRequest.EndGetResponse(ar);
                }), null);
            }

            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }
Пример #8
0
        internal ManualAsyncResult PreparePostRequest(ManualAsyncResult asyncResult)
        {
            asyncResult = SetUpHttpWebRequest(asyncResult, "Post", 5000);

            asyncResult.RequestRetryHandler = new RequestRetryHandler(this, asyncResult);

            try
            {
                asyncResult = RequestPost(asyncResult);
            }
            catch (Exception ex)
            {
                asyncResult.Error = ex;
            }


            return(asyncResult);
        }
Пример #9
0
        internal ManualAsyncResult PrepareGetRequestAsync <TReturnType>(Action <ResponseAccessor <TReturnType> > callback, string path, string query = null)
        {
            var asyncResult = new ManualAsyncResult
            {
                Path  = path,
                Query = query,
            };

            asyncResult = SetUpHttpWebRequest(asyncResult, "GET", 5000);

            asyncResult.AsyncRequestRetryHandler = new AsyncRequestRetryHandler(this, asyncResult);

            try
            {
                asyncResult.AsyncRequestRetryHandler.SetCallback(body =>
                {
                    if (body.Ex != null)
                    {
                        callback(new ResponseAccessor <TReturnType>
                        {
                            Ex = body.Ex
                        });
                    }
                    else
                    {
                        callback(new ResponseAccessor <TReturnType>
                        {
                            Content = JsonConvert.DeserializeObject <TReturnType>(body.Content),
                        });
                    }
                });

                asyncResult = RequestGetAsync(asyncResult.AsyncRequestRetryHandler.Callback, asyncResult);
            }
            catch (Exception ex)
            {
                callback(new ResponseAccessor <TReturnType>
                {
                    Ex = ex
                });
            }

            return(asyncResult);
        }
Пример #10
0
        internal ManualAsyncResult PrepareGetRequest(string path, string query = null)
        {
            var asyncResult = new ManualAsyncResult
            {
                Path  = path,
                Query = query,
            };

            asyncResult = SetUpHttpWebRequest(asyncResult, "GET", 5000);

            asyncResult.RequestRetryHandler = new RequestRetryHandler(this, asyncResult);

            try
            {
                asyncResult = RequestGet(asyncResult);
            }
            catch (Exception ex)
            {
                asyncResult.Error = ex;
            }

            return(asyncResult);
        }
Пример #11
0
        internal ManualAsyncResult RetryRequest()
        {
            try
            {
                HttpConnector.Con.SetNewHost();

                if (Result.HttpWebRequest.Method == "GET")
                {
                    Result = HttpConnector.RequestGet(Result);
                }

                else if (Result.HttpWebRequest.Method == "Post")
                {
                    Result = HttpConnector.RequestPost(Result);
                }

                Result.TimeOutWait();

                if (Result.Error == null)
                {
                    return(Result);
                }

                throw Result.Error;
            }
            catch (Exception)
            {
                RequestRetries++;

                if (RequestRetries < 5)
                {
                    return(RetryRequest());
                }

                throw new Exception("Too Many Request Failures");
            }
        }
Пример #12
0
        // ------------------------- read -----------------------//

        private ResponseAccessor <string> ReadStream(ManualAsyncResult result, Stream stream, int len)
        {
            var done = false;

            var buffer = new byte[20000];

            var response = new ResponseAccessor <string>();

            do
            {
                try
                {
                    var innerResult = new ManualAsyncResult();

                    stream.BeginRead(buffer, len, 20000, result.WrapHandler(innerResult.WrapHandler(ar =>
                    {
                        try
                        {
                            var readLen = stream.EndRead(ar);

                            if (readLen == 0)
                            {
                                stream.Close();

                                done = true;

                                response = new ResponseAccessor <string>
                                {
                                    Content = Encoding.UTF8.GetString(buffer, 0, len)
                                };

                                innerResult.m_waitHandle.Set();
                                return;
                            }

                            var temp = buffer;

                            Array.Resize(ref temp, buffer.Length + readLen);

                            buffer = temp;

                            len += readLen;
                        }
                        catch (Exception ex)
                        {
                            done = true;

                            response = new ResponseAccessor <string>
                            {
                                Ex = ex
                            };

                            innerResult.m_waitHandle.Set();

                            stream?.Close();
                        }
                    })), null);

                    innerResult.AsyncWaitHandle.WaitOne();
                }
                catch (Exception ex)
                {
                    response = new ResponseAccessor <string>
                    {
                        Ex = ex
                    };

                    done = true;
                }
            } while (!done);

            return(response);
        }
Пример #13
0
        internal ManualAsyncResult RequestGetAsync(Action <ResponseAccessor <string> > callback, ManualAsyncResult result)
        {
            try
            {
                result.HttpWebRequest.BeginGetResponse(result.WrapHandler(ar =>
                {
                    try
                    {
                        var responseStream = result.HttpWebRequest.EndGetResponse(ar);

                        using (var response = responseStream.GetResponseStream())
                        {
                            ReadStream(callback, result, response, 0);
                        }
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;

                        if (Con.AutoHost)
                        {
                            result.AsyncRequestRetryHandler.RetryAsyncRequest();
                        }
                    }
                }), null);
            }
            catch (Exception ex)
            {
                result.Error = ex;

                if (Con.AutoHost)
                {
                    result.AsyncRequestRetryHandler.RetryAsyncRequest();
                }
            }
            return(result);
        }
Пример #14
0
        internal ManualAsyncResult PreparePostRequestAsync <TReturnType>(Action <ResponseAccessor <TReturnType> > callback, ManualAsyncResult asyncResult)
        {
            asyncResult = SetUpHttpWebRequest(asyncResult, "Post", 5000);

            asyncResult.AsyncRequestRetryHandler = new AsyncRequestRetryHandler(this, asyncResult);

            try
            {
                asyncResult.AsyncRequestRetryHandler.SetCallback(body =>
                {
                    if (body.Ex != null)
                    {
                        callback(new ResponseAccessor <TReturnType>
                        {
                            Ex = body.Ex
                        });
                    }
                    else
                    {
                        callback(new ResponseAccessor <TReturnType>
                        {
                            Content = JsonConvert.DeserializeObject <TReturnType>(body.Content),
                        });
                    }
                });

                RequestPostAsync(asyncResult.AsyncRequestRetryHandler.Callback, asyncResult);
            }
            catch (Exception e)
            {
                callback(new ResponseAccessor <TReturnType>
                {
                    Ex = e
                });
            }

            return(asyncResult);
        }