示例#1
0
        public async Task <RpcResponse <T> > SendRequest <T>(RpcRequest request)
        {
            var    webRequest          = (HttpWebRequest)WebRequest.Create(BitcoinDaemonSettings.ServerUrl);
            string authorizationHeader = GenerateBasicAuthHeader();

            webRequest.Headers.Add(HttpRequestHeader.Authorization, authorizationHeader);
            webRequest.Credentials = new NetworkCredential(BitcoinDaemonSettings.RpcUsername, BitcoinDaemonSettings.RpcPassword);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            var bytes = request.ToByteArray();

            webRequest.ContentLength = bytes.Length;
            using (var requestStream = webRequest.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            try
            {
                using (var webResponse = await webRequest.GetResponseAsync())
                {
                    return(MakeRpcResponse <T>(webResponse));
                }
            }
            catch (WebException webException)
            {
                var daemonResponse = webException.Response as HttpWebResponse;

                if (daemonResponse.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException("Can not authorize in bitcoin server. Please contact your system administrator"
                                                          , webException);
                }
                return(MakeRpcResponse <T>(webException.Response));
            }
        }
示例#2
0
    public void SendWorkThreadFunction()
    {
        while (true)
        {
            Net.Rpc("rpc_get_player", 1, reader =>
            {
                Debug.Log("================");
            });

            Debug.Log("Send Count = " + Net.Instance.sendQueue.Count);
            if (Net.Instance.sendQueue.Count > 0)
            {
                RpcRequest request = Net.Instance.sendQueue.Dequeue();

                byte[] byteArray = request.ToByteArray();

                writer.Write(IPAddress.HostToNetworkOrder(byteArray.Length));
                writer.Write(byteArray);

                writer.Flush();
            }
            Thread.Sleep(1000);
        }
    }