forked from Astn/JSON-RPC.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cs
More file actions
148 lines (131 loc) · 5.26 KB
/
client.cs
File metadata and controls
148 lines (131 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
using AustinHarris.JsonRpc;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Diagnostics;
namespace AustinHarris.JsonRpc
{
public class JsonRpcClient
{
private static object idLock = new object();
private static int id = 0;
public Uri ServiceEndpoint = null;
public JsonRpcClient(Uri serviceEndpoint)
{
ServiceEndpoint = serviceEndpoint;
}
private static Stream CopyAndClose(Stream inputStream)
{
const int readSize = 256;
byte[] buffer = new byte[readSize];
MemoryStream ms = new MemoryStream();
int count = inputStream.Read(buffer, 0, readSize);
while (count > 0)
{
ms.Write(buffer, 0, count);
count = inputStream.Read(buffer, 0, readSize);
}
ms.Position = 0;
inputStream.Close();
return ms;
}
public IObservable<JsonResponse<T>> Invoke<T>(string method, object arg, IScheduler scheduler)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = new object[] { arg }
};
return Invoke<T>(req, scheduler);
}
public IObservable<JsonResponse<T>> Invoke<T>(string method, object[] args, IScheduler scheduler)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = args
};
return Invoke<T>(req,scheduler);
}
public IObservable<JsonResponse<T>> Invoke<T>(JsonRequest jsonRpc, IScheduler scheduler)
{
var res = Observable.Create<JsonResponse<T>>((obs) =>
scheduler.Schedule(()=>{
WebRequest req = null;
try
{
int myId;
lock (idLock)
{
myId = ++id;
}
jsonRpc.Id = myId.ToString();
req = HttpWebRequest.Create(new Uri(ServiceEndpoint, "?callid=" + myId.ToString()));
req.Method = "Post";
req.ContentType = "application/json-rpc";
}
catch (Exception ex)
{
obs.OnError(ex);
}
var ar = req.BeginGetRequestStream(new AsyncCallback((iar) =>
{
HttpWebRequest request = null;
try
{
request = (HttpWebRequest)iar.AsyncState;
var stream = new StreamWriter(req.EndGetRequestStream(iar));
var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonRpc);
stream.Write(json);
stream.Close();
}
catch (Exception ex)
{
obs.OnError(ex);
}
var rar = req.BeginGetResponse(new AsyncCallback((riar) =>
{
JsonResponse<T> rjson = null;
string sstream = "";
try
{
var request1 = (HttpWebRequest)riar.AsyncState;
var resp = (HttpWebResponse)request1.EndGetResponse(riar);
using (var rstream = new StreamReader(CopyAndClose(resp.GetResponseStream())))
{
sstream = rstream.ReadToEnd();
}
rjson = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonResponse<T>>(sstream);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debugger.Break();
}
if (rjson == null)
{
if (!string.IsNullOrEmpty(sstream))
{
JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(sstream) as JObject;
obs.OnError(new Exception(jo["Error"].ToString()));
}
else
{
obs.OnError(new Exception("Empty response"));
}
}
obs.OnNext(rjson);
obs.OnCompleted();
}), request);
}), req);
}));
return res;
}
}
}