完成阶段 4
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public interface INetworkMessageDispatcher
|
||||
{
|
||||
int PendingCount { get; }
|
||||
|
||||
void Enqueue(Func<Task> workItem);
|
||||
|
||||
Task<int> DrainAsync(int maxItems = int.MaxValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 688ae8436f5f43fa88382700ef9c5e58
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public sealed class ImmediateNetworkMessageDispatcher : INetworkMessageDispatcher
|
||||
{
|
||||
public int PendingCount => 0;
|
||||
|
||||
public void Enqueue(Func<Task> workItem)
|
||||
{
|
||||
if (workItem == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(workItem));
|
||||
}
|
||||
|
||||
workItem().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public Task<int> DrainAsync(int maxItems = int.MaxValue)
|
||||
{
|
||||
if (maxItems <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(maxItems));
|
||||
}
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c1b99c76091f41299af26864a9a25fb0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public sealed class MainThreadNetworkDispatcher : INetworkMessageDispatcher
|
||||
{
|
||||
private readonly ConcurrentQueue<Func<Task>> pendingWork = new();
|
||||
|
||||
public int PendingCount => pendingWork.Count;
|
||||
|
||||
public void Enqueue(Func<Task> workItem)
|
||||
{
|
||||
if (workItem == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(workItem));
|
||||
}
|
||||
|
||||
pendingWork.Enqueue(workItem);
|
||||
}
|
||||
|
||||
public async Task<int> DrainAsync(int maxItems = int.MaxValue)
|
||||
{
|
||||
if (maxItems <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(maxItems));
|
||||
}
|
||||
|
||||
var processed = 0;
|
||||
|
||||
while (processed < maxItems && pendingWork.TryDequeue(out var workItem))
|
||||
{
|
||||
await workItem();
|
||||
processed++;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3dc7b1ecbad541ea86a9d700dd5148e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
@@ -11,16 +11,22 @@ namespace Network.NetworkApplication
|
||||
public class MessageManager
|
||||
{
|
||||
private readonly ITransport transport;
|
||||
private readonly INetworkMessageDispatcher dispatcher;
|
||||
|
||||
private readonly Dictionary<MessageType, Func<byte[], IPEndPoint, Task>> handlers =
|
||||
new();
|
||||
|
||||
public MessageManager(ITransport transport)
|
||||
public MessageManager(ITransport transport, INetworkMessageDispatcher dispatcher)
|
||||
{
|
||||
this.transport = transport ?? throw new ArgumentNullException(nameof(transport));
|
||||
this.transport.OnReceive += OnTransportReceiveAsync;
|
||||
this.dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
|
||||
this.transport.OnReceive += OnTransportReceive;
|
||||
}
|
||||
|
||||
public INetworkMessageDispatcher Dispatcher => dispatcher;
|
||||
|
||||
public int PendingMessageCount => dispatcher.PendingCount;
|
||||
|
||||
public void RegisterHandler(MessageType type, IMessageHandler handler)
|
||||
{
|
||||
if (handler == null)
|
||||
@@ -94,7 +100,12 @@ namespace Network.NetworkApplication
|
||||
transport.SendToAll(envelope.ToByteArray());
|
||||
}
|
||||
|
||||
private async void OnTransportReceiveAsync(byte[] data, IPEndPoint sender)
|
||||
public Task<int> DrainPendingMessagesAsync(int maxMessages = int.MaxValue)
|
||||
{
|
||||
return dispatcher.DrainAsync(maxMessages);
|
||||
}
|
||||
|
||||
private void OnTransportReceive(byte[] data, IPEndPoint sender)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -104,7 +115,8 @@ namespace Network.NetworkApplication
|
||||
|
||||
if (handlers.TryGetValue(type, out var handler))
|
||||
{
|
||||
await handler(envelope.Payload.ToByteArray(), sender);
|
||||
var payload = envelope.Payload.ToByteArray();
|
||||
dispatcher.Enqueue(() => DispatchAsync(handler, payload, sender, type));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -116,5 +128,21 @@ namespace Network.NetworkApplication
|
||||
Console.WriteLine($"[MessageManager] 消息处理错误:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task DispatchAsync(
|
||||
Func<byte[], IPEndPoint, Task> handler,
|
||||
byte[] payload,
|
||||
IPEndPoint sender,
|
||||
MessageType type)
|
||||
{
|
||||
try
|
||||
{
|
||||
await handler(payload, sender);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[MessageManager] Handler 执行错误:{type} -> {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Network.NetworkTransport;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public sealed class SharedNetworkRuntime
|
||||
{
|
||||
public SharedNetworkRuntime(ITransport transport, INetworkMessageDispatcher dispatcher)
|
||||
{
|
||||
Transport = transport ?? throw new ArgumentNullException(nameof(transport));
|
||||
MessageManager = new MessageManager(transport, dispatcher ?? throw new ArgumentNullException(nameof(dispatcher)));
|
||||
}
|
||||
|
||||
public ITransport Transport { get; }
|
||||
|
||||
public MessageManager MessageManager { get; }
|
||||
|
||||
public Task StartAsync()
|
||||
{
|
||||
return Transport.StartAsync();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Transport.Stop();
|
||||
}
|
||||
|
||||
public Task<int> DrainPendingMessagesAsync(int maxMessages = int.MaxValue)
|
||||
{
|
||||
return MessageManager.DrainPendingMessagesAsync(maxMessages);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 314c8d46e1ae4d9eb4914d0cac7bb628
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user