Beispiel #1
0
        internal virtual void Update()
        {
            if (m_ClientId == -1)
            {
                return;
            }

            // don't do anything if we aren't fully connected
            // -> we don't check Client.Connected because then we wouldn't
            //    process the last disconnect message.
            if (connectState != ConnectState.Connecting &&
                connectState != ConnectState.Connected)
            {
                return;
            }

            // pause message handling while a scene load is in progress
            //
            // problem:
            //   if we handle packets (calling the msgDelegates) while a
            //   scene load is in progress, then all the handled data and state
            //   will be lost as soon as the scene load is finished, causing
            //   state bugs.
            //
            // solution:
            //   don't handle messages until scene load is finished. the
            //   transport layer will queue it automatically.
            if (pauseMessageHandling)
            {
                Debug.Log("NetworkClient.Update paused during scene load...");
                return;
            }

            if (connectState == ConnectState.Connected)
            {
                NetworkTime.UpdateClient(this);
            }

            // any new message?
            // -> calling it once per frame is okay, but really why not just
            //    process all messages and make it empty..
            TransportEvent transportEvent;

            byte[] data;
            while (NetworkManager.singleton.transport.ClientGetNextMessage(out transportEvent, out data))
            {
                switch (transportEvent)
                {
                case TransportEvent.Connected:
                    //Debug.Log("NetworkClient loop: Connected");

                    if (m_Connection != null)
                    {
                        // reset network time stats
                        NetworkTime.Reset();

                        // the handler may want to send messages to the client
                        // thus we should set the connected state before calling the handler
                        connectState = ConnectState.Connected;
                        m_Connection.InvokeHandlerNoData((short)MsgType.Connect);
                    }
                    else
                    {
                        Debug.LogError("Skipped Connect message handling because m_Connection is null.");
                    }

                    break;

                case TransportEvent.Data:
                    //Debug.Log("NetworkClient loop: Data: " + BitConverter.ToString(data));

                    if (m_Connection != null)
                    {
                        m_Connection.TransportReceive(data);
                    }
                    else
                    {
                        Debug.LogError("Skipped Data message handling because m_Connection is null.");
                    }

                    break;

                case TransportEvent.Disconnected:
                    //Debug.Log("NetworkClient loop: Disconnected");
                    connectState = ConnectState.Disconnected;

                    //GenerateDisconnectError(error); TODO which one?
                    ClientScene.HandleClientDisconnect(m_Connection);
                    if (m_Connection != null)
                    {
                        m_Connection.InvokeHandlerNoData((short)MsgType.Disconnect);
                    }
                    break;
                }
            }
        }