\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
/// </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
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
\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
<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
<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
/// <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
\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
<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