From: CryptoManiac Date: Tue, 4 Aug 2015 23:11:54 +0000 (+0300) Subject: Proper locking and another example X-Git-Url: https://git.novaco.in/?p=StratumLibrary.git;a=commitdiff_plain;h=27ffb176358e7959320a5be4d916a862a55cff37 Proper locking and another example --- diff --git a/StratumLibrary/Stratum.cs b/StratumLibrary/Stratum.cs index 722abda..30ca0bc 100644 --- a/StratumLibrary/Stratum.cs +++ b/StratumLibrary/Stratum.cs @@ -9,9 +9,13 @@ using System.Collections.Generic; namespace Stratum { + delegate void NotificationCallback(out object[] notificationData); + public class Stratum { private Socket client; + + object responsesLock = new object(); private Dictionary responses = new Dictionary(); ManualResetEvent gotResponse = new ManualResetEvent(false); @@ -94,6 +98,22 @@ namespace Stratum /// /// Return type /// Method name + /// StratumResponse object + public StratumResponse Invoke(string method) + { + var req = new StratumRequest() + { + Method = method, + Params = new object[] { } + }; + return Invoke(req); + } + + /// + /// Invoke remote method + /// + /// Return type + /// Method name /// Argument /// StratumResponse object public StratumResponse Invoke(string method, object arg) @@ -129,16 +149,22 @@ namespace Stratum var reqJSON = Newtonsoft.Json.JsonConvert.SerializeObject(stratumReq) + '\n'; var reqId = (string) stratumReq.Id; + string strResponse = ""; + StratumResponse responseObj = null; + // Send JSON data to the remote device. Send(client, reqJSON); // Wait for response gotResponse.WaitOne(); - // Deserialize the response - string strResponse = responses[reqId]; - StratumResponse responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject>(strResponse); - responses.Remove(reqId); + lock (responsesLock) + { + // Deserialize the response + strResponse = responses[reqId]; + responses.Remove(reqId); + } + responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject>(strResponse); // Reset the state gotResponse.Reset(); @@ -195,7 +221,10 @@ namespace Stratum if (!String.IsNullOrEmpty(requestId)) { - responses.Add(requestId, strMessage); + lock (responsesLock) + { + responses.Add(requestId, strMessage); + } gotResponse.Set(); } diff --git a/StratumLibrary/Stratum.csproj b/StratumLibrary/Stratum.csproj index 38deac1..dbaed4f 100644 --- a/StratumLibrary/Stratum.csproj +++ b/StratumLibrary/Stratum.csproj @@ -32,7 +32,6 @@ ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - monodevelop @@ -44,6 +43,7 @@ + diff --git a/StratumLibrary/StratumReadState.cs b/StratumLibrary/StratumReadState.cs index 78898ae..5d0ffa9 100644 --- a/StratumLibrary/StratumReadState.cs +++ b/StratumLibrary/StratumReadState.cs @@ -21,7 +21,7 @@ namespace Stratum /// /// Receive buffer size /// - public const int BufferSize = 8; + public const int BufferSize = 256; /// /// Receive buffer /// diff --git a/StratumTest/Program.cs b/StratumTest/Program.cs index 4131afc..ea64cbd 100644 --- a/StratumTest/Program.cs +++ b/StratumTest/Program.cs @@ -13,13 +13,13 @@ namespace StratumTest while (true) { - var res = s.Invoke("blockchain.headers.subscribe", new object[] {}); - + var res = s.Invoke("blockchain.numblocks.subscribe"); + + // var res = s.Invoke("blockchain.headers.subscribe"); // var res = s.Invoke("blockchain.transaction.get", "101379cb55ac431c435db40b4325f858568b0de3d8bd652a23a19e5d62521a72"); - - // var res = s.Invoke("blockchain.address.get_balance", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); - // var res = s.Invoke("blockchain.address.get_history", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); - // var res = s.Invoke("blockchain.address.listunspent", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); + // var res = s.Invoke("blockchain.address.get_balance", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); + // var res = s.Invoke("blockchain.address.get_history", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); + // var res = s.Invoke("blockchain.address.listunspent", "4PQtUNZ2aBYpZpVMPV2Qgz1PitCqgoT388"); Console.Write(res.Result.ToString()); Console.ReadLine(); diff --git a/StratumTest/StratumTest.csproj b/StratumTest/StratumTest.csproj index 607095f..d8fabb0 100644 --- a/StratumTest/StratumTest.csproj +++ b/StratumTest/StratumTest.csproj @@ -32,13 +32,12 @@ 4 - - False - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - + + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll +