AddHost() private method

private AddHost ( HostTopology topology ) : int
topology HostTopology
return int
Ejemplo n.º 1
0
        public override void StartServer()
        {
            if (NetworkManager.Get().enableLogging)
            {
                Debug.Log("Transport Start Server");
            }
            HostTopology topology = new HostTopology(GetConfig(), MaxConnections);

            if (SupportWebsocket)
            {
                int websocketHostID = Unet.AddWebsocketHost(topology, ServerWebsocketListenPort);
            }

            int normalHostID = Unet.AddHost(topology, ServerListenPort);
        }
Ejemplo n.º 2
0
        public override void StartClient()
        {
            if (NetworkManager.Get().enableLogging)
            {
                Debug.Log("Transport Start Client");
            }
            //SocketTask task = SocketTask.Working;

            serverHostID       = Unet.AddHost(new HostTopology(GetConfig(), 1));
            serverConnectionID = Unet.Connect(serverHostID, address, port, 0, out byte error);

            NetworkError connectError = (NetworkError)error;

            if (connectError != NetworkError.Ok)
            {
                //Throw Start Client failed exception
                throw new NetworkException("Unet failed to start client with error '" + connectError + "'.");
            }
        }
Ejemplo n.º 3
0
 private void PrepareForConnect()
 {
     NetworkClient.SetActive(true);
     this.RegisterSystemHandlers(false);
     if (this.m_HostTopology == null)
     {
         ConnectionConfig connectionConfig = new ConnectionConfig();
         connectionConfig.AddChannel(QosType.Reliable);
         connectionConfig.AddChannel(QosType.Unreliable);
         this.m_HostTopology = new HostTopology(connectionConfig, 8);
     }
     if (this.m_UseSimulator)
     {
         int num = this.m_SimulatedLatency / 3 - 1;
         if (num < 1)
         {
             num = 1;
         }
         int num2 = this.m_SimulatedLatency * 3;
         if (LogFilter.logDebug)
         {
             Debug.Log(string.Concat(new object[]
             {
                 "AddHost Using Simulator ",
                 num,
                 "/",
                 num2
             }));
         }
         this.m_ClientId = NetworkTransport.AddHostWithSimulator(this.m_HostTopology, num, num2, 0);
     }
     else
     {
         this.m_ClientId = NetworkTransport.AddHost(this.m_HostTopology, 0);
     }
 }
Ejemplo n.º 4
0
        public static int AddHost(HostTopology topology, int port)
        {
            string ip = (string)null;

            return(NetworkTransport.AddHost(topology, port, ip));
        }
Ejemplo n.º 5
0
        public bool ReconnectToNewHost(EndPoint secureTunnelEndPoint)
        {
            bool result;

            if (!NetworkClient.active)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - NetworkClient must be active");
                }
                result = false;
            }
            else if (this.m_Connection == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - no old connection exists");
                }
                result = false;
            }
            else
            {
                if (LogFilter.logInfo)
                {
                    Debug.Log("NetworkClient Reconnect to remoteSockAddr");
                }
                ClientScene.HandleClientDisconnect(this.m_Connection);
                ClientScene.ClearLocalPlayers();
                this.m_Connection.Disconnect();
                this.m_Connection = null;
                this.m_ClientId   = NetworkTransport.AddHost(this.m_HostTopology, this.m_HostPort);
                if (secureTunnelEndPoint == null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Reconnect failed: null endpoint passed in");
                    }
                    this.m_AsyncConnect = NetworkClient.ConnectState.Failed;
                    result = false;
                }
                else if (secureTunnelEndPoint.AddressFamily != AddressFamily.InterNetwork && secureTunnelEndPoint.AddressFamily != AddressFamily.InterNetworkV6)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Reconnect failed: Endpoint AddressFamily must be either InterNetwork or InterNetworkV6");
                    }
                    this.m_AsyncConnect = NetworkClient.ConnectState.Failed;
                    result = false;
                }
                else
                {
                    string fullName = secureTunnelEndPoint.GetType().FullName;
                    if (fullName == "System.Net.IPEndPoint")
                    {
                        IPEndPoint ipendPoint = (IPEndPoint)secureTunnelEndPoint;
                        this.Connect(ipendPoint.Address.ToString(), ipendPoint.Port);
                        result = (this.m_AsyncConnect != NetworkClient.ConnectState.Failed);
                    }
                    else if (fullName != "UnityEngine.XboxOne.XboxOneEndPoint" && fullName != "UnityEngine.PS4.SceEndPoint")
                    {
                        if (LogFilter.logError)
                        {
                            Debug.LogError("Reconnect failed: invalid Endpoint (not IPEndPoint or XboxOneEndPoint or SceEndPoint)");
                        }
                        this.m_AsyncConnect = NetworkClient.ConnectState.Failed;
                        result = false;
                    }
                    else
                    {
                        byte b = 0;
                        this.m_RemoteEndPoint = secureTunnelEndPoint;
                        this.m_AsyncConnect   = NetworkClient.ConnectState.Connecting;
                        try
                        {
                            this.m_ClientConnectionId = NetworkTransport.ConnectEndPoint(this.m_ClientId, this.m_RemoteEndPoint, 0, out b);
                        }
                        catch (Exception arg)
                        {
                            if (LogFilter.logError)
                            {
                                Debug.LogError("Reconnect failed: Exception when trying to connect to EndPoint: " + arg);
                            }
                            this.m_AsyncConnect = NetworkClient.ConnectState.Failed;
                            return(false);
                        }
                        if (this.m_ClientConnectionId == 0)
                        {
                            if (LogFilter.logError)
                            {
                                Debug.LogError("Reconnect failed: Unable to connect to EndPoint (" + b + ")");
                            }
                            this.m_AsyncConnect = NetworkClient.ConnectState.Failed;
                            result = false;
                        }
                        else
                        {
                            this.m_Connection = (NetworkConnection)Activator.CreateInstance(this.m_NetworkConnectionClass);
                            this.m_Connection.SetHandlers(this.m_MessageHandlers);
                            this.m_Connection.Initialize(this.m_ServerIp, this.m_ClientId, this.m_ClientConnectionId, this.m_HostTopology);
                            result = true;
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 6
0
        public bool ReconnectToNewHost(string serverIp, int serverPort)
        {
            bool result;

            if (!NetworkClient.active)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - NetworkClient must be active");
                }
                result = false;
            }
            else if (this.m_Connection == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - no old connection exists");
                }
                result = false;
            }
            else
            {
                if (LogFilter.logInfo)
                {
                    Debug.Log(string.Concat(new object[]
                    {
                        "NetworkClient Reconnect ",
                        serverIp,
                        ":",
                        serverPort
                    }));
                }
                ClientScene.HandleClientDisconnect(this.m_Connection);
                ClientScene.ClearLocalPlayers();
                this.m_Connection.Disconnect();
                this.m_Connection = null;
                this.m_ClientId   = NetworkTransport.AddHost(this.m_HostTopology, this.m_HostPort);
                this.m_ServerPort = serverPort;
                if (Application.platform == RuntimePlatform.WebGLPlayer)
                {
                    this.m_ServerIp     = serverIp;
                    this.m_AsyncConnect = NetworkClient.ConnectState.Resolved;
                }
                else if (serverIp.Equals("127.0.0.1") || serverIp.Equals("localhost"))
                {
                    this.m_ServerIp     = "127.0.0.1";
                    this.m_AsyncConnect = NetworkClient.ConnectState.Resolved;
                }
                else
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("Async DNS START:" + serverIp);
                    }
                    this.m_AsyncConnect = NetworkClient.ConnectState.Resolving;
                    Dns.BeginGetHostAddresses(serverIp, new AsyncCallback(NetworkClient.GetHostAddressesCallback), this);
                }
                result = true;
            }
            return(result);
        }
Ejemplo n.º 7
0
        public bool ReconnectToNewHost(EndPoint secureTunnelEndPoint)
        {
            if (!NetworkClient.active)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - NetworkClient must be active");
                }
                return(false);
            }

            if (m_Connection == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - no old connection exists");
                }
                return(false);
            }

            if (LogFilter.logInfo)
            {
                Debug.Log("NetworkClient Reconnect to remoteSockAddr");
            }

            ClientScene.HandleClientDisconnect(m_Connection);
            ClientScene.ClearLocalPlayers();

            m_Connection.Disconnect();
            m_Connection = null;
            m_ClientId   = NetworkTransport.AddHost(m_HostTopology, 0);

            if (secureTunnelEndPoint == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect failed: null endpoint passed in");
                }
                m_AsyncConnect = ConnectState.Failed;
                return(false);
            }

            // Make sure it's either IPv4 or IPv6
            if (secureTunnelEndPoint.AddressFamily != AddressFamily.InterNetwork && secureTunnelEndPoint.AddressFamily != AddressFamily.InterNetworkV6)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect failed: Endpoint AddressFamily must be either InterNetwork or InterNetworkV6");
                }
                m_AsyncConnect = ConnectState.Failed;
                return(false);
            }

            // Make sure it's an Endpoint we know what to do with
            string endPointType = secureTunnelEndPoint.GetType().FullName;

            if (endPointType == "System.Net.IPEndPoint")
            {
                IPEndPoint tmp = (IPEndPoint)secureTunnelEndPoint;
                Connect(tmp.Address.ToString(), tmp.Port);
                return(m_AsyncConnect != ConnectState.Failed);
            }
            if ((endPointType != "UnityEngine.XboxOne.XboxOneEndPoint") && (endPointType != "UnityEngine.PS4.SceEndPoint"))
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect failed: invalid Endpoint (not IPEndPoint or XboxOneEndPoint or SceEndPoint)");
                }
                m_AsyncConnect = ConnectState.Failed;
                return(false);
            }

            byte error = 0;

            // regular non-relay connect
            m_RemoteEndPoint = secureTunnelEndPoint;
            m_AsyncConnect   = ConnectState.Connecting;

            try
            {
                m_ClientConnectionId = NetworkTransport.ConnectEndPoint(m_ClientId, m_RemoteEndPoint, 0, out error);
            }
            catch (Exception ex)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect failed: Exception when trying to connect to EndPoint: " + ex);
                }
                m_AsyncConnect = ConnectState.Failed;
                return(false);
            }
            if (m_ClientConnectionId == 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect failed: Unable to connect to EndPoint (" + error + ")");
                }
                m_AsyncConnect = ConnectState.Failed;
                return(false);
            }

            m_Connection = (NetworkConnection)Activator.CreateInstance(m_NetworkConnectionClass);
            m_Connection.SetHandlers(m_MessageHandlers);
            m_Connection.Initialize(m_ServerIp, m_ClientId, m_ClientConnectionId, m_HostTopology);
            return(true);
        }
Ejemplo n.º 8
0
        public bool ReconnectToNewHost(string serverIp, int serverPort)
        {
            if (!NetworkClient.active)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - NetworkClient must be active");
                }
                return(false);
            }

            if (m_Connection == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Reconnect - no old connection exists");
                }
                return(false);
            }

            if (LogFilter.logInfo)
            {
                Debug.Log("NetworkClient Reconnect " + serverIp + ":" + serverPort);
            }

            ClientScene.HandleClientDisconnect(m_Connection);
            ClientScene.ClearLocalPlayers();

            m_Connection.Disconnect();
            m_Connection = null;
            m_ClientId   = NetworkTransport.AddHost(m_HostTopology, 0);

            string hostnameOrIp = serverIp;

            m_ServerPort = serverPort;

            //TODO: relay reconnect

            /*
             * if (Match.NetworkMatch.matchSingleton != null)
             * {
             *  hostnameOrIp = Match.NetworkMatch.matchSingleton.address;
             *  m_ServerPort = Match.NetworkMatch.matchSingleton.port;
             * }*/

            if (UnityEngine.Application.platform == RuntimePlatform.WebGLPlayer)
            {
                m_ServerIp     = hostnameOrIp;
                m_AsyncConnect = ConnectState.Resolved;
            }
            else if (serverIp.Equals("127.0.0.1") || serverIp.Equals("localhost"))
            {
                m_ServerIp     = "127.0.0.1";
                m_AsyncConnect = ConnectState.Resolved;
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log("Async DNS START:" + hostnameOrIp);
                }
                m_AsyncConnect = ConnectState.Resolving;
                Dns.BeginGetHostAddresses(hostnameOrIp, new AsyncCallback(GetHostAddressesCallback), this);
            }
            return(true);
        }
Ejemplo n.º 9
0
        void PrepareForConnect(bool usePlatformSpecificProtocols)
        {
            SetActive(true);
            RegisterSystemHandlers(false);

            if (m_HostTopology == null)
            {
                var config = new ConnectionConfig();
                config.AddChannel(QosType.ReliableSequenced);
                config.AddChannel(QosType.Unreliable);
                config.UsePlatformSpecificProtocols = usePlatformSpecificProtocols;
                m_HostTopology = new HostTopology(config, 8);
            }

            if (m_UseSimulator)
            {
                int minTimeout = (m_SimulatedLatency / 3) - 1;
                if (minTimeout < 1)
                {
                    minTimeout = 1;
                }
                int maxTimeout = m_SimulatedLatency * 3;

                if (LogFilter.logDebug)
                {
                    Debug.Log("AddHost Using Simulator " + minTimeout + "/" + maxTimeout);
                }
                m_ClientId = m_UseRelay ? RelayTransport.AddHostWithSimulator(m_HostTopology, minTimeout, maxTimeout, m_HostPort, false, RelayTransport.RelayAddress, RelayTransport.RelayPort) : NetworkTransport.AddHostWithSimulator(m_HostTopology, minTimeout, maxTimeout, m_HostPort);
            }
            else
            {
                m_ClientId = m_UseRelay ? RelayTransport.AddHost(m_HostTopology, m_HostPort, false, RelayTransport.RelayAddress, RelayTransport.RelayPort) :  NetworkTransport.AddHost(m_HostTopology, m_HostPort);
            }
        }
Ejemplo n.º 10
0
        public bool Listen(int serverListenPort, HostTopology topology)
        {
            m_HostTopology = topology;
            Initialize();
            m_ListenPort = serverListenPort;

            if (m_UseWebSockets)
            {
                m_ServerHostId = m_UseRelay ? RelayTransport.AddWebsocketHost(m_HostTopology, serverListenPort, true) : NetworkTransport.AddWebsocketHost(m_HostTopology, serverListenPort);
            }
            else
            {
                m_ServerHostId = m_UseRelay ? RelayTransport.AddWebsocketHost(m_HostTopology, serverListenPort, true) : NetworkTransport.AddHost(m_HostTopology, serverListenPort);
            }

            if (m_ServerHostId == -1)
            {
                return(false);
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkServerSimple listen " + m_ListenPort);
            }
            return(true);
        }
Ejemplo n.º 11
0
 public int AddHost(HostTopology topology, int port, string ip)
 {
     return(NetworkTransport.AddHost(topology, port, ip));
 }
 public static int AddHost(HostTopology topology)
 {
     return(NetworkTransport.AddHost(topology, 0, null));
 }
Ejemplo n.º 13
0
        public bool Listen(string ipAddress, int serverListenPort)
        {
            Initialize();
            m_ListenPort = serverListenPort;

            if (m_UseWebSockets)
            {
                m_ServerHostId = m_UseRelay ? RelayTransport.AddWebsocketHost(m_HostTopology, serverListenPort, ipAddress, true, RelayTransport.RelayAddress, RelayTransport.RelayPort) : NetworkTransport.AddWebsocketHost(m_HostTopology, serverListenPort, ipAddress);
            }
            else
            {
                m_ServerHostId = m_UseRelay ? RelayTransport.AddHost(m_HostTopology, serverListenPort, ipAddress, true, RelayTransport.RelayAddress, RelayTransport.RelayPort) : NetworkTransport.AddHost(m_HostTopology, serverListenPort, ipAddress);
            }

            if (m_ServerHostId == -1)
            {
                return(false);
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkServerSimple listen: " + ipAddress + ":" + m_ListenPort);
            }
            return(true);
        }