-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathTransport.cs
More file actions
191 lines (161 loc) · 7.85 KB
/
Transport.cs
File metadata and controls
191 lines (161 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using MLAPI.Transports.Tasks;
using UnityEngine;
namespace MLAPI.Transports
{
/// <summary>
/// A network transport
/// </summary>
public abstract class Transport : MonoBehaviour
{
/// <summary>
/// Delegate used to request channels on the underlying transport.
/// </summary>
public delegate void RequestChannelsDelegate(List<TransportChannel> channels);
/// <summary>
/// Delegate called when the transport wants to know what channels to register.
/// </summary>
public event RequestChannelsDelegate OnChannelRegistration;
/// <summary>
/// A constant clientId that represents the server.
/// When this value is found in methods such as Send, it should be treated as a placeholder that means "the server"
/// </summary>
public abstract ulong ServerClientId { get; }
/// <summary>
/// Gets a value indicating whether this <see cref="T:MLAPI.Transports.Transport"/> is supported in the current runtime context.
/// This is used by multiplex adapters.
/// </summary>
/// <value><c>true</c> if is supported; otherwise, <c>false</c>.</value>
public virtual bool IsSupported => true;
private TransportChannel[] _channelsCache = null;
internal void ResetChannelCache()
{
_channelsCache = null;
}
public TransportChannel[] MLAPI_CHANNELS
{
get
{
if (_channelsCache == null)
{
List<TransportChannel> channels = new List<TransportChannel>();
if (OnChannelRegistration != null)
{
OnChannelRegistration(channels);
}
_channelsCache = new TransportChannel[MLAPI_INTERNAL_CHANNELS.Length + channels.Count];
for (int i = 0; i < MLAPI_INTERNAL_CHANNELS.Length; i++)
{
_channelsCache[i] = MLAPI_INTERNAL_CHANNELS[i];
}
for (int i = 0; i < channels.Count; i++)
{
_channelsCache[i + MLAPI_INTERNAL_CHANNELS.Length] = channels[i];
}
}
return _channelsCache;
}
}
public const byte MLAPI_INTERNAL_CHANNEL = 0;
public const byte MLAPI_STDRPC_CHANNEL = 1;
public const byte MLAPI_TIME_SYNC_CHANNEL = 2;
public static string GetChannelString(byte channel)
{
string channelName = "";
if (!TransportChannel.ChannelByteToString.TryGetValue(channel, out channelName))
{
channelName = "MLAPI_INTERNAL";
}
return channelName;
}
public static byte GetChannelByte(string channelName)
{
byte channel = MLAPI_INTERNAL_CHANNEL;
TransportChannel.ChannelStringToByte.TryGetValue(channelName, out channel);
return channel;
}
/// <summary>
/// The channels the MLAPI will use when sending internal messages.
/// </summary>
private TransportChannel[] MLAPI_INTERNAL_CHANNELS = new TransportChannel[]
{
new TransportChannel("MLAPI_INTERNAL", ChannelType.ReliableFragmentedSequenced, MLAPI_INTERNAL_CHANNEL),
new TransportChannel("STDRPC", ChannelType.ReliableSequenced, MLAPI_STDRPC_CHANNEL),
new TransportChannel("MLAPI_TIME_SYNC", ChannelType.Unreliable, MLAPI_TIME_SYNC_CHANNEL),
new TransportChannel("MLAPI_DEFAULT_MESSAGE", ChannelType.Reliable, 3),
new TransportChannel("MLAPI_POSITION_UPDATE", ChannelType.UnreliableSequenced, 4),
new TransportChannel("MLAPI_ANIMATION_UPDATE", ChannelType.ReliableSequenced,5 ),
new TransportChannel("MLAPI_NAV_AGENT_STATE", ChannelType.ReliableSequenced, 6),
new TransportChannel("MLAPI_NAV_AGENT_CORRECTION", ChannelType.UnreliableSequenced, 7),
};
/// <summary>
/// Delegate for transport events.
/// </summary>
public delegate void TransportEventDelegate(NetEventType type, ulong clientId, byte channel, ArraySegment<byte> payload, float receiveTime);
/// <summary>
/// Occurs when the transport has a new transport event. Can be used to make an event based transport instead of a poll based.
/// Invokation has to occur on the Unity thread in the Update loop.
/// </summary>
public event TransportEventDelegate OnTransportEvent;
/// <summary>
/// Invokes the <see cref="OnTransportEvent"/>. Invokation has to occur on the Unity thread in the Update loop.
/// </summary>
/// <param name="type">The event type</param>
/// <param name="clientId">The clientId this event is for</param>
/// <param name="channelName">The channel the data arrived at. This is usually used when responding to things like RPCs</param>
/// <param name="payload">The incoming data payload</param>
/// <param name="receiveTime">The time the event was received, as reported by Time.realtimeSinceStartup.</param>
protected void InvokeOnTransportEvent(NetEventType type, ulong clientId, byte channel, ArraySegment<byte> payload, float receiveTime)
{
OnTransportEvent?.Invoke(type, clientId, channel, payload, receiveTime);
}
/// <summary>
/// Send a payload to the specified clientId, data and channelName.
/// </summary>
/// <param name="clientId">The clientId to send to</param>
/// <param name="data">The data to send</param>
/// <param name="channelName">The channel to send data to</param>
public abstract void Send(ulong clientId, ArraySegment<byte> data, byte channel);
/// <summary>
/// Polls for incoming events, with an extra output parameter to report the precise time the event was received.
/// </summary>
/// <param name="clientId">The clientId this event is for</param>
/// <param name="channelName">The channel the data arrived at. This is usually used when responding to things like RPCs</param>
/// <param name="payload">The incoming data payload</param>
/// <param name="receiveTime">The time the event was received, as reported by Time.realtimeSinceStartup.</param>
/// <returns>Returns the event type</returns>
public abstract NetEventType PollEvent(out ulong clientId, out byte channel, out ArraySegment<byte> payload, out float receiveTime);
/// <summary>
/// Connects client to server
/// </summary>
public abstract SocketTasks StartClient();
/// <summary>
/// Starts to listen for incoming clients.
/// </summary>
public abstract SocketTasks StartServer();
/// <summary>
/// Disconnects a client from the server
/// </summary>
/// <param name="clientId">The clientId to disconnect</param>
public abstract void DisconnectRemoteClient(ulong clientId);
/// <summary>
/// Disconnects the local client from the server
/// </summary>
public abstract void DisconnectLocalClient();
/// <summary>
/// Gets the round trip time for a specific client. This method is optional
/// </summary>
/// <param name="clientId">The clientId to get the rtt from</param>
/// <returns>Returns the round trip time in milliseconds </returns>
public abstract ulong GetCurrentRtt(ulong clientId);
/// <summary>
/// Shuts down the transport
/// </summary>
public abstract void Shutdown();
/// <summary>
/// Initializes the transport
/// </summary>
public abstract void Init();
}
}