Esempio n. 1
0
        /// <summary>
        /// Registers a server to the NAT server so that it can be requested to be joined by clients
        /// </summary>
        /// <param name="currentPort">The current port that this server is listening for client connections 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 Register(ushort currentPort, 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);

            // When connected, request for this server to be registered to the NAT lookup for clients
            NetWorker.BaseNetworkEvent accepted = (NetWorker sender) => {
                JSONNode obj = JSONNode.Parse("{}");
                obj.Add("port", new JSONData(currentPort));

                JSONClass sendJson = new JSONClass();
                sendJson.Add("register", obj);

                // Send the message to the NAT server
                Text register = Text.CreateFromString(Client.Time.Timestep, sendJson.ToString(), false,
                                                      Receivers.Target, MessageGroupIds.NAT_SERVER_REGISTER, false);
                Client.Send(register, true);
            };

            Client.serverAccepted += accepted;

            // Setup the callback events for when clients attempt to join
            Client.textMessageReceived += PlayerConnectRequestReceived;
        }
Esempio n. 2
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;
        }