Exemplo n.º 1
0
        public void Connect(string host = "0.0.0.0", ushort port = DEFAULT_PORT, string natHost = "", ushort natPort = NatHolePunch.DEFAULT_NAT_SERVER_PORT)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("UDPServer", "This object has been disposed and can not be used to connect, please use a new UDPServer");
            }

            try
            {
                Client = new CachedUdpClient(port);
                Client.EnableBroadcast = true;
                Me = new NetworkingPlayer(ServerPlayerCounter++, host, true, HostResolver.Resolve(host, port), this);
                Me.InstanceGuid = InstanceGuid.ToString();

                // Do any generic initialization in result of the successful bind
                OnBindSuccessful();

                // Create the thread that will be listening for new data from connected clients and start its execution
                Task.Queue(ReadClients);

                // Create the thread that will check for player timeouts
                Task.Queue(() =>
                {
                    commonServerLogic.CheckClientTimeout((player) =>
                    {
                        Disconnect(player, true);
                        OnPlayerTimeout(player);
                        CleanupDisconnections();
                    });
                });

                // Let myself know I connected successfully
                OnPlayerConnected(Me);

                // Set myself as a connected client
                Me.Connected = true;

                //Set the port
                SetPort((ushort)((IPEndPoint)Client.Client.LocalEndPoint).Port);

                if (!string.IsNullOrEmpty(natHost))
                {
                    nat.Register((ushort)Me.IPEndPointHandle.Port, natHost, natPort);
                    nat.clientConnectAttempt += NatClientConnectAttempt;
                }
            }
            catch (Exception e)
            {
                Logging.BMSLog.LogException(e);
                // Do any generic initialization in result of the binding failure
                OnBindFailure();

                throw new FailedBindingException("Failed to bind to host/port, see inner exception", e);
            }
        }
Exemplo n.º 2
0
        private void CreateTheNetworkingPlayer(string host, ushort port)
        {
            try
            {
                // Setup the identity of the server as a player
                server = new NetworkingPlayer(0, host, true, HostResolver.Resolve(host, port), this);
            }
            catch (ArgumentException)
            {
                if (connectAttemptFailed != null)
                {
                    connectAttemptFailed(this);
                }

                throw;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// A callback from the NatHolePunch object saying that a client is trying to connect
        /// </summary>
        /// <param name="host">The host address of the client trying to connect</param>
        /// <param name="port">The port number to communicate with the client on</param>
        private void NatClientConnectAttempt(string host, ushort port)
        {
            IPEndPoint clientIPEndPoint;

            Logging.BMSLog.LogFormat("ATTEMPTING CONNECT ON {0} AND PORT IS {1}", host, port);

            try
            {
                clientIPEndPoint = HostResolver.Resolve(host, port);
            }
            catch (ArgumentException)
            {
                Logging.BMSLog.LogExceptionFormat("Unable to resolve client host {0}", host);
                // Do nothing as the client's host cannot be resolved.
                return;
            }

            Logging.BMSLog.LogFormat("RESOLVED IS {0} AND {1}", clientIPEndPoint.Address.ToString(), clientIPEndPoint.Port);

            // Punch a hole in the nat for this client
            Client.Send(new byte[1] {
                0
            }, 1, clientIPEndPoint);
        }