|
| 1 | +using LiteNetLib; |
| 2 | +using LiteNetLib.Utils; |
| 3 | +using MLAPI.MonoBehaviours.Core; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace MLAPI.Data.Transports.LiteNetLib |
| 7 | +{ |
| 8 | + public class LiteNetLibTransport : IUDPTransport, INetEventListener |
| 9 | + { |
| 10 | + public ChannelType InternalChannel => ChannelType.ReliableFragmentedSequenced; |
| 11 | + |
| 12 | + public uint ServerNetId => (uint)serverPeer.ConnectId; |
| 13 | + |
| 14 | + public uint HostDummyId => uint.MaxValue - 1; |
| 15 | + |
| 16 | + public uint InvalidDummyId => uint.MaxValue - 2; |
| 17 | + |
| 18 | + private NetManager man = null; |
| 19 | + private class LiteNetLibEvent |
| 20 | + { |
| 21 | + public NetEventType eventType; |
| 22 | + public NetPeer peer; |
| 23 | + public byte[] data; |
| 24 | + } |
| 25 | + private Queue<LiteNetLibEvent> pendingEvents = new Queue<LiteNetLibEvent>(); |
| 26 | + private Dictionary<uint, NetPeer> connectedPeers = new Dictionary<uint, NetPeer>(); |
| 27 | + private Dictionary<int, SendOptions> channels = new Dictionary<int, SendOptions>(); |
| 28 | + private int channelCounter = 0; |
| 29 | + private NetPeer serverPeer; |
| 30 | + |
| 31 | + public int AddChannel(ChannelType type, object settings) |
| 32 | + { |
| 33 | + SendOptions options = SendOptions.ReliableUnordered; |
| 34 | + switch (type) |
| 35 | + { |
| 36 | + case ChannelType.Unreliable: |
| 37 | + options = SendOptions.Unreliable; |
| 38 | + break; |
| 39 | + case ChannelType.UnreliableFragmented: |
| 40 | + options = SendOptions.Unreliable; |
| 41 | + break; |
| 42 | + case ChannelType.UnreliableSequenced: |
| 43 | + options = SendOptions.Sequenced; |
| 44 | + break; |
| 45 | + case ChannelType.Reliable: |
| 46 | + options = SendOptions.ReliableUnordered; |
| 47 | + break; |
| 48 | + case ChannelType.ReliableFragmented: |
| 49 | + options = SendOptions.ReliableUnordered; |
| 50 | + break; |
| 51 | + case ChannelType.ReliableSequenced: |
| 52 | + options = SendOptions.ReliableOrdered; |
| 53 | + break; |
| 54 | + case ChannelType.StateUpdate: |
| 55 | + options = SendOptions.Unreliable; |
| 56 | + break; |
| 57 | + case ChannelType.ReliableStateUpdate: |
| 58 | + options = SendOptions.ReliableUnordered; |
| 59 | + break; |
| 60 | + case ChannelType.AllCostDelivery: |
| 61 | + options = SendOptions.ReliableOrdered; |
| 62 | + break; |
| 63 | + case ChannelType.UnreliableFragmentedSequenced: |
| 64 | + options = SendOptions.Unreliable; |
| 65 | + break; |
| 66 | + case ChannelType.ReliableFragmentedSequenced: |
| 67 | + options = SendOptions.ReliableOrdered; |
| 68 | + break; |
| 69 | + default: |
| 70 | + options = SendOptions.ReliableUnordered; |
| 71 | + break; |
| 72 | + } |
| 73 | + channels.Add(channelCounter, options); |
| 74 | + channelCounter++; |
| 75 | + return channelCounter - 1; |
| 76 | + } |
| 77 | + |
| 78 | + public void Connect(string address, int port, object settings, out byte error) |
| 79 | + { |
| 80 | + man = new NetManager(this, string.Empty); |
| 81 | + man.Start(); |
| 82 | + serverPeer = man.Connect(NetworkingManager.singleton.NetworkConfig.ConnectAddress, NetworkingManager.singleton.NetworkConfig.ConnectPort); |
| 83 | + error = 0; |
| 84 | + } |
| 85 | + |
| 86 | + public void DisconnectClient(uint clientId) |
| 87 | + { |
| 88 | + man.DisconnectPeer(connectedPeers[clientId]); |
| 89 | + } |
| 90 | + |
| 91 | + public void DisconnectFromServer() |
| 92 | + { |
| 93 | + man.DisconnectPeer(serverPeer); |
| 94 | + } |
| 95 | + |
| 96 | + public int GetCurrentRTT(uint clientId, out byte error) |
| 97 | + { |
| 98 | + error = 0; |
| 99 | + return connectedPeers[clientId].Ping; |
| 100 | + } |
| 101 | + |
| 102 | + public int GetNetworkTimestamp() |
| 103 | + { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + public int GetRemoteDelayTimeMS(uint clientId, int remoteTimestamp, out byte error) |
| 108 | + { |
| 109 | + error = 0; |
| 110 | + return connectedPeers[clientId].Ping; |
| 111 | + } |
| 112 | + |
| 113 | + public object GetSettings() |
| 114 | + { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + public NetEventType PollReceive(out uint clientId, out int channelId, ref byte[] data, int bufferSize, out int receivedSize, out byte error) |
| 119 | + { |
| 120 | + error = 0; |
| 121 | + man.PollEvents(); |
| 122 | + if (pendingEvents.Count == 0) |
| 123 | + { |
| 124 | + receivedSize = 0; |
| 125 | + channelId = 0; |
| 126 | + clientId = 0; |
| 127 | + return NetEventType.Nothing; |
| 128 | + } |
| 129 | + LiteNetLibEvent evnt = pendingEvents.Dequeue(); |
| 130 | + switch (evnt.eventType) |
| 131 | + { |
| 132 | + case NetEventType.Data: |
| 133 | + clientId = (uint)evnt.peer.ConnectId; |
| 134 | + receivedSize = evnt.data.Length; |
| 135 | + data = evnt.data; |
| 136 | + channelId = 0; |
| 137 | + return NetEventType.Data; |
| 138 | + case NetEventType.Connect: |
| 139 | + clientId = (uint)evnt.peer.ConnectId; |
| 140 | + connectedPeers.Add(clientId, evnt.peer); |
| 141 | + channelId = 0; |
| 142 | + receivedSize = 0; |
| 143 | + return NetEventType.Connect; |
| 144 | + case NetEventType.Disconnect: |
| 145 | + clientId = (uint)evnt.peer.ConnectId; |
| 146 | + connectedPeers.Remove(clientId); |
| 147 | + channelId = 0; |
| 148 | + receivedSize = 0; |
| 149 | + return NetEventType.Disconnect; |
| 150 | + case NetEventType.Nothing: |
| 151 | + receivedSize = 0; |
| 152 | + channelId = 0; |
| 153 | + clientId = 0; |
| 154 | + return NetEventType.Nothing; |
| 155 | + default: |
| 156 | + receivedSize = 0; |
| 157 | + channelId = 0; |
| 158 | + clientId = 0; |
| 159 | + return NetEventType.Nothing; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public void QueueMessageForSending(uint clientId, ref byte[] dataBuffer, int dataSize, int channelId, bool skipQueue, out byte error) |
| 164 | + { |
| 165 | + error = 0; |
| 166 | + connectedPeers[clientId].Send(dataBuffer, 0, dataSize, channels[channelId]); |
| 167 | + } |
| 168 | + |
| 169 | + public void RegisterServerListenSocket(object settings) |
| 170 | + { |
| 171 | + man = new NetManager(this, NetworkingManager.singleton.NetworkConfig.MaxConnections, string.Empty); |
| 172 | + man.Start(NetworkingManager.singleton.NetworkConfig.ConnectPort); |
| 173 | + } |
| 174 | + |
| 175 | + public void SendQueue(uint clientId, out byte error) |
| 176 | + { |
| 177 | + error = 0; |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + public void Shutdown() |
| 182 | + { |
| 183 | + man.Stop(); |
| 184 | + man = null; |
| 185 | + } |
| 186 | + |
| 187 | + //*LITE NET LIB EVENTS BELOW*// |
| 188 | + |
| 189 | + public void OnPeerConnected(NetPeer peer) |
| 190 | + { |
| 191 | + pendingEvents.Enqueue(new LiteNetLibEvent() |
| 192 | + { |
| 193 | + eventType = NetEventType.Connect, |
| 194 | + peer = peer |
| 195 | + }); |
| 196 | + } |
| 197 | + |
| 198 | + public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) |
| 199 | + { |
| 200 | + pendingEvents.Enqueue(new LiteNetLibEvent() |
| 201 | + { |
| 202 | + eventType = NetEventType.Disconnect, |
| 203 | + peer = peer |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode) |
| 208 | + { |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + public void OnNetworkReceive(NetPeer peer, NetDataReader reader) |
| 213 | + { |
| 214 | + pendingEvents.Enqueue(new LiteNetLibEvent() |
| 215 | + { |
| 216 | + eventType = NetEventType.Data, |
| 217 | + peer = peer, |
| 218 | + data = reader.Data |
| 219 | + }); |
| 220 | + } |
| 221 | + |
| 222 | + public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType) |
| 223 | + { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + public void OnNetworkLatencyUpdate(NetPeer peer, int latency) |
| 228 | + { |
| 229 | + return; |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments