using System.Net;\r
using System.Threading;\r
using System.Net.Sockets;\r
+using Newtonsoft.Json;\r
using Newtonsoft.Json.Linq;\r
\r
using System.Text;\r
\r
namespace Stratum\r
{\r
- delegate void NotificationCallback(out object[] notificationData);\r
-\r
public class Stratum\r
{\r
private Socket client;\r
private StratumResponse<T> Invoke<T>(StratumRequest stratumReq)\r
{\r
// Serialize stratumReq into JSON string\r
- var reqJSON = Newtonsoft.Json.JsonConvert.SerializeObject(stratumReq) + '\n';\r
- var reqId = (string) stratumReq.Id;\r
-\r
- string strResponse = "";\r
- StratumResponse<T> responseObj = null;\r
+ var reqJSON = JsonConvert.SerializeObject(stratumReq) + '\n';\r
\r
// Send JSON data to the remote device.\r
Send(client, reqJSON);\r
// Wait for response\r
gotResponse.WaitOne();\r
\r
+ var strResponse = string.Empty;\r
lock (responsesLock)\r
{\r
// Deserialize the response\r
- strResponse = responses[reqId];\r
- responses.Remove(reqId);\r
+ strResponse = responses[stratumReq.Id];\r
+ responses.Remove(stratumReq.Id);\r
}\r
- responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<StratumResponse<T>>(strResponse);\r
+\r
+ // Deserialize response into new instance of StratumResponse<T> \r
+ StratumResponse<T> responseObj = JsonConvert.DeserializeObject<StratumResponse<T>>(strResponse);\r
\r
// Reset the state\r
gotResponse.Reset();\r
{\r
try\r
{\r
- JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(strResponse) as JObject;\r
- throw new Exception(jo["Error"].ToString());\r
+ JObject jResponseObj = JsonConvert.DeserializeObject(strResponse) as JObject;\r
+ throw new Exception(jResponseObj["Error"].ToString());\r
}\r
- catch (Newtonsoft.Json.JsonSerializationException)\r
+ catch (JsonSerializationException)\r
{\r
throw new Exception("Inconsistent or empty response");\r
}\r
\r
if (arStatus.buffer[bytesRead - 1] == '\n')\r
{\r
- string strMessage = arStatus.sb.ToString();\r
+ var strMessage = arStatus.sb.ToString();\r
arStatus.sb.Clear();\r
\r
try\r
{\r
- JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(strMessage) as JObject;\r
- string requestId = (string)jo["id"];\r
+ JObject jResponse = JsonConvert.DeserializeObject(strMessage) as JObject;\r
+ var reqId = (string)jResponse["id"];\r
\r
- if (!String.IsNullOrEmpty(requestId))\r
+ if (!String.IsNullOrEmpty(reqId))\r
{\r
lock (responsesLock)\r
{\r
- responses.Add(requestId, strMessage);\r
+ responses.Add(reqId, strMessage);\r
}\r
\r
gotResponse.Set();\r
}\r
else\r
{\r
- // TODO: notifications handling\r
- Console.WriteLine("Notification: {0}", strMessage);\r
+ StratumNotification jNotification = JsonConvert.DeserializeObject<StratumNotification>(strMessage);\r
+\r
+ var NotifyProcessThread = new Thread(() => NotificationHandler(jNotification.Method, jNotification.Params));\r
+ NotifyProcessThread.Start();\r
}\r
}\r
- catch (Newtonsoft.Json.JsonSerializationException e)\r
+ catch (JsonSerializationException e)\r
{\r
// TODO: handle parse error\r
}\r
\r
client.BeginReceive(state.buffer, 0, StratumReadState.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);\r
}\r
+\r
+ /// <summary>\r
+ /// Notifications stub which is run in a separate thread. If you wish to implement real notification processing then just override this method in the derived class.\r
+ /// </summary>\r
+ /// <param name="NotificationMethod">Method name</param>\r
+ /// <param name="NotificationData">Array of values</param>\r
+ private static void NotificationHandler(string NotificationMethod, JArray NotificationData)\r
+ {\r
+ Console.WriteLine("\nNotification: Method={0}, data={1}", NotificationMethod, NotificationData.ToString());\r
+ }\r
}\r
\r
}
\ No newline at end of file