Exemple #1
0
        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);
            }
        }
Exemple #2
0
        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);
            }
        }
Exemple #3
0
        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);
            }
        }