Skip to content

Commit c8e91d1

Browse files
committed
feat: Added pre init channel registration
1 parent 70ee6a9 commit c8e91d1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

MLAPI/Core/NetworkingManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ private void Init(bool server)
400400

401401
NetworkConfig.NetworkTransport.OnTransportEvent += HandleRawTransportPoll;
402402

403+
NetworkConfig.NetworkTransport.ResetChannelCache();
404+
403405
NetworkConfig.NetworkTransport.Init();
404406
}
405407

MLAPI/Transports/Transport.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using MLAPI.Transports.Tasks;
34
using UnityEngine;
45

@@ -9,6 +10,16 @@ namespace MLAPI.Transports
910
/// </summary>
1011
public abstract class Transport : MonoBehaviour
1112
{
13+
/// <summary>
14+
/// Delegate used to request channels on the underlying transport.
15+
/// </summary>
16+
public delegate void RequestChannelsDelegate(List<TransportChannel> channels);
17+
18+
/// <summary>
19+
/// Delegate called when the transport wants to know what channels to register.
20+
/// </summary>
21+
public event RequestChannelsDelegate OnChannelRegistration;
22+
1223
/// <summary>
1324
/// A constant clientId that represents the server.
1425
/// When this value is found in methods such as Send, it should be treated as a placeholder that means "the server"
@@ -22,10 +33,44 @@ public abstract class Transport : MonoBehaviour
2233
/// <value><c>true</c> if is supported; otherwise, <c>false</c>.</value>
2334
public virtual bool IsSupported => true;
2435

36+
private TransportChannel[] _channelsCache = null;
37+
38+
internal void ResetChannelCache()
39+
{
40+
_channelsCache = null;
41+
}
42+
43+
public TransportChannel[] MLAPI_CHANNELS
44+
{
45+
get
46+
{
47+
if (_channelsCache == null)
48+
{
49+
List<TransportChannel> channels = new List<TransportChannel>();
50+
51+
OnChannelRegistration(channels);
52+
53+
_channelsCache = new TransportChannel[MLAPI_INTERNAL_CHANNELS.Length + channels.Count];
54+
55+
for (int i = 0; i < MLAPI_INTERNAL_CHANNELS.Length; i++)
56+
{
57+
_channelsCache[i] = MLAPI_INTERNAL_CHANNELS[i];
58+
}
59+
60+
for (int i = 0; i < channels.Count; i++)
61+
{
62+
_channelsCache[i + MLAPI_INTERNAL_CHANNELS.Length] = channels[i];
63+
}
64+
}
65+
66+
return _channelsCache;
67+
}
68+
}
69+
2570
/// <summary>
2671
/// The channels the MLAPI will use when sending internal messages.
2772
/// </summary>
28-
public static TransportChannel[] MLAPI_CHANNELS = new TransportChannel[]
73+
private TransportChannel[] MLAPI_INTERNAL_CHANNELS = new TransportChannel[]
2974
{
3075
new TransportChannel()
3176
{

0 commit comments

Comments
 (0)