4using System.Threading.Tasks;
17 private readonly TaskCompletionSource<T> result;
18 private readonly Func<object?, Task<T>> parseResult;
20 private readonly
string method;
21 private readonly
object? parameters;
22 private bool processed =
false;
25 object? Parameters,
IJsonRpcSession Session, Func<
object?, Task<T>> ParseResult,
31 this.parameters = Parameters;
32 this.session = Session;
33 this.parseResult = ParseResult;
34 this.webService = WebService;
36 this.result =
new TaskCompletionSource<T>();
47 public object?
Id {
get; }
53 public object?
Tag {
get;
set; }
63 if (!(Result is T ParsedResult))
64 ParsedResult = await this.parseResult(Result);
66 await this.
Return(ParsedResult);
81 this.processed =
true;
82 this.webService.RemoveClientRequest(this.Id?.
ToString() ??
string.Empty);
83 this.result.TrySetCanceled();
85 await this.
Cancelled.Raise(
this, EventArgs.Empty);
103 this.processed =
true;
104 this.webService.RemoveClientRequest(this.Id?.
ToString() ??
string.Empty);
105 this.result.TrySetException(
new Exception(ErrorMessage));
110 return Task.CompletedTask;
121 this.processed =
true;
122 this.webService.RemoveClientRequest(this.Id?.
ToString() ??
string.Empty);
123 this.result.TrySetResult(Result);
142 this.processed =
true;
143 this.webService.RemoveClientRequest(this.Id?.
ToString() ??
string.Empty);
144 this.result.TrySetException(
Error);
158 [Obsolete(
"Use DisposeAsync instead.")]
177 Dictionary<string, object?> Request =
new Dictionary<string, object?>()
179 {
"jsonrpc",
"2.0" },
181 {
"method", this.method },
182 {
"params", this.parameters }
186 int NrSent = await this.webService.SendEvent(
189 if (this.session.SessionId != Session?.
SessionId)
196 new KeyValuePair<string, object>(
"event",
"message"),
197 new KeyValuePair<string, object>(
"data", Data));
201 this.webService.RemoveClientRequest(this.Id?.
ToString() ??
string.Empty);
202 throw new IOException(
"Session no longer active.");
213 TaskCompletionSource<bool> Completed =
new TaskCompletionSource<bool>();
215 async
void KeepAlive(TaskCompletionSource<bool> Completed,
int Timeout)
217 DateTime Start = DateTime.UtcNow;
218 DateTime Until = Start.AddMilliseconds(Timeout);
223 while (!Completed.Task.IsCompleted && (TP = DateTime.UtcNow) < Until)
225 this.httpRequest.Ping();
226 await Task.Delay(Math.Min(1000, (
int)Until.Subtract(TP).TotalMilliseconds));
229 if (!Completed.Task.IsCompleted)
230 await this.
Error(
new TimeoutException());
238 KeepAlive(Completed, Timeout);
242 return await this.result.Task;
246 Completed.TrySetResult(
true);
Helps with common JSON-related tasks.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Represents an HTTP request.
Represents a request made to a JSON-RPC client.
EventHandlerAsync? Cancelled
Event raised when the request has been cancelled.
EventHandlerAsync? ErrorReturned
Event raised when an error has been returned for the request.
Task ReportError(int? ErrorCode, string ErrorMessage)
Called when an error is received for the request.
void Dispose()
IDisposable.Dispose
async Task< T > WaitForResultAsync(int Timeout)
Waits for a response to the request.
async Task Return(T Result)
Returns a result for the request.
async Task ReportResult(object? Result)
Called when a result is received for the request.
object? Tag
Property that can be used to store user-defined data associated with the request.
Task DisposeAsync()
Disposes of the object, asynchronously.
string Message
Message to user.
async Task SendRequest()
Sends the request to the MCP client.
async Task Cancel()
Called when the input dialog has been cancelled.
async Task Error(Exception Error)
Returns an error for the request.
EventHandlerAsync? ResultReturned
Event raised when a result has been returned for the request.
Abstract base class for Web Services based on JSON-RPC v2.0.
Interface for asynchronously disposable objects.
Interface for JSON-RPC client request objects.
Interface for JSON-RPC session objects.
void TransmitText(string Text)
Is called when text has been transmitted.
string SessionId
Session ID
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.