コード例 #1
0
        internal virtual void Update()
        {
            if (m_ClientId == -1)
            {
                return;
            }

            switch (m_AsyncConnect)
            {
            case ConnectState.None:
            case ConnectState.Resolving:
            case ConnectState.Disconnected:
                return;

            case ConnectState.Failed:
                GenerateConnectError((int)NetworkError.DNSFailure);
                m_AsyncConnect = ConnectState.Disconnected;
                return;

            case ConnectState.Resolved:
                m_AsyncConnect = ConnectState.Connecting;
                ContinueConnect();
                return;

            case ConnectState.Connecting:
            case ConnectState.Connected:
            {
                break;
            }
            }

            if (m_Connection != null)
            {
                if ((int)Time.time != m_StatResetTime)
                {
                    m_Connection.ResetStats();
                    m_StatResetTime = (int)Time.time;
                }
            }

            int numEvents = 0;
            NetworkEventType networkEvent;

            do
            {
                int  connectionId;
                int  channelId;
                int  receivedSize;
                byte error;

                networkEvent = m_UseRelay ? RelayTransport.ReceiveFromHost(m_ClientId, out connectionId, out channelId, m_MsgBuffer, (ushort)m_MsgBuffer.Length, out receivedSize, out error) : NetworkTransport.ReceiveFromHost(m_ClientId, out connectionId, out channelId, m_MsgBuffer, (ushort)m_MsgBuffer.Length, out receivedSize, out error);
                if (m_Connection != null)
                {
                    m_Connection.lastError = (NetworkError)error;
                }

                if (networkEvent != NetworkEventType.Nothing)
                {
                    if (LogFilter.logDev)
                    {
                        Debug.Log("Client event: host=" + m_ClientId + " event=" + networkEvent + " error=" + error);
                    }
                }

                switch (networkEvent)
                {
                case NetworkEventType.ConnectEvent:

                    if (LogFilter.logDebug)
                    {
                        Debug.Log("Client connected");
                    }

                    if (error != 0)
                    {
                        GenerateConnectError(error);
                        return;
                    }

                    m_AsyncConnect = ConnectState.Connected;
                    m_Connection.InvokeHandlerNoData(MsgType.Connect);
                    break;

                case NetworkEventType.DataEvent:
                    if (error != 0)
                    {
                        GenerateDataError(error);
                        return;
                    }

#if UNITY_EDITOR
                    UnityEditor.NetworkDetailStats.IncrementStat(
                        UnityEditor.NetworkDetailStats.NetworkDirection.Incoming,
                        MsgType.LLAPIMsg, "msg", 1);
#endif

                    m_MsgReader.SeekZero();
                    m_Connection.TransportReceive(m_MsgBuffer, receivedSize, channelId);
                    break;

                case NetworkEventType.DisconnectEvent:
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("Client disconnected");
                    }

                    m_AsyncConnect = ConnectState.Disconnected;

                    if (error != 0)
                    {
                        if ((NetworkError)error != NetworkError.Timeout)
                        {
                            GenerateDisconnectError(error);
                        }
                    }
                    ClientScene.HandleClientDisconnect(m_Connection);
                    if (m_Connection != null)
                    {
                        m_Connection.InvokeHandlerNoData(MsgType.Disconnect);
                    }
                    break;

                case NetworkEventType.Nothing:
                    break;

                default:
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Unknown network message type received: " + networkEvent);
                    }
                    break;
                }

                if (++numEvents >= k_MaxEventsPerFrame)
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("MaxEventsPerFrame hit (" + k_MaxEventsPerFrame + ")");
                    }
                    break;
                }
                if (m_ClientId == -1)
                {
                    break;
                }
            }while (networkEvent != NetworkEventType.Nothing);

            if (m_Connection != null && m_AsyncConnect == ConnectState.Connected)
            {
                m_Connection.FlushChannels();
            }
        }
コード例 #2
0
        public void Update()
        {
            if (m_ServerHostId == -1)
            {
                return;
            }

            int  connectionId;
            int  channelId;
            int  receivedSize;
            byte error;

            var networkEvent = NetworkEventType.DataEvent;

            if (m_RelaySlotId != -1)
            {
                networkEvent = NetworkTransport.ReceiveRelayEventFromHost(m_ServerHostId, out error);
                if (NetworkEventType.Nothing != networkEvent)
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("NetGroup event:" + networkEvent);
                    }
                }
                if (networkEvent == NetworkEventType.ConnectEvent)
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("NetGroup server connected");
                    }
                }
                if (networkEvent == NetworkEventType.DisconnectEvent)
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("NetGroup server disconnected");
                    }
                }
            }

            do
            {
                networkEvent = m_UseRelay ? RelayTransport.ReceiveFromHost(m_ServerHostId, out connectionId, out channelId, m_MsgBuffer, (ushort)m_MsgBuffer.Length, out receivedSize, out error) : NetworkTransport.ReceiveFromHost(m_ServerHostId, out connectionId, out channelId, m_MsgBuffer, (int)m_MsgBuffer.Length, out receivedSize, out error);
                if (networkEvent != NetworkEventType.Nothing)
                {
                    if (LogFilter.logDev)
                    {
                        Debug.Log("Server event: host=" + m_ServerHostId + " event=" + networkEvent + " error=" + error);
                    }
                }

                switch (networkEvent)
                {
                case NetworkEventType.ConnectEvent:
                {
                    HandleConnect(connectionId, error);
                    break;
                }

                case NetworkEventType.DataEvent:
                {
                    HandleData(connectionId, channelId, receivedSize, error);
                    break;
                }

                case NetworkEventType.DisconnectEvent:
                {
                    HandleDisconnect(connectionId, error);
                    break;
                }

                case NetworkEventType.Nothing:
                    break;

                default:
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Unknown network message type received: " + networkEvent);
                    }
                    break;
                }
            }while (networkEvent != NetworkEventType.Nothing);

            UpdateConnections();
        }