\feff\r
Microsoft Visual Studio Solution File, Format Version 12.00\r
-# Visual Studio 2012\r
+# Visual Studio Express 2012 for Windows Desktop\r
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stratum", "StratumLibrary\Stratum.csproj", "{27F0C6C3-DCD6-4588-AFBB-DCE22306F5F8}"\r
EndProject\r
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StratumTest", "StratumTest\StratumTest.csproj", "{FB61065F-93BA-497B-A2D5-3574D23B0B62}"\r
{FB61065F-93BA-497B-A2D5-3574D23B0B62}.Release|Any CPU.ActiveCfg = Release|Any CPU\r
{FB61065F-93BA-497B-A2D5-3574D23B0B62}.Release|Any CPU.Build.0 = Release|Any CPU\r
EndGlobalSection\r
- GlobalSection(MonoDevelopProperties) = preSolution\r
- StartupItem = StratumTest\StratumTest.csproj\r
- EndGlobalSection\r
GlobalSection(SolutionProperties) = preSolution\r
HideSolutionNode = FALSE\r
EndGlobalSection\r
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
--- /dev/null
+\feffusing Newtonsoft.Json;\r
+using Newtonsoft.Json.Linq;\r
+\r
+\r
+namespace Stratum\r
+{\r
+ /// <summary>\r
+ /// Represents a Stratum notification\r
+ /// </summary>\r
+ [JsonObject(MemberSerialization.OptIn)]\r
+ public class StratumNotification\r
+ {\r
+ /// <summary>\r
+ /// Response id, should be null\r
+ /// </summary>\r
+ [JsonProperty(PropertyName = "id")]\r
+ public object Id { get; set; }\r
+\r
+ /// <summary>\r
+ /// Method name\r
+ /// </summary>\r
+ [JsonProperty(PropertyName = "method")]\r
+ public string Method { get; set; }\r
+\r
+ /// <summary>\r
+ /// Notification data\r
+ /// </summary>\r
+ [JsonProperty(PropertyName = "params")]\r
+ public JArray Params { get; set; }\r
+ }\r
+}\r