internal static void ConnectClient(string code) { try { if (LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.Discovery) { LiteNetLib4MirrorCore.StopTransport(); } EventBasedNetListener listener = new EventBasedNetListener(); LiteNetLib4MirrorCore.Host = new NetManager(listener); listener.NetworkReceiveEvent += OnNetworkReceive; listener.NetworkErrorEvent += OnNetworkError; listener.PeerConnectedEvent += OnPeerConnected; listener.PeerDisconnectedEvent += OnPeerDisconnected; LiteNetLib4MirrorCore.SetOptions(false); LiteNetLib4MirrorCore.Host.Start(); LiteNetLib4MirrorCore.Host.Connect(LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.clientAddress, LiteNetLib4MirrorTransport.Singleton.port), code); LiteNetLib4MirrorTransport.Polling = true; LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.ClientConnecting; } catch (Exception ex) { LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; LiteNetLib4MirrorUtils.LogException(ex); } }
internal static void OnDiscoveryRequest(IPEndPoint remoteendpoint, NetPacketReader reader, UnconnectedMessageType messagetype) { if (messagetype == UnconnectedMessageType.Broadcast && reader.TryGetString(out string application) && application == Application.productName && Singleton.ProcessDiscoveryRequest(remoteendpoint, LiteNetLib4MirrorUtils.FromBase64(reader.GetString()), out string response)) { LiteNetLib4MirrorCore.Host.SendUnconnectedMessage(LiteNetLib4MirrorUtils.ReusePutDiscovery(DataWriter, response, ref _lastDiscoveryMessage), remoteendpoint); } reader.Recycle(); }
private static void OnDiscoveryResponse(IPEndPoint remoteendpoint, NetPacketReader reader, UnconnectedMessageType messagetype) { if (messagetype == UnconnectedMessageType.BasicMessage && reader.TryGetString(out string application) && application == Application.productName) { Singleton.onDiscoveryResponse.Invoke(remoteendpoint, LiteNetLib4MirrorUtils.FromBase64(reader.GetString())); } reader.Recycle(); }
private static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo) { if (disconnectinfo.AdditionalData.TryGetString(out string reason)) { LastDisconnectReason = LiteNetLib4MirrorUtils.FromBase64(reason); } LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; LiteNetLib4MirrorCore.LastDisconnectError = disconnectinfo.SocketErrorCode; LiteNetLib4MirrorCore.LastDisconnectReason = disconnectinfo.Reason; LiteNetLib4MirrorTransport.Singleton.OnClientDisconnected.Invoke(); LiteNetLib4MirrorCore.StopTransport(); }
public static void SendDiscoveryRequest(string text) { if (LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.Discovery) { LiteNetLib4MirrorUtils.ReusePutDiscovery(DataWriter, text, ref _lastDiscoveryMessage); foreach (ushort port in Singleton.ports) { LiteNetLib4MirrorCore.Host.SendBroadcast(DataWriter, port); } } }
internal static void StartServer(string code) { try { Code = code; EventBasedNetListener listener = new EventBasedNetListener(); LiteNetLib4MirrorCore.Host = new NetManager(listener); listener.ConnectionRequestEvent += OnConnectionRequest; listener.PeerDisconnectedEvent += OnPeerDisconnected; listener.NetworkErrorEvent += OnNetworkError; listener.NetworkReceiveEvent += OnNetworkReceive; listener.PeerConnectedEvent += OnPeerConnected; if (LiteNetLib4MirrorDiscovery.Singleton != null) { listener.NetworkReceiveUnconnectedEvent += LiteNetLib4MirrorDiscovery.OnDiscoveryRequest; } LiteNetLib4MirrorCore.SetOptions(true); if (LiteNetLib4MirrorTransport.Singleton.useUpnP) { LiteNetLib4MirrorUtils.ForwardPort(); } IPAddress serverIPv4BindAddress = LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.serverIPv4BindAddress); if (LiteNetLib4MirrorTransport.Singleton.forceLocalAddressForUpnP) { IPAddress oldAddress = serverIPv4BindAddress; serverIPv4BindAddress = Task.Run(() => LiteNetLib4MirrorUtils.GetLocalIP()).GetAwaiter().GetResult(); Debug.Log("Overriding the set IPv4 address (" + oldAddress + ") with the machine's local address (" + serverIPv4BindAddress + ")"); } #if DISABLE_IPV6 LiteNetLib4MirrorCore.Host.Start(serverIPv4BindAddress, IPAddress.IPv6None, LiteNetLib4MirrorTransport.Singleton.port); #else LiteNetLib4MirrorCore.Host.Start(serverIPv4BindAddress, LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.serverIPv6BindAddress), LiteNetLib4MirrorTransport.Singleton.port); #endif Peers = new NetPeer[LiteNetLib4MirrorTransport.Singleton.maxConnections * 2]; LiteNetLib4MirrorTransport.Polling = true; LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Server; } catch (Exception ex) { LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; Debug.LogException(ex); } }
public static void SendDiscoveryRequest(string text) { if (LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.Discovery) { LiteNetLib4MirrorUtils.ReusePutDiscovery(DataWriter, text, ref _lastDiscoveryMessage); LiteNetLib4MirrorCore.Host.SendBroadcast(DataWriter, Singleton.port); if (Singleton.port > 2365) { Singleton.port = 2345; } else { Singleton.port++; } } }
internal static bool Disconnect(int connectionId) { try { if (DisconnectMessage == null) { Peers[connectionId].Disconnect(); } else { Peers[connectionId].Disconnect(LiteNetLib4MirrorUtils.ReusePut(Writer, DisconnectMessage, ref _lastMessage)); } return(true); } catch { return(false); } }
internal static void StartServer(string code) { try { Code = code; EventBasedNetListener listener = new EventBasedNetListener(); LiteNetLib4MirrorCore.Host = new NetManager(listener); listener.ConnectionRequestEvent += OnConnectionRequest; listener.PeerDisconnectedEvent += OnPeerDisconnected; listener.NetworkErrorEvent += OnNetworkError; listener.NetworkReceiveEvent += OnNetworkReceive; listener.PeerConnectedEvent += OnPeerConnected; if (LiteNetLib4MirrorDiscovery.Singleton != null) { listener.NetworkReceiveUnconnectedEvent += LiteNetLib4MirrorDiscovery.OnDiscoveryRequest; } LiteNetLib4MirrorCore.SetOptions(true); if (LiteNetLib4MirrorTransport.Singleton.useUpnP) { LiteNetLib4MirrorUtils.ForwardPort(); } #if DISABLE_IPV6 LiteNetLib4MirrorCore.Host.Start(LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.serverIPv4BindAddress), IPAddress.IPv6None, LiteNetLib4MirrorTransport.Singleton.port); #else LiteNetLib4MirrorCore.Host.Start(LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.serverIPv4BindAddress), LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.serverIPv6BindAddress), LiteNetLib4MirrorTransport.Singleton.port); #endif Peers = new NetPeer[LiteNetLib4MirrorTransport.Singleton.maxConnections * 2]; LiteNetLib4MirrorTransport.Polling = true; LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Server; } catch (Exception ex) { LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; Debug.LogException(ex); } }
private static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo) { switch (disconnectinfo.Reason) { case DisconnectReason.ConnectionRejected: LiteNetLib4MirrorTransport.Singleton.OnConncetionRefused(disconnectinfo); LastDisconnectReason = null; break; case DisconnectReason.DisconnectPeerCalled when disconnectinfo.AdditionalData.TryGetString(out string reason) && !string.IsNullOrWhiteSpace(reason): LastDisconnectReason = LiteNetLib4MirrorUtils.FromBase64(reason); break; default: LastDisconnectReason = null; break; } LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; LiteNetLib4MirrorCore.LastDisconnectError = disconnectinfo.SocketErrorCode; LiteNetLib4MirrorCore.LastDisconnectReason = disconnectinfo.Reason; LiteNetLib4MirrorTransport.Singleton.OnClientDisconnected.Invoke(); LiteNetLib4MirrorCore.StopTransport(); }
protected internal virtual string GenerateCode() { return(Convert.ToBase64String(Encoding.UTF8.GetBytes(LiteNetLib4MirrorUtils.Concatenate(Application.productName, Application.companyName, Application.unityVersion, LiteNetLib4MirrorCore.TransportVersion, Singleton.authCode)))); }
private static string GetConnectKey() { return(LiteNetLib4MirrorUtils.ToBase64(Application.productName + Application.companyName + Application.unityVersion + LiteNetLib4MirrorCore.TransportVersion + Singleton.authCode)); }
protected internal virtual string GenerateCode() { return(LiteNetLib4MirrorUtils.ToBase64(Application.productName + Application.companyName + Application.unityVersion + LiteNetLib4MirrorCore.TransportVersion + Singleton.authCode)); }
private static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo) { if (disconnectinfo.AdditionalData.TryGetString(out string reason) && !string.IsNullOrWhiteSpace(reason)) { LastDisconnectReason = LiteNetLib4MirrorUtils.FromBase64(reason); }