Diverse Updates / Work in Progress

master
Harald Wolff 2017-10-17 21:43:50 +02:00
parent c19de0df90
commit 8b9937c2f4
8 changed files with 97 additions and 11 deletions

View File

@ -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("")]

View File

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
namespace JSONRPC
namespace sharp.jsonrpc
{
public class RPCCall
{

View File

@ -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<String,object> args){
try
{
string request = JSONRPC.Request.Create(method,args);
string request = sharp.jsonrpc.Request.Create(method,args);
//Console.WriteLine("json-call: {0}",request);
Task<HttpResponseMessage> 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;
}
}

View File

@ -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";

View File

@ -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<string, JToken> result { get; set; }
public string error { get; set; }
public Response(string source){
JsonConvert.PopulateObject(source,this);
}

View File

@ -0,0 +1,5 @@
using System;
namespace sharp.jsonrpc.events
{
}

73
net/JSONClient.cs 100644
View File

@ -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);
}
}
}

View File

@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DCE6066E-9709-4D12-8994-F7879C3557D6}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>JSONRPC</RootNamespace>
<RootNamespace>sharp.jsonrpc</RootNamespace>
<AssemblyName>JSONRPC</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
@ -39,10 +39,16 @@
<Compile Include="Response.cs" />
<Compile Include="RPCConnector.cs" />
<Compile Include="RPCCall.cs" />
<Compile Include="net\JSONClient.cs" />
<Compile Include="events\SimpleEvent.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="net\" />
<Folder Include="events\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>