2022-01-05 21:16:17 +00:00
|
|
|
|
// See https://aka.ms/new-console-template for more information
|
|
|
|
|
using Example;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
class TestSrvimpl : Example.TestServiceServer<int>
|
|
|
|
|
{
|
|
|
|
|
public TestSrvimpl() : base() { }
|
|
|
|
|
public override async Task<Example.AddValueResponse> AddValuesSingleParam(AddValueRequest request, int ctx)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
var res = new Example.AddValueResponse();
|
|
|
|
|
res.value = 1;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override async Task<double> AddValuesMultipleParams(double value1, double value2, int ctx)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
return value1 + value2;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override async Task ReturningVoid(double param1, int ctx)
|
|
|
|
|
{
|
|
|
|
|
|
2022-01-05 21:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override void OnEvent(string param1, int ctx)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
Console.WriteLine($"OnEvent {param1}");
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override async Task<IList<double>> FunctionWithArrayAsParamAndReturn(List<double> values1, List<double> values2, int ctx)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
var l = new List<double>();
|
|
|
|
|
l.Append(1);
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override async Task ThrowingError(int ctx)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
throw new Exception("This is a remote error :)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
class CopyTransportS2 : Example.JRpcTransport
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
CopyTransportS1 tr1;
|
|
|
|
|
public Queue<string> backlog = new Queue<string>();
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public CopyTransportS2(CopyTransportS1 tr1)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
this.tr1 = tr1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override Task Write(string data)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
Console.WriteLine("--> " + data);
|
2022-01-10 14:08:38 +00:00
|
|
|
|
this.tr1.SendPacketEvent(data);
|
2022-01-05 21:16:17 +00:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
class CopyTransportS1 : Example.JRpcTransport
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
public Queue<string> backlog = new Queue<string>();
|
|
|
|
|
|
|
|
|
|
public CopyTransportS2 tr2;
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public CopyTransportS1()
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
this.tr2 = new CopyTransportS2(this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public override Task Write(string data)
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
Console.WriteLine("<-- " + data);
|
2022-01-10 14:08:38 +00:00
|
|
|
|
this.tr2.SendPacketEvent(data);
|
2022-01-05 21:16:17 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
Program.Start().Wait();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
public static async Task Start()
|
|
|
|
|
{
|
2022-01-05 21:16:17 +00:00
|
|
|
|
var server = new Example.JRpcServer<int>();
|
2022-01-07 22:10:24 +00:00
|
|
|
|
server.AddService(new TestSrvimpl());
|
2022-01-05 21:16:17 +00:00
|
|
|
|
var transport = new CopyTransportS1();
|
|
|
|
|
var sess = server.GetSession(transport, 0);
|
2022-01-10 14:08:38 +00:00
|
|
|
|
|
2022-01-05 21:16:17 +00:00
|
|
|
|
var client = new Example.JRpcClient(transport.tr2);
|
|
|
|
|
var testService = new Example.TestServiceClient(client);
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
var result = await testService.AddValuesMultipleParams(1, 2);
|
2022-01-05 21:16:17 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Add 1 + 2 = {result}");
|
|
|
|
|
|
2022-07-16 21:34:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await testService.ThrowingError();
|
|
|
|
|
throw new Exception("No error was issued, even though it was expected!");
|
|
|
|
|
}
|
|
|
|
|
catch (JRpcException err)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error successfully catched: ", err.Message);
|
|
|
|
|
}
|
2022-01-05 21:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|