示例#1
0
        static void OnConnected()
        {
            if (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;
                NetworkTime.UpdateClient();
                connection.InvokeHandler(new ConnectMessage(), -1);
            }
            else
            {
                logger.LogError("Skipped Connect message handling because connection is null.");
            }
        }
示例#2
0
        void OnConnected()
        {
            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;
                NetworkTime.UpdateClient(this);
                m_Connection.InvokeHandlerNoData((short)MsgType.Connect);
            }
            else
            {
                Debug.LogError("Skipped Connect message handling because m_Connection is null.");
            }
        }
示例#3
0
        /// <summary>
        /// Connect client to a NetworkServer instance.
        /// </summary>
        /// <param name="uri">Address of the server to connect to</param>
        public async UniTask ConnectAsync(Uri uri)
        {
            if (logger.LogEnabled())
            {
                logger.Log("Client Connect: " + uri);
            }

            if (Transport == null)
            {
                Transport = GetComponent <Transport>();
            }
            if (Transport == null)
            {
                throw new InvalidOperationException("Transport could not be found for NetworkClient");
            }

            connectState = ConnectState.Connecting;

            try
            {
                IConnection transportConnection = await Transport.ConnectAsync(uri);

                InitializeAuthEvents();

                // setup all the handlers
                Connection = GetNewConnection(transportConnection);
                Time.Reset();

                RegisterMessageHandlers();
                Time.UpdateClient(this);
                OnConnected().Forget();
            }
            catch (Exception)
            {
                connectState = ConnectState.Disconnected;
                throw;
            }
        }
示例#4
0
        /// <summary>
        /// Connect client to a NetworkServer instance.
        /// </summary>
        /// <param name="uri">Address of the server to connect to</param>
        public async Task ConnectAsync(Uri uri)
        {
            if (logger.LogEnabled())
            {
                logger.Log("Client Connect: " + uri);
            }

            AsyncTransport transport = Transport;

            if (transport == null)
            {
                transport = GetComponent <AsyncTransport>();
            }

            connectState = ConnectState.Connecting;

            try
            {
                IConnection transportConnection = await transport.ConnectAsync(uri);


                RegisterSpawnPrefabs();
                InitializeAuthEvents();

                // setup all the handlers
                Connection = new NetworkConnection(transportConnection);
                Time.Reset();

                RegisterMessageHandlers(Connection);
                Time.UpdateClient(this);
                _ = OnConnected();
            }
            catch (Exception)
            {
                connectState = ConnectState.Disconnected;
                throw;
            }
        }
示例#5
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 && Time.time - lastPingTime >= pingFrequency)
            {
                NetworkPingMessage pingMessage = NetworkTime.GetPing();
                Send((short)MsgType.Ping, pingMessage);
                lastPingTime = Time.time;
            }

            // 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 (Transport.layer.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(pingWindowSize);

                        // 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;
                }
            }
        }