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 (132 loc) · 4.7 KB
/
Copy pathclient.cs
File metadata and controls
148 lines (132 loc) · 4.7 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;
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 subj = new Subject<JsonResponse<T>>();
int myId;
lock (idLock)
{
myId = ++id;
}
jsonRpc.Id = myId.ToString();
WebRequest req = null;
try
{
req = HttpWebRequest.CreateHttp(new Uri(ServiceEndpoint + "?callid=" + myId.ToString()));
req.Method = "Post";
req.ContentType = "application/json-rpc";
}
catch (Exception ex)
{
return Observable.Throw<JsonResponse<T>>(ex);
}
var ar = req.BeginGetRequestStream(new AsyncCallback((iar) =>
{
HttpWebRequest request = null;
try
{
request = (HttpWebRequest)iar.AsyncState;
var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonRpc);
var reqStream = req.EndGetRequestStream(iar);
using (var stream = new StreamWriter(reqStream))
{
stream.Write(json);
stream.Flush();
stream.Close();
}
}
catch (Exception ex)
{
subj.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);
var respStream = resp.GetResponseStream();
using (var rstream = new StreamReader(CopyAndClose(respStream)))
{
sstream = rstream.ReadToEnd();
}
rjson = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonResponse<T>>(sstream);
}
catch (Exception ex)
{
subj.OnError(ex);
return;
}
try
{
if (rjson == null)
{
JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(sstream) as JObject;
subj.OnError(new Exception(jo["Error"].ToString()));
return;
}
}
catch (Exception)
{ }
subj.OnNext(rjson);
subj.OnCompleted();
}), request);
}), req);
return subj;
}
}
}