private async Task <T> BuildAndSendRequest <T>(string function, JArray additionalParams, T defaultValue, CancellationToken ctoken) { try { if (IsConnected == false) { ErrorCallbackProvider.ReportError("Whisper connection error!"); throw new WebException("Whisper connection error!"); } JObject jobj = new JObject(); jobj.Add("jsonrpc", JsonVersion); jobj.Add("method", function); if (additionalParams != null) { jobj.Add("params", additionalParams); } jobj.Add("id", ++JsonId); await WebSocketProvider.Send(Encoding.UTF8.GetBytes(jobj.ToString()), ctoken); return(await ReceiveResponse <T>(JsonId, defaultValue, ctoken)); } catch (WebSocketException e) { throw new HoardException("Whisper unknown exception:\n" + e.Message, e); } catch (Exception e) { throw new HoardException("Unknown exception:\n" + e.Message, e); } }
private async Task <SubscriptionResponse> ReceiveSubscriptionResponse(CancellationToken token) { if (IsConnected == false) { ErrorCallbackProvider.ReportError("Whisper connection error!"); throw new WebException("Whisper connection error!"); } while (true) { byte[] msgBytes = await WebSocketProvider.Receive(token); //we are skipping all messages that are not subscriptions if (msgBytes != null) { string jsonMsg = Encoding.UTF8.GetString(msgBytes); ErrorCallbackProvider.ReportInfo(jsonMsg); JToken message = null; JObject json = JObject.Parse(jsonMsg); json.TryGetValue("error", out message); if (message != null) { Error = message.ToString(); ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error); } else { json.TryGetValue("method", out JToken method); if ((method != null) && (method.ToString() == "shh_subscription")) { JToken prms = null; json.TryGetValue("params", out prms); if (prms != null) { JObject jsonParams = prms.ToObject <JObject>(); jsonParams.TryGetValue("result", out message); if (message != null) { return(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString())); } } } else { ErrorCallbackProvider.ReportWarning("Expected notification but got: " + jsonMsg); } } } } }
private async Task <T> ReceiveResponse <T>(int reqId, T defaultValue, CancellationToken ctoken) { if (IsConnected == false) { ErrorCallbackProvider.ReportError("Whisper connection error!"); throw new WebException("Whisper connection error!"); } //wait for requested response (or timeout) while (true) { byte[] msgBytes = await WebSocketProvider.Receive(ctoken); if (msgBytes != null) { string jsonMsg = Encoding.UTF8.GetString(msgBytes); ErrorCallbackProvider.ReportInfo(jsonMsg); JObject json = JObject.Parse(jsonMsg); json.TryGetValue("error", out JToken error); if (error != null) { Error = error.ToString(); ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error); return(defaultValue); } else { json.TryGetValue("result", out JToken result); if (result != null) { json.TryGetValue("id", out JToken respId); if (respId.Value <int>() == reqId) { if (result.Type == JTokenType.Array || result.Type == JTokenType.Object) { return(JsonConvert.DeserializeObject <T>(result.ToString())); } return(result.Value <T>()); } else { ErrorCallbackProvider.ReportError("Waiting for response with ID: " + reqId + " but got " + respId.Value <int>()); } } else { json.TryGetValue("method", out JToken method); if ((method != null) && (method.ToString() == "shh_subscription")) { json.TryGetValue("params", out JToken prms); if (prms != null) { JObject jsonParams = prms.ToObject <JObject>(); jsonParams.TryGetValue("result", out JToken message); if (message != null) { OnSubscriptionMessage?.Invoke(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString())); } } } } } } else { ErrorCallbackProvider.ReportWarning("Conection was closed!"); return(defaultValue); } } }