Điều phối luồng
Trên trang này
Namespace: Nalix.SDK
Assembly: Nalix.SDK
IThreadDispatcher trừu tượng hóa cách các callback từ luồng nhận mạng chạy nền được lên lịch sang luồng đích của ứng dụng (chẳng hạn vòng lặp chính của Unity, luồng chính của Godot, Dispatcher của WPF/WinUI hoặc MainThread của MAUI).
Ánh xạ mã nguồn
src/Nalix.SDK/IThreadDispatcher.cssrc/Nalix.SDK/InlineDispatcher.cs
Contract công khai
namespace Nalix.SDK;
public interface IThreadDispatcher
{
void Post(Action action);
}
public sealed class InlineDispatcher : IThreadDispatcher
{
[Pure]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Post(Action action) => action?.Invoke();
}
Cách sử dụng IThreadDispatcher
InlineDispatcher: Thực thiaction?.Invoke()đồng bộ trên luồng gọi, không chuyển ngữ cảnh và không cấp phát hàng đợi.- Dispatcher tùy chỉnh cho UI / engine: Khi xử lý gói tin qua TransportSessionSubscriptions.On<TPacket> (đặt
disposeAfter: falsenếu truyền gói lấy từ pool sang luồng khác) hoặcOnMessageReceived(gọilease.Retain()trước khi post vàlease.Dispose()bên trong action được post), hãy gọidispatcher.Post(() => ...)để đưa việc thay đổi trạng thái về luồng chính của engine.
Triển khai IThreadDispatcher cho engine game và UI
Unity (SynchronizationContext / hàng đợi luồng chính)
using System;
using System.Collections.Concurrent;
using Nalix.SDK;
using UnityEngine;
public sealed class UnityMainThreadDispatcher : MonoBehaviour, IThreadDispatcher
{
private readonly ConcurrentQueue<Action> _queue = new();
public void Post(Action action)
{
if (action is not null)
{
_queue.Enqueue(action);
}
}
private void Update()
{
while (_queue.TryDequeue(out Action? action))
{
action();
}
}
}
.NET MAUI / WPF (SynchronizationContext)
using System;
using System.Threading;
using Nalix.SDK;
public sealed class SyncContextDispatcher(SynchronizationContext context) : IThreadDispatcher
{
public void Post(Action action) => context.Post(static state => ((Action)state!)(), action);
}