/// <summary> /// Invoke remote method /// </summary> /// <typeparam name="T">Return type</typeparam> /// <param name="method">Method name</param> /// <param name="args">Arguments array</param> /// <returns>StratumResponse object</returns> public StratumResponse <T> Invoke <T>(string method, object[] args) { var req = new StratumRequest() { Method = method, Params = args }; return(Invoke <T>(req)); }
private StratumResponse <T> Invoke <T>(StratumRequest stratumReq) { // Serialize stratumReq into JSON string var reqJSON = JsonConvert.SerializeObject(stratumReq) + '\n'; // Send JSON data to the remote device. Send(client, reqJSON); // Wait for response gotResponse.WaitOne(); var strResponse = string.Empty; lock (responsesLock) { // Deserialize the response strResponse = responses[stratumReq.Id]; responses.Remove(stratumReq.Id); } // Deserialize response into new instance of StratumResponse<T> StratumResponse <T> responseObj = JsonConvert.DeserializeObject <StratumResponse <T> >(strResponse); // Reset the state gotResponse.Reset(); if (responseObj == null) { try { JObject jResponseObj = JsonConvert.DeserializeObject(strResponse) as JObject; throw new Exception(jResponseObj["Error"].ToString()); } catch (JsonSerializationException) { throw new Exception("Inconsistent or empty response"); } } return(responseObj); }