Ejemplo n.º 1
0
        /// <summary>
        /// Connect to the NAT hole punch server, this is called from a client machine
        /// </summary>
        /// <param name="host">The host address of the server the client is trying to connect to</param>
        /// <param name="port">The port number of the server the client is trying to connect to</param>
        /// <param name="clientPort">The port number that the client is listening on</param>
        /// <param name="natServer">The NAT server host address to connect to</param>
        /// <param name="natPort">The NAT server port number to connect to</param>
        public void Connect(string host, ushort port, ushort clientPort, string natServer,
                            ushort natPort = DEFAULT_NAT_SERVER_PORT)
        {
            // Don't allow multiple NAT server connection requests at once
            if (Client != null)
            {
                return;
            }

            // Connect to the NAT server
            Client = new UDPClient();
            Client.Connect(natServer, natPort, pendCreates: true);

            NetWorker.BaseNetworkEvent accepted = (NetWorker sender) => {
                // Send the data to the nat server with the host address and port that this client
                // is trying to connect to so that it can punch a hole in the network for this client
                JSONNode sendJson = JSONNode.Parse("{}");
                sendJson.Add("host", new JSONData(host));
                sendJson.Add("port", new JSONData(port));
                sendJson.Add("clientPort", new JSONData(clientPort));

                // Send the message to the NAT server
                Text connect = Text.CreateFromString(Client.Time.Timestep, sendJson.ToString(), false, Receivers.Server,
                                                     MessageGroupIds.NAT_SERVER_CONNECT, false);
                Client.Send(connect, true);
                Client.messageConfirmed += (player, packet) => {
                    if (packet.uniqueId == connect.UniqueId)
                    {
                        Client.Disconnect(false);
                    }
                };
            };

            Client.serverAccepted += accepted;
        }
        protected static void ConnectTeardown(NetworkObject.NetworkObjectEvent objectCreatedCallback = null)
        {
            if (objectCreatedCallback != null)
            {
                client.objectCreated -= objectCreatedCallback;
                server.objectCreated -= objectCreatedCallback;
            }

            if (otherClient != null)
            {
                if (objectCreatedCallback != null)
                {
                    otherClient.objectCreated -= objectCreatedCallback;
                }

                otherClient.Disconnect(false);
            }

            client.Disconnect(false);
            server.Disconnect(false);

            WaitFor(() => { return(!client.IsBound && !server.IsBound && (otherClient == null || !otherClient.IsBound)); });

            server      = null;
            client      = null;
            otherClient = null;
        }
        void RunUDPClient(string hostAddress, bool sendRandomData)
        {
            UDPClient client = new UDPClient(hostAddress, this);

            client.Start();
            client.Send("Hello World");

            if (sendRandomData)
            {
                int waitTimeMilliseconds = 100;

                // call a generater in a different thread to send lots of numbers
                // at specified intervals
                Thread randomNumbersThread = new Thread(() =>
                                                        SendRandomNumbers(ref client, waitTimeMilliseconds));
                randomNumbersThread.Start();

                //if the user presses the key start trying to stop sending data
                Console.ReadKey();
                Console.WriteLine("Preparing to stop sending data");

                // stop the loop
                this.endFunction = false;

                // wait for a while to make sure your not stoping the thread some were important
                Thread.Sleep(waitTimeMilliseconds * 3); // 3 times the wait time should be more than enough
            }

            // Disconnect the client
            client.Disconnect();
            Console.WriteLine("Program Stoped Sending Data");
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Disconnects from the remote host.
 /// </summary>
 public UdpClientHelper Disconnect()
 {
     udpClient.Disconnect();
     udpClient.MessageReceived       -= udpClient_MessageReceived;
     udpClient.ConnectedStateChanged -= udpClient_ConnectedStateChanged;
     return(this);
 }
Ejemplo n.º 5
0
        public void Disconnect()
        {
            Debug.Assert(peerId != 0);
            if (!Connected)
            {
                return;
            }

            cli.Disconnect(peerId);
        }
Ejemplo n.º 6
0
        public void ConnectTest()
        {
            client = new UDPClient();

            Console.WriteLine("Using port number: " + currentPort);
            client.Connect("127.0.0.1", currentPort);
            Assert.IsTrue(client.IsBound);
            WaitFor(() => { return(client.IsConnected); });

            client.Disconnect(false);

            WaitFor(() => { return(!client.IsConnected); });
            Assert.IsFalse(client.IsBound);
        }
        /// <summary>
        ///
        /// </summary>
        void RunUDPDemoToms()
        {
            UDPClient client = new UDPClient(this);

            UDPListener listener = new UDPListener(NetworkingLibaryStandard.NetworkingLibaryStandard.DefaultPortNumber + 1, this);

            listener.Start();

            client.Start();
            client.Send("Hello World");

            Console.ReadKey();

            client.Disconnect();
            listener.Stop();
        }
Ejemplo n.º 8
0
        public void MultipleReconnectTest()
        {
            UDPClient singleClient;
            int       i, clientCount = 4;

            for (i = 0; i < clientCount; i++)
            {
                singleClient = new UDPClient();
                singleClient.Connect("127.0.0.1", currentPort);
                Assert.IsTrue(singleClient.IsBound);
                WaitFor(() => { return(singleClient.IsConnected); });
                singleClient.Disconnect(false);
                WaitFor(() => { return(!singleClient.IsConnected); });
                Assert.IsFalse(singleClient.IsBound);
                singleClient = null;
                WaitFor(() => { return(server.Players.Count == 1); });
            }
        }
Ejemplo n.º 9
0
    public void Disconnect()
    {
        client.Disconnect(true);

        _logger.Log(LogTag.Network, new[] { Time.time.ToString(), "Disconnect server" });
    }
Ejemplo n.º 10
0
 public void Connect_Disconnect()
 {
     client.Connect();
     client.Disconnect();
 }
Ejemplo n.º 11
0
		public async void UDPTryDisconnect()
		{
			udpClient.Disconnect();
		}