Esempio n. 1
0
        public void Delete(string path, Action<ConnectionStatus> callback)
        {
            Thread deleteThread = new Thread(() =>
            {
                this._ResourceLock.WaitOne();
                ConnectionState state = new ConnectionState
                {
                    Callback = (c) =>
                    {
                        this._ResourceLock.Release();
                        if (callback != null)
                            callback(c);

                    },
                    Connection = this.CreateConnection(path, "DELETE")
                };
                this.BeginGetResponse(state);
            });
            deleteThread.Start();
        }
Esempio n. 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;
            }
        }
Esempio n. 3
0
        private void PostDataUsingMethod(string method, string path,string data, Action<ConnectionStatus> callback)
        {
            if (method != "PUT" && method != "POST" && method != "DELETE")
            {
                throw new ArgumentException("Valid methods that can post a body are PUT, POST, DELETE", "method");
            }

            ConnectionState state = new ConnectionState()
            {
                Callback = callback,
                Connection = this.CreateConnection(path, method),
                PostData = data
            };

            state.Connection.BeginGetRequestStream(this.PostStreamOpened, state);
        }
Esempio n. 4
0
        private void BeginGetResponse(ConnectionState state)
        {
            try
            {
                IAsyncResult result = (IAsyncResult)state.Connection.BeginGetResponse(
                    new AsyncCallback(GetResponseHandle),
                    state
                );
                ThreadPool.RegisterWaitForSingleObject(
                    result.AsyncWaitHandle,
                    new WaitOrTimerCallback(ThreadTimeoutCallback),
                    state.Connection,
                    this._ConnectionSettings.TimeOut,
                    true
                );

            }
            catch (WebException e)
            {
                state.RaiseCallBack(e);
            }
            catch (Exception e)
            {
                state.RaiseCallBack(e);
            }
        }
Esempio n. 5
0
        private IEnumerable <Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource <ConnectionStatus> tcs, string data = null)
        {
            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);
                }
            yield break;
        }
Esempio n. 6
0
        private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
        {
            var timeout = this._ConnectionSettings.Timeout;

            if (!this._ResourceLock.WaitOne(timeout))
            {
                var m = "Could not start the operation before the timeout of " + timeout + "ms completed while waiting for the semaphore";
                tcs.SetResult(new ConnectionStatus(new TimeoutException(m)));
                yield break;
            }
            try
            {
                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 (true)
                    {
                        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 };
                    tcs.TrySetResult(cs);
                }
            }
            finally
            {
                this._ResourceLock.Release();
            }
        }