Пример #1
0
        /// <inheritdoc />
        protected override TinyNetConnection CreateTinyNetConnection(NetPeer peer)
        {
            TinyNetConnection tinyConn;

            if (((string)peer.Tag).Equals(TinyNetGameManager.ApplicationGUIDString))
            {
                tinyConn = new TinyNetLocalConnectionToClient(peer);
                if (TinyNetLogLevel.logDev)
                {
                    TinyLogger.Log("TinyNetServer::CreateTinyNetConnection created new TinyNetLocalConnectionToClient.");
                }
            }
            else
            {
                tinyConn = new TinyNetConnection(peer);
                if (TinyNetLogLevel.logDev)
                {
                    TinyLogger.Log("TinyNetServer::CreateTinyNetConnection created new TinyNetConnection.");
                }
            }

            peer.Tag = tinyConn;

            tinyNetConns.Add(tinyConn);

            return(tinyConn);
        }
Пример #2
0
        //============ Clients Functions ====================//

        /// <summary>
        /// Sets the client as ready.
        /// </summary>
        /// <param name="conn">The connection.</param>
        void SetClientReady(TinyNetConnection conn)
        {
            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("SetClientReady for conn:" + conn.ConnectId);
            }

            if (conn.isReady)
            {
                if (TinyNetLogLevel.logDebug)
                {
                    TinyLogger.Log("SetClientReady conn " + conn.ConnectId + " already ready");
                }
                return;
            }

            if (conn.playerControllers.Count == 0)
            {
                if (TinyNetLogLevel.logDebug)
                {
                    TinyLogger.LogWarning("Ready with no player object");
                }
            }

            conn.isReady = true;

            // This is only in case this is a listen server.
            TinyNetLocalConnectionToClient localConnection = conn as TinyNetLocalConnectionToClient;

            if (localConnection != null)
            {
                if (TinyNetLogLevel.logDev)
                {
                    TinyLogger.Log("NetworkServer Ready handling TinyNetLocalConnectionToClient");
                }

                // Setup spawned objects for local player
                // Only handle the local objects for the first player (no need to redo it when doing more local players)
                // and don't handle player objects here, they were done above
                foreach (TinyNetIdentity tinyNetId in LocalIdentityObjects.Values)
                {
                    // Need to call OnStartClient directly here, as it's already been added to the local object dictionary
                    // in the above SetLocalPlayer call
                    if (tinyNetId != null && tinyNetId.gameObject != null)
                    {
                        if (!tinyNetId.isClient)
                        {
                            localConnection.ShowObjectToConnection(tinyNetId);

                            if (TinyNetLogLevel.logDev)
                            {
                                TinyLogger.Log("LocalClient.SetSpawnObject calling OnStartClient");
                            }
                            tinyNetId.OnStartClient();
                        }
                    }
                }

                return;
            }

            // Spawn/update all current server objects
            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("Spawning " + LocalIdentityObjects.Count + " objects for conn " + conn.ConnectId);
            }

            TinyNetObjectSpawnFinishedMessage msg = new TinyNetObjectSpawnFinishedMessage();

            msg.state = 0;             //State 0 means we are starting the spawn messages 'spam'.
            SendMessageByChannelToTargetConnection(msg, DeliveryMethod.ReliableOrdered, conn);

            foreach (TinyNetIdentity tinyNetId in LocalIdentityObjects.Values)
            {
                if (tinyNetId == null)
                {
                    if (TinyNetLogLevel.logWarn)
                    {
                        TinyLogger.LogWarning("Invalid object found in server local object list (null TinyNetIdentity).");
                    }
                    continue;
                }
                if (!tinyNetId.gameObject.activeSelf)
                {
                    continue;
                }

                if (TinyNetLogLevel.logDebug)
                {
                    TinyLogger.Log("Sending spawn message for current server objects name='" + tinyNetId.gameObject.name + "' netId=" + tinyNetId.TinyInstanceID);
                }

                conn.ShowObjectToConnection(tinyNetId);
            }

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("Spawning objects for conn " + conn.ConnectId + " finished");
            }

            msg.state = 1;             //We finished spamming the spawn messages!
            SendMessageByChannelToTargetConnection(msg, DeliveryMethod.ReliableOrdered, conn);
        }