Proper locking and another example
authorCryptoManiac <balthazar@yandex.ru>
Tue, 4 Aug 2015 23:11:54 +0000 (02:11 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Tue, 4 Aug 2015 23:11:54 +0000 (02:11 +0300)
StratumLibrary/Stratum.cs
StratumLibrary/Stratum.csproj
StratumLibrary/StratumReadState.cs
StratumTest/Program.cs
StratumTest/StratumTest.csproj

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
index 38deac1..dbaed4f 100644 (file)
@@ -32,7 +32,6 @@
   <ItemGroup>\r
     <Reference Include="Newtonsoft.Json">\r
       <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>\r
-      <Package>monodevelop</Package>\r
     </Reference>\r
     <Reference Include="System" />\r
     <Reference Include="System.Core" />\r
@@ -44,6 +43,7 @@
     <Compile Include="Stratum.cs" />\r
     <Compile Include="Properties\AssemblyInfo.cs" />\r
     <Compile Include="StratumException.cs" />\r
+    <Compile Include="StratumNotification.cs" />\r
     <Compile Include="StratumRequest.cs" />\r
     <Compile Include="StratumResponse.cs" />\r
     <Compile Include="StratumReadState.cs" />\r
index 78898ae..5d0ffa9 100644 (file)
@@ -21,7 +21,7 @@ namespace Stratum
         /// <summary>\r
         /// Receive buffer size\r
         /// </summary>\r
-        public const int BufferSize = 8;\r
+        public const int BufferSize = 256;\r
         /// <summary>\r
         /// Receive buffer\r
         /// </summary>\r
index 4131afc..ea64cbd 100644 (file)
@@ -13,13 +13,13 @@ namespace StratumTest
 \r
             while (true)\r
             {\r
-                               var res = s.Invoke<Newtonsoft.Json.Linq.JObject>("blockchain.headers.subscribe", new object[] {});\r
-\r
+                var res = s.Invoke<int>("blockchain.numblocks.subscribe");\r
+                \r
+                // var res = s.Invoke<Newtonsoft.Json.Linq.JObject>("blockchain.headers.subscribe");\r
                 // var res = s.Invoke<string>("blockchain.transaction.get", "101379cb55ac431c435db40b4325f858568b0de3d8bd652a23a19e5d62521a72");\r
-\r
-                //                var res = s.Invoke<Newtonsoft.Json.Linq.JObject>("blockchain.address.get_balance", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
-                //                var res = s.Invoke<Newtonsoft.Json.Linq.JArray>("blockchain.address.get_history", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
-                //                var res = s.Invoke<Newtonsoft.Json.Linq.JArray>("blockchain.address.listunspent", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
+                // var res = s.Invoke<Newtonsoft.Json.Linq.JObject>("blockchain.address.get_balance", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
+                // var res = s.Invoke<Newtonsoft.Json.Linq.JArray>("blockchain.address.get_history", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
+                // var res = s.Invoke<Newtonsoft.Json.Linq.JArray>("blockchain.address.listunspent", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388");\r
 \r
                 Console.Write(res.Result.ToString());\r
                 Console.ReadLine();\r
index 607095f..d8fabb0 100644 (file)
     <WarningLevel>4</WarningLevel>\r
   </PropertyGroup>\r
   <ItemGroup>\r
-    <Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">\r
-      <SpecificVersion>False</SpecificVersion>\r
-      <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>\r
-    </Reference>\r
     <Reference Include="System" />\r
     <Reference Include="System.Core" />\r
     <Reference Include="Microsoft.CSharp" />\r
+    <Reference Include="Newtonsoft.Json">\r
+      <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>\r
+    </Reference>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <Compile Include="Program.cs" />\r