Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/JsonRpc/Client/Response.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OmniSharp.Extensions.JsonRpc.Client
{
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Response
{
public Response(object id) : this(id, null)
public Response(object id)
{
Id = id;
}

public Response(object id, object result)
Expand All @@ -22,4 +23,4 @@ public Response(object id, object result)

public object Result { get; set; }
}
}
}
43 changes: 0 additions & 43 deletions src/JsonRpc/Error.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/JsonRpc/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using OmniSharp.Extensions.JsonRpc.Client;
using OmniSharp.Extensions.JsonRpc.Client;

namespace OmniSharp.Extensions.JsonRpc
{
public struct ErrorResponse
{
public ErrorResponse(Error error)
public ErrorResponse(RpcError error)
{
Response = null;
Error = error;
Expand All @@ -20,17 +20,17 @@ public ErrorResponse(Response response)
public Response Response { get; }

public bool IsError => Error != null;
public Error Error { get; }
public RpcError Error { get; }
public object Value => IsResponse ? (object)Response : IsError ? Error : null;

public static implicit operator ErrorResponse(Response response)
{
return new ErrorResponse(response);
}

public static implicit operator ErrorResponse(Error error)
public static implicit operator ErrorResponse(RpcError error)
{
return new ErrorResponse(error);
}
}
}
}
14 changes: 2 additions & 12 deletions src/JsonRpc/HandlerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -66,16 +66,6 @@ public IDisposable Add(IJsonRpcHandler handler)
return h;
}

public IHandlerInstance Get(IJsonRpcHandler handler)
{
return _handlers.Find(instance => instance.Handler == handler);
}

public IHandlerInstance Get(string method)
{
return _handlers.Find(instance => instance.Method == method);
}

private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };

private string GetMethodName(Type type)
Expand Down Expand Up @@ -115,4 +105,4 @@ private Type GetHandlerInterface(Type type)
.First(IsValidInterface);
}
}
}
}
18 changes: 9 additions & 9 deletions src/JsonRpc/InputHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OmniSharp.Extensions.JsonRpc.Server;
using OmniSharp.Extensions.JsonRpc.Server.Messages;

namespace OmniSharp.Extensions.JsonRpc
Expand Down Expand Up @@ -147,13 +149,13 @@ private void HandleRequest(string request)
var tcs = _responseRouter.GetRequest(id);
if (tcs is null) continue;

if (response.Error is null)
if (response is ServerResponse serverResponse)
{
tcs.SetResult(response.Result);
tcs.SetResult(serverResponse.Result);
}
else
else if (response is ServerError serverError)
{
tcs.SetException(new Exception(response.Error));
tcs.SetException(new Exception(JsonConvert.SerializeObject(serverError.Error)));
}
}

Expand All @@ -167,8 +169,7 @@ private void HandleRequest(string request)
_scheduler.Add(
type,
item.Request.Method,
async () =>
{
async () => {
try
{
var result = await _requestRouter.RouteRequest(item.Request);
Expand All @@ -189,8 +190,7 @@ private void HandleRequest(string request)
_scheduler.Add(
type,
item.Notification.Method,
() =>
{
() => {
try
{
_requestRouter.RouteNotification(item.Notification);
Expand Down
7 changes: 4 additions & 3 deletions src/JsonRpc/Reciever.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc.Server;
Expand Down Expand Up @@ -63,12 +63,13 @@ protected virtual Renor GetRenor(JToken @object)

if (hasRequestId && request.TryGetValue("result", out var response))
{
return new Response(requestId, response);
return new ServerResponse(requestId, response);
}

if (hasRequestId && request.TryGetValue("error", out var errorResponse))
{
return new Response(requestId, errorResponse.ToString());
// TODO: this doesn't seem right.
return new ServerError(requestId, errorResponse);
}

var method = request["method"]?.Value<string>();
Expand Down
19 changes: 9 additions & 10 deletions src/JsonRpc/RequestRouter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -23,7 +24,7 @@ public IDisposable Add(IJsonRpcHandler handler)

public async void RouteNotification(Notification notification)
{
var handler = _collection.Get(notification.Method);
var handler = _collection.FirstOrDefault(x => x.Method == notification.Method);

Task result;
if (handler.Params is null)
Expand All @@ -46,16 +47,14 @@ public Task<ErrorResponse> RouteRequest(Request request)

protected virtual async Task<ErrorResponse> RouteRequest(Request request, CancellationToken token)
{
var handler = _collection.Get(request.Method);

var method = _collection.Get(request.Method);
if (method is null)
var handler = _collection.FirstOrDefault(x => x.Method == request.Method);
if (request.Method is null)
{
return new MethodNotFound(request.Id);
return new MethodNotFound(request.Id, request.Method);
}

Task result;
if (method.Params is null)
if (handler.Params is null)
{
result = ReflectionRequestHandlers.HandleRequest(handler, token);
}
Expand All @@ -64,7 +63,7 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
object @params;
try
{
@params = request.Params.ToObject(method.Params);
@params = request.Params.ToObject(handler.Params);
}
catch
{
Expand All @@ -89,4 +88,4 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
return new Client.Response(request.Id, responseValue);
}
}
}
}
42 changes: 42 additions & 0 deletions src/JsonRpc/RpcError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OmniSharp.Extensions.JsonRpc.Server.Messages;

namespace OmniSharp.Extensions.JsonRpc
{

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy)), JsonConverter(typeof(RpcErrorConverter))]
public class RpcError<T>
{
public RpcError(object id, ErrorMessage<T> message) : this(id, message, "2.0")
{
}

[JsonConstructor]
public RpcError(object id, ErrorMessage<T> message, string protocolVersion)
{
Id = id;
Error = message;
ProtocolVersion = protocolVersion;
}

public string ProtocolVersion { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object Id { get; }

public ErrorMessage<T> Error { get; }
}

public class RpcError : RpcError<object>
{
public RpcError(object id, ErrorMessage<object> message) : this(id, message, "2.0")
{
}

[JsonConstructor]
public RpcError(object id, ErrorMessage<object> message, string protocolVersion) : base(id, message, protocolVersion)
{
}
}
}
51 changes: 51 additions & 0 deletions src/JsonRpc/RpcErrorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc.Server.Messages;

namespace OmniSharp.Extensions.JsonRpc
{
public class RpcErrorConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);

var messageDataType = objectType == typeof(RpcError)
? typeof(object)
: objectType.GetTypeInfo().GetGenericArguments()[0];

object requestId = null;
if (obj.TryGetValue("id", out var id))
{
var idString = id.Type == JTokenType.String ? (string)id : null;
var idLong = id.Type == JTokenType.Integer ? (long?)id : null;
requestId = idString ?? (idLong.HasValue ? (object)idLong.Value : null);
}

object data = null;
if (obj.TryGetValue("error", out var dataToken))
{
var errorMessageType = typeof(ErrorMessage<>).MakeGenericType(messageDataType);
data = dataToken.ToObject(errorMessageType);
}

return Activator.CreateInstance(objectType, requestId, data, obj["protocolVersion"].ToString());
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(RpcError) ||
(objectType.GetTypeInfo().IsGenericType && objectType.GetTypeInfo().GetGenericTypeDefinition() == typeof(RpcError<>));
}

public override bool CanWrite { get; } = false;
public override bool CanRead { get; } = true;
}
}
Loading