private static RspInfo GetResponse(string reqMethod, JObject json) { RspInfo rspInfo = new RspInfo() { state = false, msg = "Input data error!" }; switch (reqMethod) { case "getBalance": rspInfo = GetBalanceRsp(json); break; case "getBalances": rspInfo = GetBalancesRsp(json); break; case "addAddress": rspInfo = AddAddressRsp(json); break; case "getStatus": rspInfo = GetProcStatus(); break; default: break; } return(rspInfo); }
private static RspInfo GetProcStatus() { BlockHeight blockHeight = new BlockHeight(); blockHeight.ethBlockHeight = EthServer.GetEthBlockHeight(); blockHeight.ethParseHeight = EthServer.GetEthParseHeight(); blockHeight.zoroBlockHeight = ZoroServer.GetZoroBlockHeight(); blockHeight.zoroParseHeight = ZoroServer.GetZoroPraseHeight(); RspInfo rspInfo = new RspInfo() { state = true, msg = blockHeight }; return(rspInfo); }
public static void ListenerCallback(IAsyncResult result) { byte[] buffer = new byte[] { }; RspInfo rspInfo = new RspInfo() { state = false, msg = "Input data error!" }; HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext requestContext = listener.EndGetContext(result); listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); HttpListenerResponse response = requestContext.Response; try { //获取客户端传递的参数 StreamReader sr = new StreamReader(requestContext.Request.InputStream); var reqMethod = requestContext.Request.RawUrl.Replace("/", ""); var remoteIP = requestContext.Request.RemoteEndPoint.Address.ToString(); if (!Config.AllowIPs.Contains(remoteIP)) { Logger.Warn($"Remote IP Address error:{remoteIP}"); return; } var data = sr.ReadToEnd(); Logger.Info($"data: {data}"); var json = new JObject(); if (!string.IsNullOrEmpty(data)) { json = JObject.Parse(data); } Logger.Info($"Have a request:{reqMethod} post data: {json}"); rspInfo = GetResponse(reqMethod, json); var rsp = JsonConvert.SerializeObject(rspInfo); buffer = Encoding.UTF8.GetBytes(rsp); Logger.Info(rsp); } catch (Exception e) { var rsp = JsonConvert.SerializeObject(new RspInfo() { state = false, msg = e.Message + e.StackTrace }); buffer = Encoding.UTF8.GetBytes(rsp); Logger.Error(rsp); Logger.Error("stack: " + e.StackTrace); } finally { response.ContentLength64 = buffer.Length; response.StatusCode = 200; response.Headers.Add("Access-Control-Allow-Origin", "*"); response.ContentType = "application/json"; response.ContentEncoding = Encoding.UTF8; response.ContentLength64 = buffer.Length; var output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); } }