feat: add shared codegen support

This commit is contained in:
2026-09-05 02:34:02 +08:00
parent dd13a01bc9
commit 26241cb534
4 changed files with 16 additions and 11 deletions
+5 -1
View File
@@ -1,17 +1,21 @@
#if UNITY_5_3_OR_NEWER
using UnityEngine; using UnityEngine;
#endif
namespace ShrinkNetwork namespace ShrinkNetwork
{ {
public static class ShrinkNetworkRuntime public static class ShrinkNetworkRuntime
{ {
public static ShrinkNetworkService Default { get; private set; } public static ShrinkNetworkService Default { get; private set; } = null!;
static ShrinkNetworkRuntime() static ShrinkNetworkRuntime()
{ {
RebuildDefault(); RebuildDefault();
} }
#if UNITY_5_3_OR_NEWER
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
#endif
private static void ResetOnPlayModeEnter() private static void ResetOnPlayModeEnter()
{ {
RebuildDefault(); RebuildDefault();
@@ -1,3 +1,5 @@
#nullable enable
using System; using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@@ -16,14 +18,14 @@ namespace ShrinkNetwork
private readonly ShrinkKcpTransportOptions _options; private readonly ShrinkKcpTransportOptions _options;
private readonly object _syncRoot = new(); private readonly object _syncRoot = new();
private CancellationTokenSource _cts; private CancellationTokenSource? _cts;
private UdpClient _udpClient; private UdpClient? _udpClient;
private ShrinkKcpPeer _peer; private ShrinkKcpPeer? _peer;
private long _handshakeNonce; private long _handshakeNonce;
private int _started; private int _started;
private int _connected; private int _connected;
public ShrinkKcpClientTransport(string host, int port, ShrinkKcpTransportOptions options = null, long sessionId = 1) public ShrinkKcpClientTransport(string host, int port, ShrinkKcpTransportOptions? options = null, long sessionId = 1)
{ {
if (string.IsNullOrWhiteSpace(host)) if (string.IsNullOrWhiteSpace(host))
throw new ArgumentException("Host cannot be empty.", nameof(host)); throw new ArgumentException("Host cannot be empty.", nameof(host));
@@ -39,7 +41,7 @@ namespace ShrinkNetwork
public bool IsStarted => Volatile.Read(ref _started) == 1; public bool IsStarted => Volatile.Read(ref _started) == 1;
public event Action<ShrinkNetworkTransportEvent> OnEvent; public event Action<ShrinkNetworkTransportEvent>? OnEvent;
public void Start() public void Start()
{ {
@@ -52,7 +54,7 @@ namespace ShrinkNetwork
_udpClient.Connect(_host, _port); _udpClient.Connect(_host, _port);
_handshakeNonce = CreateHandshakeNonce(); _handshakeNonce = CreateHandshakeNonce();
ReceiveLoopAsync(_cts.Token).Forget(); ReceiveLoopAsync(_udpClient, _cts.Token).Forget();
HandshakeLoopAsync(_cts.Token).Forget(); HandshakeLoopAsync(_cts.Token).Forget();
UpdateLoopAsync(_cts.Token).Forget(); UpdateLoopAsync(_cts.Token).Forget();
} }
@@ -145,7 +147,7 @@ namespace ShrinkNetwork
} }
} }
private async UniTaskVoid ReceiveLoopAsync(CancellationToken cancellationToken) private async UniTaskVoid ReceiveLoopAsync(UdpClient udpClient, CancellationToken cancellationToken)
{ {
try try
{ {
@@ -154,7 +156,7 @@ namespace ShrinkNetwork
UdpReceiveResult result; UdpReceiveResult result;
try try
{ {
result = await _udpClient.ReceiveAsync(); result = await udpClient.ReceiveAsync();
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
@@ -37,7 +37,6 @@ namespace ShrinkNetwork
private CancellationTokenSource? _cts; private CancellationTokenSource? _cts;
private UdpClient? _udpClient; private UdpClient? _udpClient;
private long _sessionIdGenerator; private long _sessionIdGenerator;
private int _conversationIdGenerator;
public ShrinkKcpServerTransport(IPAddress listeningAddress, int port, ShrinkKcpTransportOptions? options = null) public ShrinkKcpServerTransport(IPAddress listeningAddress, int port, ShrinkKcpTransportOptions? options = null)
{ {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "com.cneicy.shrink-network", "name": "com.cneicy.shrink-network",
"version": "0.2.1", "version": "0.3.0",
"displayName": "ShrinkNetwork", "displayName": "ShrinkNetwork",
"description": "面向 Unity 的轻量网络框架,提供会话、消息注册、RPC、权限控制,以及 TCP/KCP 传输抽象。", "description": "面向 Unity 的轻量网络框架,提供会话、消息注册、RPC、权限控制,以及 TCP/KCP 传输抽象。",
"unity": "2022.3", "unity": "2022.3",