Initial Commit

master
Harald Wolff 2017-09-18 11:30:23 +02:00
commit c19de0df90
8 changed files with 270 additions and 0 deletions

40
.gitignore vendored 100644
View File

@ -0,0 +1,40 @@
# Autosave files
*~
# build
[Oo]bj/
[Bb]in/
packages/
TestResults/
# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
# Mac bundle stuff
*.dmg
*.app
# resharper
*_Resharper.*
*.Resharper
# dotCover
*.dotCover

56
JSONRPC.csproj 100644
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DCE6066E-9709-4D12-8994-F7879C3557D6}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>JSONRPC</RootNamespace>
<AssemblyName>JSONRPC</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Request.cs" />
<Compile Include="Response.cs" />
<Compile Include="RPCConnector.cs" />
<Compile Include="RPCCall.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="FileFormatDefault" />
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>

View File

@ -0,0 +1,26 @@
using System.Reflection;
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: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

33
RPCCall.cs 100644
View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
namespace JSONRPC
{
public class RPCCall
{
public RPCConnector Connector { get; private set; }
public String MethodName { get; private set; }
public Dictionary<string, object> args;
public RPCCall(RPCConnector connector)
{
this.Connector = connector;
this.args = new Dictionary<string, object>();
}
public RPCCall method(string method){
this.MethodName = method;
return this;
}
public RPCCall parameter(string name,object value){
this.args.Add(name,value);
return this;
}
public Response execute(){
return Connector.Call(this.MethodName,this.args);
}
}
}

52
RPCConnector.cs 100644
View File

@ -0,0 +1,52 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace JSONRPC
{
public class RPCConnector
{
private readonly HttpClient http;
public RPCConnector(string hostname,int port)
{
this.http = new HttpClient();
this.Hostname = hostname;
this.Port = port;
}
public String Hostname { get; private set; }
public int Port { get; private set; }
public string URL { get { return String.Format("http://{0}:{1}/json_rpc",Hostname,Port); } }
public RPCCall Call(){
return new RPCCall(this);
}
public Response Call(string method)
{
return Call(method, null);
}
public Response Call(string method,Dictionary<String,object> args){
try
{
string request = JSONRPC.Request.Create(method,args);
Task<HttpResponseMessage> response = http.PostAsync(URL, new StringContent(request));
response.Wait();
Task<string> result = response.Result.Content.ReadAsStringAsync();
result.Wait();
return new Response(result.Result);
}
catch (HttpRequestException e)
{
Response resp = new Response(String.Format("{ \"id\": \"0\", \"jsonrpc\": \"2.0\", \"result\": { status: \"Error\", message: \"HttpException: {0}\"}", e.ToString()));
return resp;
}
}
}
}

35
Request.cs 100644
View File

@ -0,0 +1,35 @@
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace JSONRPC
{
public class Request {
[JsonPropertyAttribute("jsonrpc")]
public string jsonrpc { get; set; } = "2.0";
[JsonPropertyAttribute("id")]
public String id { get; set; } = "0";
[JsonPropertyAttribute("method")]
public String method { get; set; }
[JsonPropertyAttribute("params")]
public Dictionary<string,object> args { get; set; }
private Request(string method,Dictionary<String,object> args){
this.id = Environment.TickCount.ToString();
this.method = method;
this.args = args == null ? new Dictionary<string, object>() : new Dictionary<String, object>(args);
}
public static string Create(string method)
{
return Create(method, null);
}
public static string Create(string method,Dictionary<String,object> args){
Request r = new Request(method,args);
return JsonConvert.SerializeObject(r);
}
}
}

24
Response.cs 100644
View File

@ -0,0 +1,24 @@
using System;
using Newtonsoft.Json.Linq;
using System.Threading;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace JSONRPC
{
public class Response {
public string jsonrpc { get; set; }
public string id { get; set; }
public Dictionary<string, JToken> result { get; set; }
public Response(string source){
JsonConvert.PopulateObject(source,this);
}
public bool Success(){
return result["status"].ToString().Equals("OK");
}
}
}

4
packages.config 100644
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
</packages>