Пример #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);
            }
        }
 public override void ClientDisconnect()
 {
     if (!LiteNetLib4MirrorServer.IsActive())
     {
         LiteNetLib4MirrorCore.StopTransport();
     }
 }
Пример #3
0
 public static void StopDiscovery()
 {
     if (LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.Discovery)
     {
         LiteNetLib4MirrorCore.StopTransport();
     }
 }
Пример #4
0
 private static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo)
 {
     LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle;
     LiteNetLib4MirrorCore.LastDisconnectError  = disconnectinfo.SocketErrorCode;
     LiteNetLib4MirrorCore.LastDisconnectReason = disconnectinfo.Reason;
     LiteNetLib4MirrorTransport.Singleton.OnClientDisconnected.Invoke();
     LiteNetLib4MirrorCore.StopTransport();
 }
 private void OnDestroy()
 {
     LiteNetLib4MirrorCore.StopTransport();
     if (LiteNetLib4MirrorUtils.LastForwardedPort != 0)
     {
         NatDiscoverer.ReleaseAll();
         LiteNetLib4MirrorUtils.LastForwardedPort = 0;
     }
 }
Пример #6
0
 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();
 }
Пример #7
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);
            }
        }
Пример #8
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);
            }
        }
Пример #9
0
        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();
        }
 public override string ToString()
 {
     return(LiteNetLib4MirrorCore.GetState());
 }
 public override int GetMaxPacketSize(int channelId = Channels.DefaultReliable)
 {
     return(LiteNetLib4MirrorCore.GetMaxPacketSize(channels[channelId]));
 }
 public override void Shutdown()
 {
     LiteNetLib4MirrorCore.StopTransport();
 }
 public override void ServerStop()
 {
     LiteNetLib4MirrorCore.StopTransport();
 }