예제 #1
0
        protected virtual ConnectionStatus DoSynchronousRequest(HttpWebRequest request, string data = null)
        {
            using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
            {
                ConnectionStatus cs = null;
                if (data != null)
                {
                    using (var r = request.GetRequestStream())
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(data);
                        r.Write(buffer, 0, buffer.Length);
                    }
                }
                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    using (var responseStream = response.GetResponseStream())
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        string result = streamReader.ReadToEnd();
                        cs = new ConnectionStatus(result)
                        {
                            Request = data,
                            RequestUrl = request.RequestUri.ToString(),
                            RequestMethod = request.Method
                        };
                        tracer.SetResult(cs);
                        return cs;
                    }
                }
                catch (WebException webException)
                {
                    cs = new ConnectionStatus(webException)
                    {
                        Request = data,
                        RequestUrl = request.RequestUri.ToString(),
                        RequestMethod = request.Method
                    };
                    tracer.SetResult(cs);

                    _ConnectionSettings.ConnectionStatusHandler(cs);

                    return cs;
                }
            }
        }
예제 #2
0
        private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
        {
            using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
            {
                var timeout = this._ConnectionSettings.Timeout;

                var state = new ConnectionState { Connection = request };

                if (data != null)
                {
                    var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
                    //ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                    yield return getRequestStream;

                    var requestStream = getRequestStream.Result;
                    try
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(data);
                        var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, buffer, 0, buffer.Length, state);
                        yield return writeToRequestStream;
                    }
                    finally
                    {
                        requestStream.Close();
                    }
                }

                // Get the response
                var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                //ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                yield return getResponse;

                // Get the response stream
                using (var response = (HttpWebResponse)getResponse.Result)
                using (var responseStream = response.GetResponseStream())
                {
                    // Copy all data from the response stream
                    var output = new MemoryStream();
                    var buffer = new byte[BUFFER_SIZE];
                    while (responseStream != null)
                    {
                        var read = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, buffer, 0, BUFFER_SIZE, null);
                        yield return read;
                        if (read.Result == 0) break;
                        output.Write(buffer, 0, read.Result);
                    }

                    // Decode the data and store the result
                    var result = Encoding.UTF8.GetString(output.ToArray());
                    var cs = new ConnectionStatus(result) { Request = data, RequestUrl = request.RequestUri.ToString(), RequestMethod = request.Method };
                    tcs.TrySetResult(cs);
                    tracer.SetResult(cs);
                    _ConnectionSettings.ConnectionStatusHandler(cs);
                }
                yield break;
            }
        }
예제 #3
0
        protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
        {
            var tcs = new TaskCompletionSource<ConnectionStatus>();
            if (this._ConnectionSettings.MaximumAsyncConnections <= 0)
                return this.CreateIterateTask(request, data, tcs);

            var timeout = this._ConnectionSettings.Timeout;
            if (!this._ResourceLock.WaitOne(timeout))
            {
                using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
                {
                    var m = "Could not start the operation before the timeout of " + timeout +
                        "ms completed while waiting for the semaphore";
                    var cs = new ConnectionStatus(new TimeoutException(m));
                    tcs.SetResult(cs);
                    tracer.SetResult(cs);
                    return tcs.Task;
                }
            }
            try
            {
                return Task.Factory.StartNew(
                    () => this.CreateIterateTask(request, data, tcs).Result
                    , TaskCreationOptions.LongRunning
                );
            }
            finally
            {
                this._ResourceLock.Release();
            }
        }