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); } }
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); } }