예제 #1
0
        /// <summary>
        /// [Server only] Assigns the client authority.
        /// </summary>
        /// <param name="conn">The connection.</param>
        /// <returns><c>false</c> on error, true otherwise.</returns>
        public bool AssignClientAuthority(TinyNetConnection conn)
        {
            if (!isServer)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("AssignClientAuthority can only be call on the server for spawned objects.");
                }
                return(false);
            }
            if (!_localPlayerAuthority)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("AssignClientAuthority can only be used for NetworkIdentity component with LocalPlayerAuthority set.");
                }
                return(false);
            }

            if (_connectionToOwnerClient != null && conn != _connectionToOwnerClient)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("AssignClientAuthority for " + gameObject + " already has an owner. Use RemoveClientAuthority() first.");
                }
                return(false);
            }

            if (conn == null)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("AssignClientAuthority for " + gameObject + " owner cannot be null. Use RemoveClientAuthority() instead.");
                }
                return(false);
            }

            _connectionToOwnerClient = conn;
            _connectionToOwnerClient.AddOwnedObject(this);

            // server no longer has authority (this is called on server). Note that local client could re-acquire authority below
            ForceAuthority(false);

            OnGiveAuthority();

            // send msg to that client
            var msg = new TinyNetClientAuthorityMessage();

            msg.networkID = TinyInstanceID.NetworkID;
            msg.authority = true;
            TinyNetServer.instance.SendMessageByChannelToTargetConnection(msg, LiteNetLib.DeliveryMethod.ReliableOrdered, conn);

            //Saishy: Still don't have an authority callback

            /*if (clientAuthorityCallback != null) {
             *      clientAuthorityCallback(conn, this, true);
             * }*/
            return(true);
        }
예제 #2
0
        /// <summary>
        /// [Server only] Removes the client authority.
        /// </summary>
        /// <param name="conn">The connection.</param>
        /// <returns><c>false</c> on error, true otherwise.</returns>
        public bool RemoveClientAuthority(TinyNetConnection conn)
        {
            if (!isServer)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("RemoveClientAuthority can only be called on the server for spawned objects.");
                }
                return(false);
            }

            if (_connectionToOwnerClient == null)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("RemoveClientAuthority for " + gameObject + " has no clientAuthority owner.");
                }
                return(false);
            }

            if (_connectionToOwnerClient != conn)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("RemoveClientAuthority for " + gameObject + " has different owner.");
                }
                return(false);
            }

            _connectionToOwnerClient.RemoveOwnedObject(this);
            _connectionToOwnerClient = null;

            // server now has authority (this is only called on server)
            ForceAuthority(true);

            OnRemoveAuthority();

            // send msg to that client
            var msg = new TinyNetClientAuthorityMessage();

            msg.networkID = TinyInstanceID.NetworkID;
            msg.authority = false;
            TinyNetServer.instance.SendMessageByChannelToTargetConnection(msg, LiteNetLib.DeliveryMethod.ReliableOrdered, conn);

            //Saishy: Still don't have an authority callback

            /*if (clientAuthorityCallback != null) {
             *      clientAuthorityCallback(conn, this, false);
             * }*/
            return(true);
        }