Skip to content

Commit

Permalink
ok, this approach looks really, really good
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kornev committed Dec 2, 2016
1 parent 15b72d3 commit 744a058
Show file tree
Hide file tree
Showing 63 changed files with 2,921 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EB21BC49-725B-49F9-A1FA-908F826C8480}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Spooky.Json20.JsonNet.NetStandard13</RootNamespace>
<AssemblyName>Spooky.Json20.JsonNet.NetStandard13</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<None Include="project.json" />
<!-- A reference to the entire .NET Framework is automatically included -->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Spooky.Portable\Spooky.Portable.csproj">
<Project>{e2eb6b0f-a747-4afd-b99b-7cc1cbfffd2d}</Project>
<Name>Spooky.Portable</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="..\Spooky.Json20.JsonNet.SharedImplementation\Spooky.Json20.JsonNet.SharedImplementation.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
13 changes: 13 additions & 0 deletions Spooky.Json20.JsonNet.NetStandard13/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"supports": {},
"dependencies": {
"McStreamy": "1.0.0.1",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"System.IO.Pipes": "4.3.0"
},
"frameworks": {
"netstandard1.3": {}
}
}
39 changes: 39 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/AssemblyInfoCommon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyDescription("A JsonRPC 2.0 implementation of Spooky.")]
#if DEBUG
[assembly: AssemblyConfiguration("DEBUG")]
#else
[assembly: AssemblyConfiguration("RELEASE")]
#endif
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Spooky.Json20.JsonNet.Portable")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]

#if !LEGACYPORTABLE
[assembly: ComVisible(false)]
#endif
[assembly: CLSCompliant(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.3")]
[assembly: AssemblyFileVersion("1.0.0.3")]
37 changes: 37 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/JsonRpcErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// Contains a set of constants that represent official Json RPC error codes.
/// </summary>
public static class JsonRpcErrorCodes
{
/// <summary>
/// An official error code representing an error on the server parsing the request.
/// </summary>
public const int ParseError = -32700;

/// <summary>
/// An official error code representing an invalid request.
/// </summary>
public const int InvalidRequest = -32600;

/// <summary>
/// An official error code representing a call to a non-existent method.
/// </summary>
public const int MethodNotFound = -32601;

/// <summary>
/// An official error code representing an invalid request.
/// </summary>
public const int InvalidParameters = -32602;

/// <summary>
/// An official error code representing an internal error on the server.
/// </summary>
public const int InternalError = -32603;
}
}
46 changes: 46 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/JsonRpcHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// An <see cref="IRpcClient"/> implementation for making Json RPC 2.0 calls.
/// </summary>
public class JsonRpcHttpClient : RpcClient
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonRpcHttpClient"/> class.
/// </summary>
/// <param name="serviceAddress">The url of the Json RPC service this client accesses.</param>
public JsonRpcHttpClient(Uri serviceAddress)
: base
(
new RpcClientOptions()
{
Serializer = new JsonRpcSerializer(),
Transport = new JsonRpcHttpTransport(serviceAddress)
}
)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonRpcHttpClient"/> class.
/// </summary>
/// <param name="serviceAddress">The url of the Json RPC service this client accesses.</param>
/// <param name="httpClient">An <see cref="System.Net.Http.HttpClient"/> to use when making HTTP requests.</param>
public JsonRpcHttpClient(Uri serviceAddress, HttpClient httpClient)
: base
(
new RpcClientOptions()
{
Serializer = new JsonRpcSerializer(),
Transport = new JsonRpcHttpTransport(serviceAddress, httpClient)
}
)
{
}

}
}
65 changes: 65 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/JsonRpcHttpTransport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// An HTTP transport for Json RPC requests, using an <see cref="HttpClient"/> to make requests.
/// </summary>
/// <remarks>
/// <para>You can inject your own <see cref="HttpClient"/> instance via the constructors to control the HTTP pipeline and add features such as authorisation etc.</para>
/// <para>If no <see cref="HttpClient"/> is injected the system creates a new instance with GZIP and Deflate compression support enabled.</para>
/// </remarks>
public class JsonRpcHttpTransport : HttpClientTransport
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonRpcHttpTransport"/> class.
/// </summary>
/// <remarks>
/// <para>Creates an instance using a new <see cref="HttpClient"/> instance with compression enabled and <see cref="System.Text.UTF8Encoding"/> encoding.</para>
/// </remarks>
/// <param name="serviceAddress">The service address.</param>
public JsonRpcHttpTransport(Uri serviceAddress) :
this
(
serviceAddress,
CreateDefaultJsonRpcHttpClient()
)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonRpcHttpTransport"/> class.
/// </summary>
/// <remarks>
/// <para>Creates an instance using a new <see cref="HttpClient"/> instance with compression enabled and <see cref="System.Text.UTF8Encoding"/> encoding.</para>
/// </remarks>
/// <param name="serviceAddress">The service address.</param>
/// <param name="httpClient">An <see cref="System.Net.Http.HttpClient"/> to use when making HTTP requests.</param>
public JsonRpcHttpTransport(Uri serviceAddress, HttpClient httpClient) :
base
(
serviceAddress, JsonRpcMediaTypes.ApplicationJson,
System.Text.UTF8Encoding.UTF8.WebName.ToLower(),
httpClient
)
{
}

private static HttpClient CreateDefaultJsonRpcHttpClient()
{
var handler = new HttpClientHandler();
if (handler.SupportsAutomaticDecompression)
handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;

var client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(JsonRpcMediaTypes.ApplicationJson));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(JsonRpcMediaTypes.ApplicationJsonRequest));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(JsonRpcMediaTypes.ApplicationJson));

return client;
}
}
}
25 changes: 25 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/JsonRpcMediaTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// Provides constants for HTTP content media types for json RPC requests.
/// </summary>
public static class JsonRpcMediaTypes
{
/// <summary>
/// The preferred media type string for http content containing a json rpc request or response.
/// </summary>
public const string ApplicationJsonRpc = "application/json-rpc";
/// <summary>
/// A commonly used, but less than ideal media type for json rpc content. Use this only if required by your specific server(s).
/// </summary>
public const string ApplicationJson = "application/json";
/// <summary>
/// An allowed media type for json rpc content, preferred over <see cref="ApplicationJson"/>, but less ideal than the recommended <see cref="ApplicationJsonRpc"/>
/// </summary>
public const string ApplicationJsonRequest = "application/jsonrequest";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// An <see cref="IRpcClient"/> implementation for making Json RPC 2.0 calls.
/// </summary>
public class JsonRpcNamedPipesClient : RpcClient
{
/// <summary>
/// Initializes a new instance of the <see cref="Spooky.Json20.JsonRpcNamedPipesClient"/> class.
/// </summary>
/// <param name="namedPipesAddress">The named pipes of the JSON RPC service address this client accesses.</param>
public JsonRpcNamedPipesClient(string namedPipesAddress)
: base
(
new RpcClientOptions()
{
Serializer = new JsonRpcSerializer(),
Transport = new JsonRpcNamedPipesTransport(namedPipesAddress)
}
)
{
}
} // class
} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// A named pipes transport for Json RPC requests, using an <see cref="System.IO.Pipes"/> to make requests.
/// </summary>
public class JsonRpcNamedPipesTransport : NamedPipesTransport
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonRpcNamedPipesTransport"/> class.
/// </summary>
/// <param name="namedPipesAddress"></param>
public JsonRpcNamedPipesTransport(string namedPipesAddress) : base (namedPipesAddress)
{

}
} // class
} // namespace
49 changes: 49 additions & 0 deletions Spooky.Json20.JsonNet.SharedImplementation/JsonRpcRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Spooky.Json20
{
/// <summary>
/// Represents a request to a server made using the Json RPC v2.0 specification.
/// </summary>
/// <typeparam name="T">The type to use for the <seealso cref="Arguments"/> property. Normally either a Dictionary&lt;string, object&gt; for named arguments or an object array for positional arguments.</typeparam>
public class JsonRpcRequest<T>
{

/// <summary>
/// Gets or sets the Json RPC version being used. Defaults to <seealso cref="JsonRpcVersions.Version2"/>.
/// </summary>
/// /// <seealso cref="JsonRpcVersions"/>
/// <seealso cref="JsonRpcVersions.Version2"/>
/// <value>The version.</value>
[Newtonsoft.Json.JsonProperty("jsonrpc")]
public string Version { get; set; } = "2.0";

/// <summary>
/// Gets or sets the unique id of the request, used to correlate responses.
/// </summary>
/// <remarks>
/// <para>By default, a unique integer value is provided (in a thread-safe manner).</para>
/// </remarks>
/// <value>The identifier.</value>
[Newtonsoft.Json.JsonProperty("id")]
public int Id { get; set; } = JsonRpcRequestIdGenerator.GenerateUniqueId();

/// <summary>
/// Gets or sets the name of the remote method to be called.
/// </summary>
/// <value>The name of the method.</value>
[Newtonsoft.Json.JsonProperty("method")]

public string MethodName { get; set; }

/// <summary>
/// Gets or sets a dictionary of arguments to pass to the remote method.
/// </summary>
/// <value>The arguments.</value>
[Newtonsoft.Json.JsonProperty("params")]
public T Arguments { get; set; }

}
}
Loading

0 comments on commit 744a058

Please sign in to comment.