From 053851b4f159ca32099598007138fdffbe40f725 Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 13 Sep 2023 10:09:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=9B=9E=20.NET=20Framework=20?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Base_/IpcDirectRoutedClientProxyBase.cs | 4 +- .../Base_/IpcDirectRoutedProviderBase.cs | 19 ++++++-- .../IpcDirectRoutedMessageWriter.cs | 4 +- .../Json_/JsonIpcDirectRoutedClientProxy.cs | 17 +++++-- .../Json_/JsonIpcDirectRoutedContext.cs | 14 ++++-- .../Json_/JsonIpcDirectRoutedProvider.cs | 47 +++++++++++++++---- .../RawByteIpcDirectRoutedClientProxy.cs | 33 +++++++++++-- .../RawByteIpcDirectRoutedProvider.cs | 4 +- 8 files changed, 108 insertions(+), 34 deletions(-) diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedClientProxyBase.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedClientProxyBase.cs index 6e27884..58c18f8 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedClientProxyBase.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedClientProxyBase.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System.IO; +using System.IO; using System.Text; using dotnetCampus.Ipc.Messages; @@ -23,4 +22,3 @@ protected IpcMessage ToIpcMessage(MemoryStream stream, string tag = "") return new IpcMessage(tag, new IpcMessageBody(buffer, start: 0, length)); } } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedProviderBase.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedProviderBase.cs index 14603bf..234d5cb 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedProviderBase.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Base_/IpcDirectRoutedProviderBase.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System; +using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -140,9 +139,16 @@ private bool TryHandleMessage(in IpcMessage ipcMessage, [NotNullWhen(true)] out return false; } - protected readonly record struct IpcDirectRoutedMessage(string RoutedPath, MemoryStream Stream, - IpcMessage PayloadIpcMessage) + protected readonly struct IpcDirectRoutedMessage { + public IpcDirectRoutedMessage(string routedPath, MemoryStream stream, + IpcMessage payloadIpcMessage) + { + this.RoutedPath = routedPath; + this.Stream = stream; + this.PayloadIpcMessage = payloadIpcMessage; + } + public IpcMessageBody GetData() { var position = (int) Stream.Position; @@ -150,7 +156,10 @@ public IpcMessageBody GetData() var data = new IpcMessageBody(payload.Buffer, payload.Start + position, payload.Length - position); return data; } + + public string RoutedPath { get; } + public MemoryStream Stream { get; } + public IpcMessage PayloadIpcMessage { get; } } } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/IpcDirectRoutedMessageWriter.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/IpcDirectRoutedMessageWriter.cs index e2068a3..cb82dbb 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/IpcDirectRoutedMessageWriter.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/IpcDirectRoutedMessageWriter.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System.IO; +using System.IO; namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds; @@ -11,4 +10,3 @@ public static void WriteHeader(BinaryWriter writer, ulong businessMessageHeader, writer.Write(routedPath); } } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedClientProxy.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedClientProxy.cs index 7ed7500..67e8c17 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedClientProxy.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedClientProxy.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System.IO; +using System.IO; using System.Text; using System.Threading.Tasks; @@ -33,7 +32,16 @@ public Task NotifyAsync(string routedPath, T obj) where T : class var responseMessage = await _peerProxy.GetResponseAsync(ipcMessage); using var memoryStream = new MemoryStream(responseMessage.Body.Buffer, responseMessage.Body.Start, responseMessage.Body.Length, writable: false); - using StreamReader reader = new StreamReader(memoryStream, leaveOpen: true); + using StreamReader reader = new StreamReader + ( + memoryStream, +#if !NETCOREAPP + Encoding.UTF8, + detectEncodingFromByteOrderMarks: false, + bufferSize: 2048, +#endif + leaveOpen: true + ); JsonReader jsonReader = new JsonTextReader(reader); return JsonSerializer.Deserialize(jsonReader); } @@ -43,7 +51,7 @@ private IpcMessage BuildMessage(string routedPath, object obj) using var memoryStream = new MemoryStream(); WriteHeader(memoryStream, routedPath); - using (var textWriter = new StreamWriter(memoryStream, Encoding.UTF8, leaveOpen: true)) + using (var textWriter = new StreamWriter(memoryStream, Encoding.UTF8, leaveOpen: true, bufferSize: 2048)) { JsonSerializer.Serialize(textWriter, obj); } @@ -53,4 +61,3 @@ private IpcMessage BuildMessage(string routedPath, object obj) protected override ulong BusinessHeader => (ulong) KnownMessageHeaders.JsonIpcDirectRoutedMessageHeader; } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedContext.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedContext.cs index ce369f0..07e98d5 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedContext.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedContext.cs @@ -1,5 +1,11 @@ -#if NET6_0_OR_GREATER -namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds; +namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds; -public record JsonIpcDirectRoutedContext(string PeerName); -#endif +public class JsonIpcDirectRoutedContext +{ + public JsonIpcDirectRoutedContext(string peerName) + { + this.PeerName = peerName; + } + + public string PeerName { get; } +} diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedProvider.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedProvider.cs index a1f75ae..840397c 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedProvider.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedProvider.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -16,6 +15,10 @@ using Newtonsoft.Json; +#if !NETCOREAPP +using ValueTask = System.Threading.Tasks.Task; +#endif + namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds; /// @@ -133,7 +136,8 @@ void HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context) protected override void OnHandleNotify(IpcDirectRoutedMessage message, PeerMessageArgs e) { // 接下来进行调度 - var (routedPath, stream, _) = message; + var routedPath = message.RoutedPath; + var stream = message.Stream; if (HandleNotifyDictionary.TryGetValue(routedPath, out var handleNotify)) { var context = new JsonIpcDirectRoutedContext(e.PeerName); @@ -228,12 +232,26 @@ public void AddRequestHandler(string routedPath, throw new InvalidOperationException($"重复添加对 {routedPath} 的处理"); } +#if NETCOREAPP async ValueTask HandleRequest(MemoryStream stream, JsonIpcDirectRoutedContext context) +#else + async Task HandleRequest(MemoryStream stream, JsonIpcDirectRoutedContext context) +#endif { var argument = ToObject(stream); var response = await handler(argument!, context); var responseMemoryStream = new MemoryStream(); - using (TextWriter textWriter = new StreamWriter(responseMemoryStream, Encoding.UTF8, leaveOpen: true)) + using + ( + TextWriter textWriter = new StreamWriter + ( + responseMemoryStream, Encoding.UTF8 +#if !NETCOREAPP + , bufferSize: 2048 +#endif + , leaveOpen: true + ) + ) { JsonSerializer.Serialize(textWriter, response); } @@ -244,14 +262,19 @@ async ValueTask HandleRequest(MemoryStream stream, JsonIpcDirectRout } } +#if NETCOREAPP private delegate ValueTask HandleRequest(MemoryStream stream, JsonIpcDirectRoutedContext context); +#else + private delegate Task HandleRequest(MemoryStream stream, JsonIpcDirectRoutedContext context); +#endif private ConcurrentDictionary HandleRequestDictionary { get; } = new ConcurrentDictionary(); protected override async Task OnHandleRequestAsync(IpcDirectRoutedMessage message, IIpcRequestContext requestContext) { - var (routedPath, stream, _) = message; + var routedPath = message.RoutedPath; + var stream = message.Stream; if (HandleRequestDictionary.TryGetValue(routedPath, out var handler)) { @@ -284,7 +307,7 @@ protected override async Task OnHandleRequestAsync(IpcDirec } } - #endregion +#endregion private JsonSerializer JsonSerializer => _jsonSerializer ??= JsonSerializer.CreateDefault(); private JsonSerializer? _jsonSerializer; @@ -292,10 +315,18 @@ protected override async Task OnHandleRequestAsync(IpcDirec private T? ToObject(MemoryStream stream) { - using StreamReader reader = new StreamReader(stream, leaveOpen: true); + using StreamReader reader = new StreamReader + ( + stream, +#if !NETCOREAPP + Encoding.UTF8, + bufferSize: 2048, + detectEncodingFromByteOrderMarks: false, +#endif + leaveOpen: true + ); JsonReader jsonReader = new JsonTextReader(reader); return JsonSerializer.Deserialize(jsonReader); } } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedClientProxy.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedClientProxy.cs index 163774d..9cd4de9 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedClientProxy.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedClientProxy.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System; +using System; using System.IO; using System.Threading.Tasks; using dotnetCampus.Ipc.Context; @@ -19,6 +18,11 @@ public RawByteIpcDirectRoutedClientProxy(IPeerProxy peerProxy) private readonly IPeerProxy _peerProxy; +#if NETCOREAPP + + public Task NotfiyAsync(string routedPath, in IpcMessageBody data) + => NotfiyAsync(routedPath, data.AsSpan()); + public Task NotfiyAsync(string routedPath, Span data) { IpcMessage ipcMessage = BuildMessage(routedPath, data); @@ -42,7 +46,30 @@ private IpcMessage BuildMessage(string routedPath, Span data) return ToIpcMessage(memoryStream, $"Message To {routedPath}"); } +#else + public Task NotfiyAsync(string routedPath, IpcMessageBody data) + { + IpcMessage ipcMessage = BuildMessage(routedPath, data); + return _peerProxy.NotifyAsync(ipcMessage); + } + + public async Task GetResponseAsync(string routedPath, IpcMessageBody ipcMessageBody) + { + IpcMessage ipcMessage = BuildMessage(routedPath, ipcMessageBody); + + var response = await _peerProxy.GetResponseAsync(ipcMessage); + return response.Body; + } + + private IpcMessage BuildMessage(string routedPath, in IpcMessageBody data) + { + using var memoryStream = new MemoryStream(); + WriteHeader(memoryStream, routedPath); + memoryStream.Write(data.Buffer, data.Start, data.Length); + + return ToIpcMessage(memoryStream, $"Message To {routedPath}"); + } +#endif protected override ulong BusinessHeader => (ulong) KnownMessageHeaders.RawByteIpcDirectRoutedMessageHeader; } -#endif diff --git a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedProvider.cs b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedProvider.cs index daada0d..f5c47bb 100644 --- a/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedProvider.cs +++ b/src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/RawByte_/RawByteIpcDirectRoutedProvider.cs @@ -1,5 +1,4 @@ -#if NET6_0_OR_GREATER -using System; +using System; using System.Collections.Concurrent; using System.IO; using System.Threading.Tasks; @@ -211,4 +210,3 @@ protected override async Task OnHandleRequestAsync(IpcDirec } } } -#endif