Exemplo n.º 1
0
        /// <summary>
        /// Start up the network manager. This should be called by Lobby.cs
        /// when the server host IP has been discovered and is connectable.
        /// </summary>
        ///
        /// <param name="serverAddr">
        /// As server, local IP address to host on.
        /// As client, remote server IP address to connect to.
        /// </param>
        ///
        /// <param name="nwMode">
        /// Are you the server or client?
        /// </param>
        ///
        /// <param name="useDNS">
        /// If serverAddr is a local IP, convert it via DNS? Set to false if using wifi-direct IP address.
        /// </param>
        ///
        /// <returns>
        /// True if successful.
        /// If client, use this.NetworkHost.ConnectionCount == 1 afterwards to confirm connection to server.
        /// </returns>
        public bool Run(string serverAddr, NetworkMode nwMode, bool useDNS = true)
        {
            ServerAddress = serverAddr;
            Mode          = nwMode;

            // Construct serializer and packet queue
            Serializer  = new XMLPacketSerializer();
            PacketQueue = new PacketQueue(Serializer);

            // Create and configure host
            if (Mode == NetworkMode.Server)
            {
                // Convert any "local address" to the "dns local address"
                if (useDNS && NetworkHost.IsLocalAddress(ServerAddress))
                {
                    ServerAddress = NetworkHost.GetLocalAddress();
                }

                // Server, configures socket to be "known" at this address
                Debug.Log("NetworkManager: As a server, I am using server IP: " + ServerAddress);
                NetworkHost = new NetworkHost(ServerAddress, ServerPort, 4);
            }
            else
            {
                // Client, connect to server
                NetworkHost = new NetworkHost(4);
            }

            // Configure channels and open/allow connections.
            NetworkHost.ConfigureChannel("info", ChannelType.ReliableSequenced);
            NetworkHost.ConfigureChannel("state", ChannelType.UnreliableStateUpdate);
            NetworkHost.ConfigureChannel("input", ChannelType.ReliableStateUpdate);
            bool rv = NetworkHost.Open();

            if (!rv)
            {
                NetworkHost.Dispose();
                NetworkHost = null;
                return(false);
            }

            // Connect client to server
            if (Mode == NetworkMode.Client)
            {
                Debug.Log("NetworkManager: As a client, I am using server IP: " + ServerAddress);
                NetworkHost.Connect(ServerAddress, ServerPort);
            }

            // Allow FixedUpdate() to run.
            IsRunning = true;

            return(true);
        }
Exemplo n.º 2
0
        public void TestNetworkHostConnect()
        {
            NetworkHost host = new NetworkHost();
            TcpClient   client;

            try
            {
                client = host.Connect("192.168.0.105");
                Assert.IsType <TcpClient>(client);
            }
            catch (Exception ex)
            {
                Assert.IsType <Exception>(ex);
            }
        }