-
Notifications
You must be signed in to change notification settings - Fork 430
/
LiteNetLibTransport.cs
234 lines (209 loc) · 7.89 KB
/
LiteNetLibTransport.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using LiteNetLib;
using LiteNetLib.Utils;
using MLAPI.MonoBehaviours.Core;
using System.Collections.Generic;
namespace MLAPI.Data.Transports.LiteNetLib
{
public class LiteNetLibTransport : IUDPTransport, INetEventListener
{
public ChannelType InternalChannel => ChannelType.ReliableFragmentedSequenced;
public uint ServerNetId => (uint)serverPeer.ConnectId;
public uint HostDummyId => uint.MaxValue - 1;
public uint InvalidDummyId => uint.MaxValue - 2;
private NetManager man = null;
private class LiteNetLibEvent
{
public NetEventType eventType;
public NetPeer peer;
public byte[] data;
}
private Queue<LiteNetLibEvent> pendingEvents = new Queue<LiteNetLibEvent>();
private Dictionary<uint, NetPeer> connectedPeers = new Dictionary<uint, NetPeer>();
private Dictionary<int, SendOptions> channels = new Dictionary<int, SendOptions>();
private int channelCounter = 0;
private NetPeer serverPeer;
public int AddChannel(ChannelType type, object settings)
{
SendOptions options = SendOptions.ReliableUnordered;
switch (type)
{
case ChannelType.Unreliable:
options = SendOptions.Unreliable;
break;
case ChannelType.UnreliableFragmented:
options = SendOptions.Unreliable;
break;
case ChannelType.UnreliableSequenced:
options = SendOptions.Sequenced;
break;
case ChannelType.Reliable:
options = SendOptions.ReliableUnordered;
break;
case ChannelType.ReliableFragmented:
options = SendOptions.ReliableUnordered;
break;
case ChannelType.ReliableSequenced:
options = SendOptions.ReliableOrdered;
break;
case ChannelType.StateUpdate:
options = SendOptions.Unreliable;
break;
case ChannelType.ReliableStateUpdate:
options = SendOptions.ReliableUnordered;
break;
case ChannelType.AllCostDelivery:
options = SendOptions.ReliableOrdered;
break;
case ChannelType.UnreliableFragmentedSequenced:
options = SendOptions.Unreliable;
break;
case ChannelType.ReliableFragmentedSequenced:
options = SendOptions.ReliableOrdered;
break;
default:
options = SendOptions.ReliableUnordered;
break;
}
channels.Add(channelCounter, options);
channelCounter++;
return channelCounter - 1;
}
public void Connect(string address, int port, object settings, out byte error)
{
man = new NetManager(this, string.Empty);
man.Start();
serverPeer = man.Connect(NetworkingManager.singleton.NetworkConfig.ConnectAddress, NetworkingManager.singleton.NetworkConfig.ConnectPort);
error = 0;
}
public void DisconnectClient(uint clientId)
{
man.DisconnectPeer(connectedPeers[clientId]);
}
public void DisconnectFromServer()
{
man.DisconnectPeer(serverPeer);
}
public int GetCurrentRTT(uint clientId, out byte error)
{
error = 0;
return connectedPeers[clientId].Ping;
}
public int GetNetworkTimestamp()
{
return 0;
}
public int GetRemoteDelayTimeMS(uint clientId, int remoteTimestamp, out byte error)
{
error = 0;
return connectedPeers[clientId].Ping;
}
public object GetSettings()
{
return null;
}
public NetEventType PollReceive(out uint clientId, out int channelId, ref byte[] data, int bufferSize, out int receivedSize, out byte error)
{
error = 0;
man.PollEvents();
if (pendingEvents.Count == 0)
{
receivedSize = 0;
channelId = 0;
clientId = 0;
return NetEventType.Nothing;
}
LiteNetLibEvent evnt = pendingEvents.Dequeue();
switch (evnt.eventType)
{
case NetEventType.Data:
clientId = (uint)evnt.peer.ConnectId;
receivedSize = evnt.data.Length;
data = evnt.data;
channelId = 0;
return NetEventType.Data;
case NetEventType.Connect:
clientId = (uint)evnt.peer.ConnectId;
connectedPeers.Add(clientId, evnt.peer);
channelId = 0;
receivedSize = 0;
return NetEventType.Connect;
case NetEventType.Disconnect:
clientId = (uint)evnt.peer.ConnectId;
connectedPeers.Remove(clientId);
channelId = 0;
receivedSize = 0;
return NetEventType.Disconnect;
case NetEventType.Nothing:
receivedSize = 0;
channelId = 0;
clientId = 0;
return NetEventType.Nothing;
default:
receivedSize = 0;
channelId = 0;
clientId = 0;
return NetEventType.Nothing;
}
}
public void QueueMessageForSending(uint clientId, ref byte[] dataBuffer, int dataSize, int channelId, bool skipQueue, out byte error)
{
error = 0;
connectedPeers[clientId].Send(dataBuffer, 0, dataSize, channels[channelId]);
}
public void RegisterServerListenSocket(object settings)
{
man = new NetManager(this, NetworkingManager.singleton.NetworkConfig.MaxConnections, string.Empty);
man.Start(NetworkingManager.singleton.NetworkConfig.ConnectPort);
}
public void SendQueue(uint clientId, out byte error)
{
error = 0;
return;
}
public void Shutdown()
{
man.Stop();
man = null;
}
//*LITE NET LIB EVENTS BELOW*//
public void OnPeerConnected(NetPeer peer)
{
pendingEvents.Enqueue(new LiteNetLibEvent()
{
eventType = NetEventType.Connect,
peer = peer
});
}
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
pendingEvents.Enqueue(new LiteNetLibEvent()
{
eventType = NetEventType.Disconnect,
peer = peer
});
}
public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
{
return;
}
public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
{
pendingEvents.Enqueue(new LiteNetLibEvent()
{
eventType = NetEventType.Data,
peer = peer,
data = reader.Data
});
}
public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
{
return;
}
public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
return;
}
}
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member