Exemplo n.º 1
0
 private void NewPlayer(BeardedManStudios.Forge.Networking.NetworkingPlayer player, BeardedManStudios.Forge.Networking.NetWorker sender)
 {
     MainThreadManager.Run(() =>
     {
         PlayerControllerBehavior go = NetworkManager.Instance.InstantiatePlayerController(0, position: Vector3.zero + UnityEngine.Random.insideUnitSphere * 10);
         go.networkObject.position   = go.transform.position;
         go.networkObject.ID         = player.NetworkId;
     });
 }
Exemplo n.º 2
0
 private void NewPlayer(BeardedManStudios.Forge.Networking.NetworkingPlayer player, BeardedManStudios.Forge.Networking.NetWorker sender)
 {
     MainThreadManager.Run(() =>
     {
         MovableObjectBehavior go  = NetworkManager.Instance.InstantiateMovableObject(0, position: networkObject.SpawnPos2);
         go.networkObject.playerID = player.NetworkId;
         go.networkObject.position = networkObject.SpawnPos2;
         networkObject.BothSpawned = true;
     });
 }
        public bool PlayerIsReceiver(NetworkingPlayer player, FrameStream frame, float proximityDistance, NetworkingPlayer skipPlayer = null, int proximityModeUpdateFrequency = 0)
        {
            // Don't send messages to a player who has not been accepted by the server yet
            if ((!player.Accepted && !player.PendingAccepted) || player == skipPlayer)
            {
                return(false);
            }

            if (player == frame.Sender)
            {
                // Don't send a message to the sending player if it was meant for others
                if (frame.Receivers == Receivers.Others || frame.Receivers == Receivers.OthersBuffered || frame.Receivers == Receivers.OthersProximity || frame.Receivers == Receivers.OthersProximityGrid)
                {
                    return(false);
                }
            }

            // check if sender is null as it doesn't get sent in certain cases
            if (frame.Sender != null)
            {
                PlayerIsDistanceReceiver(frame.Sender, player, frame, proximityDistance, proximityModeUpdateFrequency);
            }
            return(true);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Fully remove the player from the network
 /// </summary>
 /// <param name="player">The target player</param>
 /// <param name="forced">If the player is being forcibly removed from an exception</param>
 private void RemovePlayer(NetworkingPlayer player, bool forced)
 {
     Disconnect(player.TcpClientHandle, forced);
     OnPlayerDisconnected(player);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Pong back to the client
 /// </summary>
 /// <param name="playerRequesting"></param>
 protected override void Pong(NetworkingPlayer playerRequesting, DateTime time)
 {
     Send(playerRequesting, GeneratePong(time));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Callback for user auth. Sends an auth challenge to the user.
 /// </summary>
 private void IssueChallenge(NetworkingPlayer player, BMSByte buffer)
 {
     Send(player,
          new Binary(Time.Timestep, false, buffer, Receivers.Target, MessageGroupIds.AUTHENTICATION_CHALLENGE,
                     false), true);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Disconnects a client
 /// </summary>
 /// <param name="client">The target client to be disconnected</param>
 public void Disconnect(NetworkingPlayer player, bool forced)
 {
     commonServerLogic.Disconnect(player, forced, DisconnectingPlayers, ForcedDisconnectingPlayers);
 }
Exemplo n.º 8
0
        /// <summary>
        /// This will connect a UDP client to a given UDP server
        /// </summary>
        /// <param name="host">The server's host address on the network</param>
        /// <param name="port">The port that the server is hosting on</param>
        /// <param name="natHost">The NAT server host address, if blank NAT will be skipped</param>
        /// <param name="natPort">The port that the NAT server is hosting on</param>
        /// <param name="pendCreates">Immidiately set the NetWorker::PendCreates to true</param>
        public void Connect(string host, ushort port = DEFAULT_PORT, string natHost = "", ushort natPort = NatHolePunch.DEFAULT_NAT_SERVER_PORT, bool pendCreates = false, ushort overrideBindingPort = DEFAULT_PORT + 1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("UDPClient", "This object has been disposed and can not be used to connect, please use a new UDPClient");
            }

            // By default pending creates should be true and flushed when ready
            if (!pendCreates)
            {
                PendCreates = true;
            }

            try
            {
                ushort clientPort = overrideBindingPort;

                // Make sure not to listen on the same port as the server for local networks
                if (clientPort == port)
                {
                    clientPort++;
                }

                for (; ; clientPort++)
                {
                    try
                    {
                        IPEndPoint[] activeEndPoints = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
                        bool         alreadyinuse    = activeEndPoints.Any(p => p.Port == clientPort);
                        if (!alreadyinuse)
                        {
                            Client = new CachedUdpClient(clientPort);
                            break;
                        }
                    }
                    catch
                    {
                        if (port == 0)
                        {
                            throw new BaseNetworkException("There were no ports available starting from port " + port);
                        }
                    }
                }

                Client.EnableBroadcast = true;

                // If the server is behind a NAT, request for the port punch by the nat server
                if (!string.IsNullOrEmpty(natHost))
                {
                    nat.Connect(host, port, clientPort, natHost, natPort);
                }

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

                // Get a random hash key that needs to be used for validating that the server was connected to
                headerHash = Websockets.HeaderHashKey();

                // This is a typical Websockets accept header to be validated
                byte[] connectHeader = Websockets.ConnectionHeader(headerHash, port);

                try
                {
                    // Setup the identity of the server as a player
                    server = new NetworkingPlayer(0, host, true, ResolveHost(host, port), this);
                }
                catch (ArgumentException)
                {
                    if (connectAttemptFailed != null)
                    {
                        connectAttemptFailed(this);
                    }

                    throw;
                }

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

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

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

                //Set the port
                SetPort(clientPort);

                int connectCounter = 0;
                Task.Queue(() =>
                {
                    do
                    {
                        // Send the accept headers to the server to validate
                        Client.Send(connectHeader, connectHeader.Length, Server.IPEndPointHandle);
                        Thread.Sleep(3000);
                    } while (!headerExchanged && IsBound && ++connectCounter < CONNECT_TRIES);

                    if (connectCounter >= CONNECT_TRIES)
                    {
                        if (connectAttemptFailed != null)
                        {
                            connectAttemptFailed(this);
                        }
                    }
                });
            }
            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);
            }
        }