|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Buffers; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.IO; |
5 | 5 | using System.Linq; |
| 6 | +using System.Runtime.CompilerServices; |
6 | 7 | using System.Text; |
7 | 8 | using System.Threading.Tasks; |
8 | 9 | using EdjCase.JsonRpc.Router.Utilities; |
9 | 10 |
|
10 | 11 | namespace EdjCase.JsonRpc.Router.Abstractions |
11 | 12 | { |
12 | | - internal interface IRpcRequestHandler |
| 13 | + public interface IRpcRequestHandler |
13 | 14 | { |
14 | | - Task<bool> HandleRequestAsync(Stream requestBody, Stream responseBody); |
| 15 | + /// <summary> |
| 16 | + /// Takes in the request bytes and context, invokes the rpc request and then |
| 17 | + /// sets the response bytes if there is a response |
| 18 | + /// </summary> |
| 19 | + /// <param name="context">Contextual information about the request being handled</param> |
| 20 | + /// <param name="requestBody">The request byte stream</param> |
| 21 | + /// <param name="responseBody">An writable stream to write the response to</param> |
| 22 | + /// <returns>True if there is a response. If false, no bytes will be written to the stream</returns> |
| 23 | + Task<bool> HandleRequestAsync(RpcContext context, Stream requestBody, Stream responseBody); |
15 | 24 |
|
16 | | - public virtual async Task<string?> HandleRequestAsync(string requestJson) |
| 25 | + /// <summary> |
| 26 | + /// Takes in the request bytes and context, invokes the rpc request and then returns |
| 27 | + /// the response bytes if there is a response |
| 28 | + /// </summary> |
| 29 | + /// <param name="context">Contextual information about the request being handled</param> |
| 30 | + /// <param name="requestBody">The request bytes</param> |
| 31 | + /// <returns>The response bytes or null (if there is no response)</returns> |
| 32 | + public virtual async Task<byte[]?> HandleRequestAsync(RpcContext context, byte[] requestBody) |
| 33 | + { |
| 34 | + using (var requestStream = new MemoryStream(requestBody)) |
| 35 | + { |
| 36 | + using (var responseStream = new MemoryStream()) |
| 37 | + { |
| 38 | + bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream); |
| 39 | + if (!hasResponse) |
| 40 | + { |
| 41 | + return null; |
| 42 | + } |
| 43 | + responseStream.Position = 0; |
| 44 | + return responseStream.ToArray(); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Takes in the request json and context, invokes the rpc request and then returns |
| 51 | + /// the response json if there is a response |
| 52 | + /// </summary> |
| 53 | + /// <param name="context">Contextual information about the request being handled</param> |
| 54 | + /// <param name="requestJson">The request json</param> |
| 55 | + /// <returns>The response json or null (if there is no response)</returns> |
| 56 | + public virtual async Task<string?> HandleRequestAsync(RpcContext context, string requestJson) |
17 | 57 | { |
18 | 58 | using (var requestStream = StreamUtil.GetStreamFromUtf8String(requestJson)) |
19 | 59 | { |
20 | 60 | using (var responseStream = new MemoryStream()) |
21 | 61 | { |
22 | | - bool hasResponse = await this.HandleRequestAsync(requestStream, responseStream); |
| 62 | + bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream); |
23 | 63 | if (!hasResponse) |
24 | 64 | { |
25 | 65 | return null; |
|
0 commit comments