Proper locking and another example
[StratumLibrary.git] / StratumLibrary / Stratum.cs
index 722abda..30ca0bc 100644 (file)
@@ -9,9 +9,13 @@ using System.Collections.Generic;
 \r
 namespace Stratum\r
 {\r
+    delegate void NotificationCallback(out object[] notificationData);\r
+\r
     public class Stratum\r
     {\r
         private Socket client;\r
+\r
+        object responsesLock = new object();\r
         private Dictionary<string, string> responses = new Dictionary<string, string>();\r
         ManualResetEvent gotResponse = new ManualResetEvent(false);\r
 \r
@@ -94,6 +98,22 @@ namespace Stratum
         /// </summary>\r
         /// <typeparam name="T">Return type</typeparam>\r
         /// <param name="method">Method name</param>\r
+        /// <returns>StratumResponse object</returns>\r
+        public StratumResponse<T> Invoke<T>(string method)\r
+        {\r
+            var req = new StratumRequest()\r
+            {\r
+                Method = method,\r
+                Params = new object[] { }\r
+            };\r
+            return Invoke<T>(req);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Invoke remote method\r
+        /// </summary>\r
+        /// <typeparam name="T">Return type</typeparam>\r
+        /// <param name="method">Method name</param>\r
         /// <param name="arg">Argument</param>\r
         /// <returns>StratumResponse object</returns>\r
         public StratumResponse<T> Invoke<T>(string method, object arg)\r
@@ -129,16 +149,22 @@ namespace Stratum
             var reqJSON = Newtonsoft.Json.JsonConvert.SerializeObject(stratumReq) + '\n';\r
             var reqId = (string) stratumReq.Id;\r
 \r
+            string strResponse = "";\r
+            StratumResponse<T> responseObj = null;\r
+\r
             // Send JSON data to the remote device.\r
             Send(client, reqJSON);\r
 \r
             // Wait for response\r
             gotResponse.WaitOne();\r
 \r
-            // Deserialize the response\r
-            string strResponse = responses[reqId];\r
-            StratumResponse<T> responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<StratumResponse<T>>(strResponse);\r
-            responses.Remove(reqId);\r
+            lock (responsesLock)\r
+            {\r
+                // Deserialize the response\r
+                strResponse = responses[reqId];\r
+                responses.Remove(reqId);\r
+            }\r
+            responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<StratumResponse<T>>(strResponse);\r
 \r
             // Reset the state\r
             gotResponse.Reset();\r
@@ -195,7 +221,10 @@ namespace Stratum
 \r
                             if (!String.IsNullOrEmpty(requestId))\r
                             {\r
-                                responses.Add(requestId, strMessage);\r
+                                lock (responsesLock)\r
+                                {\r
+                                    responses.Add(requestId, strMessage);\r
+                                }\r
 \r
                                 gotResponse.Set();\r
                             }\r