Skip to content

Commit

Permalink
加上无参的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Sep 15, 2023
1 parent e2a4ef6 commit dcfaf63
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds;

public abstract class IpcDirectRoutedClientProxyBase
{
/// <summary>
/// 业务头
/// </summary>
protected abstract ulong BusinessHeader { get; }

protected void WriteHeader(MemoryStream stream, string routedPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ public JsonIpcDirectRoutedClientProxy(IPeerProxy peerProxy)
private JsonSerializer? _jsonSerializer;
private JsonSerializer JsonSerializer => _jsonSerializer ??= JsonSerializer.CreateDefault();

/// <summary>
/// 不带参数的通知服务端
/// </summary>
/// <param name="routedPath"></param>
/// <returns></returns>
public Task NotifyAsync(string routedPath)
=> NotifyAsync(routedPath, JsonIpcDirectRoutedParameterlessType.Instance);

public Task NotifyAsync<T>(string routedPath, T obj) where T : class
{
IpcMessage ipcMessage = BuildMessage(routedPath, obj);
return _peerProxy.NotifyAsync(ipcMessage);
}

public Task<TResponse?> GetResponseAsync<TResponse>(string routedPath) where TResponse : class
=> GetResponseAsync<TResponse>(routedPath, JsonIpcDirectRoutedParameterlessType.Instance);

public async Task<TResponse?> GetResponseAsync<TResponse>(string routedPath, object obj) where TResponse : class
{
IpcMessage ipcMessage = BuildMessage(routedPath, obj);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds;

/// <summary>
/// 表示在 JsonIpcDirectRouted 使用的无参类型
/// </summary>
internal class JsonIpcDirectRoutedParameterlessType
{
public static JsonIpcDirectRoutedParameterlessType Instance
{
get => _instance ??= new JsonIpcDirectRoutedParameterlessType();
}

private static JsonIpcDirectRoutedParameterlessType? _instance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public async Task<JsonIpcDirectRoutedClientProxy> GetAndConnectClientAsync(strin

#region Notify

/// <summary>
/// 添加通知的处理,无参
/// </summary>
/// <param name="routedPath"></param>
/// <param name="handler"></param>
public void AddNotifyHandler(string routedPath, Action handler)
=> AddNotifyHandler<JsonIpcDirectRoutedParameterlessType>(routedPath, _ => handler());

/// <summary>
/// 添加通知的处理,无参
/// </summary>
/// <param name="routedPath"></param>
/// <param name="handler"></param>
public void AddNotifyHandler(string routedPath, Func<Task> handler)
=> AddNotifyHandler<JsonIpcDirectRoutedParameterlessType>(routedPath, _ => handler());

/// <summary>
/// 添加通知的处理
/// </summary>
Expand Down Expand Up @@ -130,8 +146,10 @@ void HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context)

private ConcurrentDictionary<string, HandleNotify> HandleNotifyDictionary { get; } = new ConcurrentDictionary<string, HandleNotify>();

/// <inheritdoc />
protected override ulong BusinessHeader => (ulong) KnownMessageHeaders.JsonIpcDirectRoutedMessageHeader;

/// <inheritdoc />
protected override void OnHandleNotify(IpcDirectRoutedMessage message, PeerMessageArgs e)
{
// 接下来进行调度
Expand Down Expand Up @@ -167,6 +185,24 @@ protected override void OnHandleNotify(IpcDirectRoutedMessage message, PeerMessa

#region Request Response

/// <summary>
/// 添加请求的处理,无参请求
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="routedPath"></param>
/// <param name="handler"></param>
public void AddRequestHandler<TResponse>(string routedPath, Func<TResponse> handler) =>
AddRequestHandler<JsonIpcDirectRoutedParameterlessType, TResponse>(routedPath, _ => handler());

/// <summary>
/// 添加请求的处理,无参请求
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="routedPath"></param>
/// <param name="handler"></param>
public void AddRequestHandler<TResponse>(string routedPath, Func<Task<TResponse>> handler) =>
AddRequestHandler<JsonIpcDirectRoutedParameterlessType, TResponse>(routedPath, _ => handler());

/// <summary>
/// 添加请求的处理
/// </summary>
Expand Down Expand Up @@ -334,4 +370,3 @@ protected override async Task<IIpcResponseMessage> OnHandleRequestAsync(IpcDirec
return JsonSerializer.Deserialize<T>(jsonReader);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ public void TestNotify()
// 允许无参数,如果只是做客户端使用的话
JsonIpcDirectRoutedProvider clientProvider = new();
clientProvider.StartServer();
// todo 命名:这里叫 ClientProxy 正确还是 ServerProxy 正确
var clientProxy = await clientProvider.GetAndConnectClientAsync(serverName);

await clientProxy.NotifyAsync(routedPath, argument);
Expand All @@ -393,6 +392,68 @@ public void TestNotify()
});
}

/// <summary>
/// 测试无参版本
/// </summary>
[ContractTestCase]
public void TestParameterless()
{
"发送无参请求,可以让服务端收到请求".Test(async () =>
{
// 初始化服务端
var serverName = "JsonIpcDirectRoutedProviderTest_Test_Parameterless_1";
var serverProvider = new JsonIpcDirectRoutedProvider(serverName);
var routedPath = "Foo1";

// 注册无参数请求处理
serverProvider.AddRequestHandler(routedPath, () =>
{
return new FakeResult(nameof(TestParameterless));
});

serverProvider.StartServer();

// 创建客户端
JsonIpcDirectRoutedProvider clientProvider = new();
clientProvider.StartServer();
var clientProxy = await clientProvider.GetAndConnectClientAsync(serverName);

// 请求无参数
var result = await clientProxy.GetResponseAsync<FakeResult>(routedPath);

// 如果能收到服务端返回值,证明请求成功
Assert.IsNotNull(result);
Assert.AreEqual(nameof(TestParameterless), result.Name);
});

"发送无参通知,可以让服务端收到通知".Test(async () =>
{
// 初始化服务端
var serverName = "JsonIpcDirectRoutedProviderTest_Test_Parameterless_2";
var serverProvider = new JsonIpcDirectRoutedProvider(serverName);
var routedPath = "Foo1";

var taskCompletionSource = new TaskCompletionSource();
serverProvider.AddNotifyHandler(routedPath,()=>
{
taskCompletionSource.SetResult();
});

serverProvider.StartServer();

// 创建客户端
JsonIpcDirectRoutedProvider clientProvider = new();
clientProvider.StartServer();
var clientProxy = await clientProvider.GetAndConnectClientAsync(serverName);
await clientProxy.NotifyAsync(routedPath);

// 再等待一下,让服务端处理完成
await taskCompletionSource.Task.WaitTimeout(TimeSpan.FromSeconds(1));
// 预期是服务端能够执行完成
Assert.AreEqual(true, taskCompletionSource.Task.IsCompleted);
});
}

record class FakeArgument(string Name, int Count)
{
}
Expand Down

0 comments on commit dcfaf63

Please sign in to comment.