Skip to content

Commit

Permalink
Factored out interface
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 14, 2023
1 parent 8c4f5b4 commit b575cf6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/Draco.JsonRpc/IJsonRpcConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Draco.JsonRpc;

/// <summary>
/// Represents a JSON RPC connection.
/// </summary>
public interface IJsonRpcConnection
{
/// <summary>
/// Registers an RPC method handler.
/// </summary>
/// <param name="handler">The handler to register.</param>
public void AddHandler(IJsonRpcMethodHandler handler);

/// <summary>
/// Starts the connection to listen for messages.
/// </summary>
/// <returns>The task that completes when the listening stops.</returns>
public Task ListenAsync();

/// <summary>
/// Stops the connection.
/// </summary>
public void Shutdown();
}
4 changes: 1 addition & 3 deletions src/Draco.JsonRpc/JsonRpcConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Draco.JsonRpc;
/// </summary>
/// <typeparam name="TMessage">The message type.</typeparam>
/// <typeparam name="TError">The error message type.</typeparam>
public abstract class JsonRpcConnection<TMessage, TError>
public abstract class JsonRpcConnection<TMessage, TError> : IJsonRpcConnection
where TMessage : IJsonRpcMessage
{
private interface IOutgoingRequest
Expand Down Expand Up @@ -87,10 +87,8 @@ private enum ConsumerState
SingleWriter = false,
});

// TODO: Doc
public void AddHandler(IJsonRpcMethodHandler handler) => this.methodHandlers.Add(handler.Method, handler);

// TODO: Doc
public Task ListenAsync() => Task.WhenAll(this.ReaderLoop(), this.WriterLoop());

private Task WriterLoop() => Task.Run(async () =>
Expand Down

0 comments on commit b575cf6

Please sign in to comment.