From 8b9937c2f498736e40aafdd5af4bde9dff92e810 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 17 Oct 2017 21:43:50 +0200 Subject: [PATCH] Diverse Updates / Work in Progress --- Properties/AssemblyInfo.cs | 2 +- RPCCall.cs | 2 +- RPCConnector.cs | 7 +-- Request.cs | 6 +-- Response.cs | 5 +- events/SimpleEvent.cs | 5 ++ net/JSONClient.cs | 73 ++++++++++++++++++++++++++ JSONRPC.csproj => sharp.jsonrpc.csproj | 8 ++- 8 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 events/SimpleEvent.cs create mode 100644 net/JSONClient.cs rename JSONRPC.csproj => sharp.jsonrpc.csproj (87%) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 274e18b..8af0117 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -4,7 +4,7 @@ using System.Runtime.CompilerServices; // Information about this assembly is defined by the following attributes. // Change them to the values specific to your project. -[assembly: AssemblyTitle("JSONRPC")] +[assembly: AssemblyTitle("sharp.jsonrpc")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] diff --git a/RPCCall.cs b/RPCCall.cs index 2d69077..1f147d8 100644 --- a/RPCCall.cs +++ b/RPCCall.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -namespace JSONRPC +namespace sharp.jsonrpc { public class RPCCall { diff --git a/RPCConnector.cs b/RPCConnector.cs index 0bf385a..5d80f6e 100644 --- a/RPCConnector.cs +++ b/RPCConnector.cs @@ -3,7 +3,7 @@ using System.Net.Http; using System.Threading.Tasks; using System.Collections.Generic; -namespace JSONRPC +namespace sharp.jsonrpc { public class RPCConnector { @@ -32,7 +32,8 @@ namespace JSONRPC public Response Call(string method,Dictionary args){ try { - string request = JSONRPC.Request.Create(method,args); + string request = sharp.jsonrpc.Request.Create(method,args); + //Console.WriteLine("json-call: {0}",request); Task response = http.PostAsync(URL, new StringContent(request)); response.Wait(); @@ -43,7 +44,7 @@ namespace JSONRPC } catch (HttpRequestException e) { - Response resp = new Response(String.Format("{ \"id\": \"0\", \"jsonrpc\": \"2.0\", \"result\": { status: \"Error\", message: \"HttpException: {0}\"}", e.ToString())); + Response resp = new Response(String.Format("{ \"id\": \"0\", \"sharp.jsonrpc\": \"2.0\", \"result\": { status: \"Error\", message: \"HttpException: {0}\"}", e.ToString())); return resp; } } diff --git a/Request.cs b/Request.cs index 1c5e1ea..c182910 100644 --- a/Request.cs +++ b/Request.cs @@ -1,11 +1,11 @@ -using System; +using System; using Newtonsoft.Json; using System.Collections.Generic; -namespace JSONRPC +namespace sharp.jsonrpc { public class Request { - [JsonPropertyAttribute("jsonrpc")] + [JsonPropertyAttribute("sharp.jsonrpc")] public string jsonrpc { get; set; } = "2.0"; [JsonPropertyAttribute("id")] public String id { get; set; } = "0"; diff --git a/Response.cs b/Response.cs index 3c228a6..b31961d 100644 --- a/Response.cs +++ b/Response.cs @@ -5,13 +5,14 @@ using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using System.Collections.Generic; -namespace JSONRPC +namespace sharp.jsonrpc { public class Response { public string jsonrpc { get; set; } public string id { get; set; } public Dictionary result { get; set; } - + public string error { get; set; } + public Response(string source){ JsonConvert.PopulateObject(source,this); } diff --git a/events/SimpleEvent.cs b/events/SimpleEvent.cs new file mode 100644 index 0000000..4b05ca5 --- /dev/null +++ b/events/SimpleEvent.cs @@ -0,0 +1,5 @@ +using System; +namespace sharp.jsonrpc.events +{ + +} diff --git a/net/JSONClient.cs b/net/JSONClient.cs new file mode 100644 index 0000000..57e839b --- /dev/null +++ b/net/JSONClient.cs @@ -0,0 +1,73 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.IO; +using Newtonsoft.Json.Linq; + +namespace sharp.jsonrpc.net +{ + public delegate void JSONConnectionClosed(JSONClient sender); + + public class JSONClient + { + public event JSONConnectionClosed OnConnectionClosed; + + Socket socket; + NetworkStream stream; + TextReader reader; + TextWriter writer; + + public JSONClient(Socket socket) + { + this.socket = socket; + this.stream = new NetworkStream(socket); + this.reader = new StreamReader(this.stream); + this.writer = new StreamWriter(this.stream); + } + + public void Close(){ + this.socket.Close(); + if (OnConnectionClosed != null){ + OnConnectionClosed(this); + } + } + + public bool Connected { get { return this.socket.Connected; } } + + public JToken readJSON(){ + + if (!Connected){ + throw new IOException("JSONClient is not connected"); + } + + string line = reader.ReadLine(); + while ((line != null) && line.Equals("")){ + line = reader.ReadLine(); + } + if (line == null){ + Close(); + throw new IOException("JSONCLient connection closed"); + } + Console.WriteLine("JSONClient: readJSON(): {0}",line); + return JToken.Parse(line); + } + + public void writeJSON(JToken json){ + string line = json.ToString().Replace("\n","").Replace("\r",""); + Console.WriteLine("JSONClient: writeJSON(): {0}",line); + this.writer.WriteLine(line); + this.writer.Flush(); + } + + public void writeJSONRPC(UInt64 id,JToken result,JToken error){ + JObject reply = new JObject( + new JProperty("id", id), + new JProperty("jsonrpc", "2.0"), + new JProperty("result", result), + new JProperty("error", error) + ); + writeJSON(reply); + } + + } +} diff --git a/JSONRPC.csproj b/sharp.jsonrpc.csproj similarity index 87% rename from JSONRPC.csproj rename to sharp.jsonrpc.csproj index a19df25..d01aeeb 100644 --- a/JSONRPC.csproj +++ b/sharp.jsonrpc.csproj @@ -5,7 +5,7 @@ AnyCPU {DCE6066E-9709-4D12-8994-F7879C3557D6} Library - JSONRPC + sharp.jsonrpc JSONRPC v4.5 @@ -39,10 +39,16 @@ + + + + + +