11using System ;
2+ using System . Collections . Generic ;
23using MLAPI . Transports . Tasks ;
34using 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