public bool StartAsClient(int hostPort, string connectToIP, int connectToPort)
        {
            InitializeNetworking();

            HostTopology topology = new HostTopology(ConnectionConfig, 1);

            bool success   = false;
            int  tmpHostId = -1;

            tmpHostId = NetworkTransport.AddHost(topology, hostPort);
            if (tmpHostId >= 0)
            {
                byte networkError;
                int  connectionId = NetworkTransport.Connect(tmpHostId, connectToIP, connectToPort, 0, out networkError);
                if ((NetworkError)networkError == NetworkError.Ok)
                {
                    Mode   = OnlineSessionMode.Client;
                    HostId = tmpHostId;

                    success = true;
                }
            }

            if (!success)
            {
                Debug.LogError("StartAsClient failed");
            }

            return(success);
        }
Exemple #2
0
        public void StartConnect()
        {
            if (!m_IsInit)
            {
                // Инициализируем сеть
                NetworkTransport.Init();
                ConnectionConfig conConfig = new ConnectionConfig();

                m_ReliableChannel = conConfig.AddChannel(QosType.Reliable);
                //m_UnreliableChanel = conConfig.AddChannel (QosType.Unreliable);

                HostTopology hostTopo = new HostTopology(conConfig, m_MaxConnection);

                // Подключаемся к мастер серверу
                m_HostId = NetworkTransport.AddHost(hostTopo, 0);
                byte error;
                m_ConnectionId = NetworkTransport.Connect(m_HostId, m_IpAddress, m_Port, 0, out error);

                m_IsInit = true;
            }
            else
            {
                if (!m_IsConnect)
                {
                    byte error;
                    m_ConnectionId = NetworkTransport.Connect(m_HostId, m_IpAddress, m_Port, 0, out error);
                }
            }
        }
    public void Connect()
    {
        string pName = GameObject.Find("NameField").GetComponent <InputField> ().text;

        if (pName == "")
        {
            Debug.Log("Enter A name");
            return;
        }

        playerName = pName;

        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel   = cc.AddChannel(QosType.Reliable);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(cc, MAX_CONNECTION);

        hostId = NetworkTransport.AddHost(topo, 0);

        connectionId   = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
        connectionTime = Time.time;


        isConnected = true;
    }
Exemple #4
0
    public void Connect()
    {
        if (Server.serverIp == null || Server.serverIp.Length < 7)
        {
            return;
        }
        NetworkTransport.Init();

        ConnectionConfig cc = new ConnectionConfig();

        cc.AddChannel(QosType.Reliable);

        HostTopology topo = new HostTopology(cc, MAX_USER);

        //client only code
        hostId = NetworkTransport.AddHost(topo, 0);
#if UNITY_EDITOR || !UNITY_WEBGL
        //Stand alone client
        Debug.Log("connecting from standalone");
        connectionId = NetworkTransport.Connect(hostId, Server.serverIp, PORT, 0, out error);
#else
        //web client
        Debug.Log("connecting from web client");
        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, WEB_PORT, 0, out error);
#endif

        Debug.Log(string.Format("Attemping to connect on {0}...", Server.serverIp));

        isStarted = true;
        connected = true;
    }
    /// <summary>
    /// Initializes the network, creating a socket that will be used to communicate with the server,
    /// and two channels (reliable control channel, unreliable avatar movement channel.)
    /// </summary>
    void InitNetwork()
    {
        // Establish connection to server and get client id.
        NetworkTransport.Init();

        // Set up channels for control messages (reliable) and movement messages (unreliable)
        ConnectionConfig config = new ConnectionConfig();

        _controlChannelId = config.AddChannel(QosType.Reliable);
        _dataChannelId    = config.AddChannel(QosType.Unreliable);

        // Create socket end-point
        HostTopology topology = new HostTopology(config, _maxConnections);

        _hostId = NetworkTransport.AddHost(topology);

        // Establish connection to server
        byte error;

        _connectionId = NetworkTransport.Connect(_hostId, _serverHostIP, _serverPort, 0, out error);
        if (error != (byte)NetworkError.Ok)
        {
            Debug.LogFormat("Network error: {0}", Messages.NetworkErrorToString(error));
        }

        Debug.LogFormat("Establishing network connection with hostId {0}; connectionId {1}",
                        _hostId, _connectionId);
    }
Exemple #6
0
    public void Init()
    {
        NetworkTransport.Init();

        ConnectionConfig cc = new ConnectionConfig();

        cc.AddChannel(QosType.Reliable);

        HostTopology topo = new HostTopology(cc, MAX_USER);

        //클라이언트만 가진 코드
        hostId = NetworkTransport.AddHost(topo, 0);

#if UNITY_WEBGL && !UNITY_EDITOR
        //웹 클라이언트
        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, WEB_PORT, 0, out error);
        Debug.Log("Connection from WEBGL");
#else
        //독립 클라이언트
        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, PORT, 0, out error);
        Debug.Log("Connecting from standalone");
#endif
        Debug.Log(string.Format("Attemping to connect on {0}", SERVER_IP));
        isStarted = true;
    }
Exemple #7
0
    public void Connect()
    {
        //UI disabling is currently in spawnplayer()

        //Does the player have a name?
        string pName = GameObject.Find("NameInput").GetComponent <InputField>().text;

        if (pName == "")
        {
            Debug.Log("You must enter a name..");
            return;
        }

        playerName = pName;


        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel   = cc.AddChannel(QosType.Reliable);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(cc, MAX_CONNECTION);

        hostId       = NetworkTransport.AddHost(topo, 0);
        connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);         //Use ip: 127.0.0.1 for LOCALHOST for testing on this machine
        //Use ip: 2602:306:36d3:a450:8105:2819:bfc7:9dca for LAN testing
        //Use ip: 2620:9b::190f:1076 for hamachi

        connectionTime = Time.time;
        isConnected    = true;
    }
Exemple #8
0
    public static void StartClient()
    {
        if (isStarted)
        {
            Debug.LogError("Attempted to start when already running!");
            return;
        }
        NetworkTransport.Init();
        config        = new ConnectionConfig();
        allocChannels = new byte[channels.Count];
        for (int i = 0; i < channels.Count; i++)
        {
            allocChannels[i] = config.AddChannel(channels[i]);
        }
        topo   = new HostTopology(config, max_users);
        hostID = NetworkTransport.AddHost(topo, 0);
#if UNITY_WEBGL && !UNITY_EDITOR
        connectionID = NetworkTransport.Connect(hostID, ConnIP, web_port, 0, out error);
#else
        connectionID = NetworkTransport.Connect(hostID, ConnIP, port, 0, out error);
#endif
        if (error != 0)
        {
            Debug.LogError("Unable to connect, error: " + error);
        }
        else
        {
            isStarted     = true;
            hasRegistered = true;
            isHost        = false;
        }
    }
        public void Connect(string hostName, int port = -1)
        {
            byte error;

            NetworkTransport.Connect(m_HostId, hostName, port == -1 ? Port : port, 0, out error);
            LastError = error;
        }
    // Use this for initialization
    IEnumerator Start()
    {
        ConnectionConfig config = new ConnectionConfig();

        config.AddChannel(QosType.Unreliable);
        config.PacketSize = 1000;

        Debug.LogFormat("Default config has {0} channels", config.ChannelCount);
        HostTopology top = new HostTopology(config, 10);

        int serverHostId = NetworkTransport.AddHost(top);

        byte err = 0;

        Debug.LogFormat("Client connecting...");



        NetworkTransport.Connect(serverHostId, m_serverAddr, m_serverPort, 0, out err);

        StartCoroutine(SendReceive());
        StartCoroutine(SendTime());

        yield break;
    }
Exemple #11
0
    public void ConnectServer(string serverIp)
    {
        if (state == NetworkState.Server)
        {
            state = NetworkState.ClientAndServer;
        }
        else if (state == NetworkState.None)
        {
            state = NetworkState.Client;
        }
        else
        {
            return;
        }
        client = gameObject.AddComponent <Client>();

        //if(isStarted) hostId = NetworkTransport.AddHost(topo, port, null);
        //"192.168.0.175""127.0.0.1"
        Debug.Log("Connect: " + serverIp);
        //connectionTime = Time.time;
        //isConnected = true;
        //Debug.Log("asdfkljahsdfklja;lkfj;adlksfj;lk:"+ NetworkTransport.Connect(hostId, serverIp, port, 0, out error));



        client.Init(this, (int)NetworkTransport.Connect(NetworkTransport.AddHost(topo, 0, null), serverIp, port, 0, out error));
        isStarted = true;
    }
    public int Connect(string address, int port)
    {
        IPAddress[] ipAddresses;
        try
        {
            ipAddresses = Dns.GetHostAddresses(address);
        }
        catch (System.Exception e)
        {
            GameDebug.Log("Unable to resolve " + address + ". " + e.Message);
            return(0);
        }

        if (ipAddresses.Length < 1)
        {
            GameDebug.Log("Unable to resolve " + address + ". Host not found");
            return(0);
        }

        // TODO (petera) do we want to do round-robin?
        var ip = ipAddresses[0].ToString();

        byte error;

        if (UnityEngine.Debug.isDebugBuild && m_isNetworkSimuationActive)
        {
            var simulationConfig = new ConnectionSimulatorConfig(48, 50, 48, 50, 10);
            return(NetworkTransport.ConnectWithSimulator(m_HostId, ip, port, 0, out error, simulationConfig));
        }
        else
        {
            return(NetworkTransport.Connect(m_HostId, ip, port, 0, out error));
        }
    }
        public static void SetupClient(string ip, string port)
        {
            PlayerId = 1; // TEST set player id to 1 for client

            Camera.main.transform.localPosition = new Vector3(
                -Camera.main.transform.localPosition.x,
                Camera.main.transform.localPosition.y,
                -Camera.main.transform.localPosition.z
                );

            Camera.main.transform.localEulerAngles = new Vector3(
                Camera.main.transform.localEulerAngles.x,
                Camera.main.transform.localEulerAngles.y + 180,
                Camera.main.transform.localEulerAngles.z
                );

            if (OnMainCameraChange != null)
            {
                OnMainCameraChange();
            }

            connectionId = NetworkTransport.Connect(hostId, ip, Convert.ToInt32(port), 0, out error);

            if (error != (byte)NetworkError.Ok)
            {
                Debug.LogError("[ERROR] NetworkAPI :: SetupClient :: Connect : " + error);
            }
        }
Exemple #14
0
        public void Connect(string ip)
        {
            NetworkTransport.Init();

            // Config must be identical with the server
            var config = new ConnectionConfig();

            config.PacketSize = 1000;
            infoChannel       = config.AddChannel(QosType.ReliableSequenced);
            gameChannel       = config.AddChannel(QosType.ReliableSequenced);

            // Setup the host socket (yes also for clients)
            // Using port 0 to let the system assign a random available port
            var topology = new HostTopology(config, 200);

            server.host = NetworkTransport.AddHost(topology, 0);

            byte error;

            server.conn = NetworkTransport.Connect(server.host, ip, port, 0, out error);

            Log.TestError(error, "Failed to connect to the server!");

            // Setup the data buffer:
            data = new byte[config.PacketSize];
            // And setup a reader and writer for easy data manipulation
            stream = new MemoryStream(data);
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);
        }
Exemple #15
0
    public void Connect()
    {
        // Does the player has a name
        string playerNameInput = GameObject.Find("NameInput").GetComponent <InputField>().text;

        if (playerNameInput == "")
        {
            Debug.Log("You must enter a name");
            return;
        }

        var dropdown = GameObject.Find("RenderingMethodDropDown").GetComponent <Dropdown>();

        renderingMethod = dropdown.options[dropdown.value].text;

        playerName = playerNameInput;

        SetStatus("Connecting...");
        NetworkTransport.Init();
        ConnectionConfig connectionConfig = new ConnectionConfig();

        // Client and server need to have the same connection types
        reliableChannel   = connectionConfig.AddChannel(QosType.Reliable);
        unreliableChannel = connectionConfig.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(connectionConfig, MAX_CONNECTIONS);

        hostId       = NetworkTransport.AddHost(topo, 0);
        connectionId = NetworkTransport.Connect(hostId, server_ip, port, 0, out error);

        connectionTime = Time.time;
        isConnected    = true;
        SetStatus("Connected");
    }
Exemple #16
0
        protected virtual bool StartClient()
        {
            Initialize();

            NetworkIdentity[] identities = FindObjectsOfType <NetworkIdentity>();

            foreach (NetworkIdentity identity in identities)
            {
                Destroy(identity.gameObject);
            }

            m_hostID = NetworkTransport.AddHost(m_hostTopology);

            byte error;

            m_clientID = NetworkTransport.Connect(m_hostID, m_Address, m_Port, 0, out error);

            NetworkError networkError = (NetworkError)error;

            if (networkError != NetworkError.Ok)
            {
                Debug.LogError($"Fehler beim Erstellen des Clients! ({networkError})",
                               this);
                return(false);
            }
            m_isServer = false;
            return(true);
        }
Exemple #17
0
    public void Init()
    {
        NetworkTransport.Init();

        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);

        HostTopology topo = new HostTopology(cc, MAX_USER);

        // Client Only Code
        hostId = NetworkTransport.AddHost(topo, 0);

#if UNITY_WEBGL && !UNITY_EDITOR
        // Web Client
        NetworkTransport.Connect(hostId, SERVER_IP, WEB_PORT, 0, out error);
        Debug.Log("Connecting from Web");
#else
        // Standalone Client
        NetworkTransport.Connect(hostId, SERVER_IP, PORT, 0, out error);
        Debug.Log(string.Format("Connecting from standalone"));
#endif
        // webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);

        Debug.Log(string.Format("Openning connection on port {0} ...", SERVER_IP));
        isStarted = true;
    }
Exemple #18
0
        /// <summary>
        /// Connect this connection to remote host.
        /// </summary>
        public INetworkConnection Connect(IPAddress address, int port)
        {
            // Host must already open
            if (!IsOpen)
            {
                throw new InvalidOperationException("Unable to connect to remote host, local host not open.");
            }

            // Validate address and port
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (port < 0)
            {
                throw new ArgumentOutOfRangeException("port");
            }

            // Connect To Remote Host
            byte error;
            var  id = NetworkTransport.Connect(Id, address.ToString(), port, 0, out error);

            if (error > 0)
            {
                throw new NetworkException("Connect", error);
            }

            // Create new connection and store in connections list
            var connection = new NetworkConnection(this, id);

            _Connections.Add(connection);

            return(connection);
        }
Exemple #19
0
    public void Connect()
    {
        byte error;

        connectionId = NetworkTransport.Connect(socketId, remoteIp, socketPort, 0, out error);
        Debug.Log("Connected to server. ConnectionId: " + connectionId);
    }
Exemple #20
0
    public static void Connect()
    {
        byte error;

        connectionId = NetworkTransport.Connect(localHostId, serverIp, serverPort, 0, out error);
        Debug.Log("clent connect to " + serverIp + ": " + serverPort);
    }
Exemple #21
0
    // Creates a connection with the Zed 2 camera project
    public void Connect(String otherIP)
    {
        output.text = output.text + "starting";
        // Create the connection
        NetworkTransport.Init();
        ConnectionConfig config = new ConnectionConfig();

        myReliableChannelId = config.AddChannel(QosType.UnreliableSequenced);
        HostTopology topology = new HostTopology(config, maxConnections);

        socketId = NetworkTransport.AddHost(topology, 0);
        byte error;

        if (!localConnect)
        {
            connectionId = NetworkTransport.Connect(socketId, "192.168.43.42", socketPort, 0, out error);
        }
        else
        {
            connectionId = NetworkTransport.Connect(socketId, "127.0.0.1", socketPort, 0, out error);
        }

        // Report any errors
        if (error != null)
        {
            Debug.Log("Network Error : " + (NetworkError)error);
            output.text = output.text + "Network Error : " + (NetworkError)error;
        }
        output.text = output.text + " Connected to server. ConnectionId: " + connectionId;
        Debug.Log("Connected to server. ConnectionId: " + connectionId);
    }
Exemple #22
0
    public void Init(bool isServer)
    {
        byte error;

        host          = hostText.text;
        port          = Convert.ToInt32(portText.text.ToString());
        this.isServer = isServer;
        NetworkTransport.Init();
        config = new ConnectionConfig();
        config.AddChannel(QosType.Reliable);
        if (isServer)
        {
            topology = new HostTopology(config, 10);
            hostId   = NetworkTransport.AddHost(topology, port);
            Debug.Log("Server started on port" + port + " with id of " + hostId);
        }
        else
        {
            topology     = new HostTopology(config, 1);
            hostId       = NetworkTransport.AddHost(topology, 0);
            connectionId = NetworkTransport.Connect(hostId, host, port, 0, out error);
            NetworkError networkError = (NetworkError)error;
            if (networkError != NetworkError.Ok)
            {
                Debug.LogError(string.Format("Unable to connect to {0}:{1}, Error: {2}", host, port, networkError));
            }
            else
            {
                Debug.Log(string.Format("Connected to {0}:{1} with hostId: {2}, connectionId: {3}, channelId: {4},", host, port, hostId, connectionId, channelId));
            }
        }
        isInit = true;
    }
    public void Connect()
    {
        byte error;

        connectionID = NetworkTransport.Connect(socketID, "localhost", socketPort, 0, out error);
        Debug.Log("Connected to server. ConnectionId: " + connectionID);
    }
Exemple #24
0
        public static void ConnectToLobby(string lobbyIP)
        {
            /* if (Networking.IsBot())
             * {
             *   // Fake a connection to itself
             *   if (currentLobby == null)
             *   {
             *       Debug.LogError("Attempt to connect a bot to a server that isnt hosting a lobby");
             *   }
             * }*/
            ConnectionConfig config            = new ConnectionConfig();
            int          myReliableChannelId   = config.AddChannel(QosType.Reliable);
            int          myUnreliableChannelId = config.AddChannel(QosType.Unreliable);
            HostTopology topology = new HostTopology(config, 10);

            /* int portCount = 1;
             * while(hostId == -1 && portCount < 50)
             * {
             *  hostId = NetworkTransport.AddHost(topology, 8080 + (portCount++));
             * }*/
            hostId = NetworkTransport.AddHost(topology, 0);
            byte error;

            connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", 8080, 0, out error);
            Debug.Log("My connection  id is " + connectionId);
            if ((NetworkError)error != NetworkError.Ok)
            {
                //Output this message in the console with the Network Error
                Debug.Log("There was this error : " + (NetworkError)error);
            }
            else
            {
                Debug.Log("Success???");
            }
        }
    private IEnumerator InitializeNetwork()
    {
        serverIp = inputField.text;

        while (loadingScene)
        {
            yield return(new WaitForSeconds(0.1f));
        }

        Button button = GameObject.Find("Button").GetComponent <Button>();

        button.onClick.AddListener(delegate { Cancel(); });

        NetworkTransport.Init();

        ConnectionConfig config = new ConnectionConfig();

        reliableChannel = config.AddChannel(QosType.Unreliable);

        HostTopology toplogy = new HostTopology(config, MAX_CLIENTS);

        hostId = NetworkTransport.AddHost(toplogy, 0);

        connectionId = NetworkTransport.Connect(hostId, serverIp, PORT, 0, out error);

        Debug.Log(string.Format("Attempting to connect to {0} on port {1}.", serverIp, PORT));

        isStarted = true;
    }
Exemple #26
0
    public void Connect()
    {
        // Does the player have a name?
        string pName = GameObject.Find("NameInput").GetComponent <InputField>().text;

        if (pName == "")
        {
            Debug.Log("You must enter a name!");
            return;
        }

        playerName = pName;

        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel   = cc.AddChannel(QosType.Reliable);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(cc, MAX_CONNECTIONS);

        hostId = NetworkTransport.AddHost(topo, 0);
        // Local host IP is "127.0.0.1" or "LOCALHOST"
        connectionId = NetworkTransport.Connect(hostId, "142.160.105.52", port, 0, out error);

        connectionTime = Time.time;
        isConnected    = true;
    }
Exemple #27
0
    private IEnumerator waitThenReconnect(float waitTime, string remoteAddress, int remotePort)
    {
        timeUntilStartServer = 1000.0f;
        yield return(new WaitForSeconds(waitTime));

        while (clientSocket < 0 && port > 8870)   // limit to 16 players max
        {
            clientSocket = NetworkTransport.AddHost(topology, --port);
        }
        if (port <= 8870)   // just incase this happens
        {
            Debug.Log("CLIENT: no open ports, quiting");
            ResetToMenu.Reset();
            yield break;
        }
        Debug.Log("CLIENT: reconnected on port: " + port);
        byte error;

        serverSocket = NetworkTransport.Connect(clientSocket, remoteAddress, remotePort, 0, out error);

        // set up UI for login
        menuUI.setupLoginUI();

        // can delete server script now if not used
        if (!enabledServer)
        {
            Destroy(gameObject.GetComponent <GameServer>());
        }
    }
    public void Connect()
    {
        byte error;

        connectionId = NetworkTransport.Connect(socketId, "127.0.0.1", 33333, 0, out error);
        Debug.Log("Connected to server. ConnectionId: " + connectionId);
    }
Exemple #29
0
    public void Init()
    {
        NetworkTransport.Init();

        ConnectionConfig connectionConfig = new ConnectionConfig();

        this.reliableChannel = connectionConfig.AddChannel(QosType.Reliable);

        HostTopology topo = new HostTopology(connectionConfig, MAX_USER);

        // Client only code
        this.hostId = NetworkTransport.AddHost(topo, 0);

#if UNITY_WEBGL && !UNITY_EDITOR
        // Web Client
        NetworkTransport.Connect(hostId, SERVER_IP, WEB_PORT, 0, out error);
        Debug.Log("Connecting from Web");
#else
        // Standalone Client
        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, PORT, 0, out error);
        Debug.Log("Connecting from standalone");
#endif

        Debug.Log(string.Format("Attempting to connect on {0} ...", SERVER_IP));

        isStarted = true;
    }
Exemple #30
0
    //Start the client
    protected void Init(string server_ip)
    {
        if (IsStarted)
        {
            return;
        }

        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.ReliableFragmentedSequenced);
        HostTopology topo = new HostTopology(cc, MAX_USER);

        hostID       = NetworkTransport.AddHost(topo, 0);
        connectionID = NetworkTransport.Connect(hostID, server_ip, PORT, 0, out error);

        if (onMessageReceivedDelegates.ContainsKey(MessageType.Ack) == false)
        {
            AddMessageDelegate(MessageType.Ack, onAckMessageReceived);
        }
        if (onMessageSentDelegates.ContainsKey(MessageType.Ack) == false)
        {
            AddMessageDelegate(MessageType.Ack, onAckMessageSent);
        }
        IsStarted   = true;
        IsConnected = false;

        if (SHOW_LOGS)
        {
            string logMsg = String.Format("Connecting to {0}:{1}...", server_ip, PORT);
            Debug.Log(logMsg);
        }
    }