예제 #1
0
        public Request(T_Req rpcData, Action <T_Resp> completeAction, Action <RequestError> failedAction, uint port = 13895)
        {
            error     = new RequestError();
            this.port = port;

            this.rpcData        = rpcData;
            this.completeAction = completeAction;
            this.failedAction   = failedAction;
        }
예제 #2
0
        public Request(T_Req rpcData, Action <T_Resp> completeAction, Action <RequestError> failedAction, string host = Config.DEFAULT_HOST, uint port = Config.DEFAULT_PORT, Log log = null)
        {
            this.error = new RequestError();
            this.log   = (log == null) ? Log.Presets.Normal : log;
            this.host  = host;
            this.port  = port;

            this.rpcData        = rpcData;
            this.completeAction = completeAction;
            this.failedAction   = failedAction;
        }
예제 #3
0
        public bool MakeJsonRpcRequest(RequestData request, Log log, ref RequestError error, out string jsonString)
        {
            try
            {
                string url = $"http://{host}:{port}/json_rpc";

                string reqData      = request.Encode();
                byte[] reqDataBytes = Encoding.ASCII.GetBytes(reqData);

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method      = "POST";
                req.ContentType = "application/json";

                using (Stream stream = req.GetRequestStream())
                    stream.Write(reqDataBytes, 0, reqDataBytes.Length);

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                using (Stream stream = resp.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                    jsonString = reader.ReadToEnd();
                }

                return(true);
            }
            catch (WebException)
            {
                jsonString    = null;
                error.Code    = int.MaxValue;
                error.Message = "Error making RPC connection";
                return(false);
            }
            catch (Exception ex)
            {
                if (log.LogNetworkErrors)
                {
                    AngryWasp.Logger.Log.Instance.Write(Log_Severity.Error, $"Could not complete JSON RPC call: {request.MethodName}");
                    string paramData = request.GetParamsJson();
                    if (log.LogRpcRequest && paramData != null)
                    {
                        AngryWasp.Logger.Log.Instance.Write(Log_Severity.None, $"{request.MethodName} params: {paramData}");
                    }

                    AngryWasp.Logger.Log.Instance.Write(Log_Severity.Error, $".NET Exception, {ex.Message}");
                }

                jsonString = null;
                return(false);
            }
        }