コード例 #1
0
ファイル: RPCClient.cs プロジェクト: mandelmonkey/NBitcoin
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(Address);

            webRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Encoders.Base64.EncodeData(Encoders.ASCII.DecodeData(_Authentication));
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";

            var writer = new StringWriter();

            request.WriteJSON(writer);
            writer.Flush();
            var json  = writer.ToString();
            var bytes = Encoding.UTF8.GetBytes(json);

#if !(PORTABLE || NETCORE)
            webRequest.ContentLength = bytes.Length;
#endif
            var dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

            await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            await dataStream.FlushAsync().ConfigureAwait(false);

            dataStream.Dispose();
            RPCResponse response;
            try
            {
                using (var webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false))
                {
                    response = RPCResponse.Load(webResponse.GetResponseStream());
                }
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null || ex.Response.ContentLength == 0)
                {
                    throw;
                }
                response = RPCResponse.Load(ex.Response.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            return(response);
        }
コード例 #2
0
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Address);

            webRequest.Credentials = Credentials;
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";

            var writer = new StringWriter();

            request.WriteJSON(writer);
            writer.Flush();
            var json  = writer.ToString();
            var bytes = Encoding.UTF8.GetBytes(json);

#if !PORTABLE
            webRequest.ContentLength = bytes.Length;
#endif
            Stream dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Dispose();
            RPCResponse response = null;
            try
            {
                WebResponse webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false);

                response = RPCResponse.Load(webResponse.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    throw;
                }
                response = RPCResponse.Load(ex.Response.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            return(response);
        }
コード例 #3
0
        async Task <RPCResponse> SendCommandAsyncCore(RPCRequest request, bool throwIfRPCError)
        {
            RPCResponse response = null;
            var         batches  = _BatchedRequests;

            if (batches != null)
            {
                TaskCompletionSource <RPCResponse> source = new TaskCompletionSource <RPCResponse>();
                batches.Enqueue(Tuple.Create(request, source));
                response = await source.Task.ConfigureAwait(false);
            }
            HttpWebRequest webRequest = response == null?CreateWebRequest() : null;

            if (response == null)
            {
                var writer = new StringWriter();
                request.WriteJSON(writer);
                writer.Flush();
                var json  = writer.ToString();
                var bytes = Encoding.UTF8.GetBytes(json);
#if !(PORTABLE || NETCORE)
                webRequest.ContentLength = bytes.Length;
#endif
                var dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

                await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

                await dataStream.FlushAsync().ConfigureAwait(false);

                dataStream.Dispose();
            }
            WebResponse webResponse   = null;
            WebResponse errorResponse = null;
            try
            {
                webResponse = response == null ? await webRequest.GetResponseAsync().ConfigureAwait(false) : null;

                response = response ?? RPCResponse.Load(await ToMemoryStreamAsync(webResponse.GetResponseStream()).ConfigureAwait(false));

                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null || ex.Response.ContentLength == 0)
                {
                    throw;
                }
                errorResponse = ex.Response;
                response      = RPCResponse.Load(await ToMemoryStreamAsync(errorResponse.GetResponseStream()).ConfigureAwait(false));
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            finally
            {
                if (errorResponse != null)
                {
                    errorResponse.Dispose();
                    errorResponse = null;
                }
                if (webResponse != null)
                {
                    webResponse.Dispose();
                    webResponse = null;
                }
            }
            return(response);
        }
コード例 #4
0
        /// <summary>
        ///	Send all commands in one batch
        /// </summary>
        public async Task SendBatchAsync()
        {
            Tuple <RPCRequest, TaskCompletionSource <RPCResponse> >         req;
            List <Tuple <RPCRequest, TaskCompletionSource <RPCResponse> > > requests = new List <Tuple <RPCRequest, TaskCompletionSource <RPCResponse> > >();
            var batches = _BatchedRequests;

            if (batches == null)
            {
                throw new InvalidOperationException("This RPCClient instance is not a batch, use PrepareBatch");
            }
            _BatchedRequests = null;
            while (batches.TryDequeue(out req))
            {
                requests.Add(req);
            }
            if (requests.Count == 0)
            {
                return;
            }

            var writer = new StringWriter();

            writer.Write("[");
            bool first = true;

            foreach (var item in requests)
            {
                if (!first)
                {
                    writer.Write(",");
                }
                first = false;
                item.Item1.WriteJSON(writer);
            }
            writer.Write("]");
            writer.Flush();

            var json  = writer.ToString();
            var bytes = Encoding.UTF8.GetBytes(json);

            var webRequest = CreateWebRequest();

#if !(PORTABLE || NETCORE)
            webRequest.ContentLength = bytes.Length;
#endif

            int responseIndex = 0;
            var dataStream    = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

            await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            await dataStream.FlushAsync().ConfigureAwait(false);

            dataStream.Dispose();
            JArray      response;
            WebResponse webResponse   = null;
            WebResponse errorResponse = null;
            try
            {
                webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false);

                response = JArray.Load(new JsonTextReader(
                                           new StreamReader(
                                               await ToMemoryStreamAsync(webResponse.GetResponseStream()).ConfigureAwait(false), Encoding.UTF8)));
                foreach (var jobj in response.OfType <JObject>())
                {
                    try
                    {
                        RPCResponse rpcResponse = new RPCResponse(jobj);
                        requests[responseIndex].Item2.TrySetResult(rpcResponse);
                    }
                    catch (Exception ex)
                    {
                        requests[responseIndex].Item2.TrySetException(ex);
                    }
                    responseIndex++;
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null || ex.Response.ContentLength == 0)
                {
                    foreach (var item in requests)
                    {
                        item.Item2.TrySetException(ex);
                    }
                }
                else
                {
                    errorResponse = ex.Response;
                    try
                    {
                        RPCResponse rpcResponse = RPCResponse.Load(await ToMemoryStreamAsync(errorResponse.GetResponseStream()).ConfigureAwait(false));
                        foreach (var item in requests)
                        {
                            item.Item2.TrySetResult(rpcResponse);
                        }
                    }
                    catch (Exception)
                    {
                        foreach (var item in requests)
                        {
                            item.Item2.TrySetException(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                foreach (var item in requests)
                {
                    item.Item2.TrySetException(ex);
                }
            }
            finally
            {
                if (errorResponse != null)
                {
                    errorResponse.Dispose();
                    errorResponse = null;
                }
                if (webResponse != null)
                {
                    webResponse.Dispose();
                    webResponse = null;
                }
            }
        }