예제 #1
0
 /// <summary>
 /// This virtual function can be overridden to perform custom functionality when data is received for a connection.
 /// <para>By default this function calls HandleData() which will process the data and invoke message handlers for any messages that it finds.</para>
 /// <code>
 /// using UnityEngine;
 /// using UnityEngine.Networking;
 ///
 /// public abstract class ExampleScript : <see cref="NetworkManager">NetworkManager</see>
 /// {
 ///    byte[] msgBuffer = new byte[1024];
 ///
 ///    public virtual void OnData(<see cref="NetworkConnection">NetworkConnection</see> conn, int channelId, int receivedSize)
 ///    {
 ///        conn.TransportRecieve(msgBuffer, receivedSize, channelId);
 ///    }
 /// }
 /// </code>
 /// </summary>
 /// <param name="conn"></param>
 /// <param name="receivedSize"></param>
 /// <param name="channelId"></param>
 public virtual void OnData(NetworkConnection conn, int receivedSize, int channelId)
 {
     conn.TransportReceive(m_MsgBuffer, receivedSize, channelId);
 }
예제 #2
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(11);
                m_AsyncConnect = ConnectState.Disconnected;
                return;

            case ConnectState.Resolved:
                m_AsyncConnect = ConnectState.Connecting;
                ContinueConnect();
                return;
            }
            if (m_Connection != null && (int)Time.time != m_StatResetTime)
            {
                m_Connection.ResetStats();
                m_StatResetTime = (int)Time.time;
            }
            NetworkEventType networkEventType;

            do
            {
                int num = 0;
                networkEventType = NetworkTransport.ReceiveFromHost(m_ClientId, out int _, out int channelId, m_MsgBuffer, (ushort)m_MsgBuffer.Length, out int receivedSize, out byte error);
                if (m_Connection != null)
                {
                    m_Connection.lastError = (NetworkError)error;
                }
                if (networkEventType != NetworkEventType.Nothing && LogFilter.logDev)
                {
                    Debug.Log("Client event: host=" + m_ClientId + " event=" + networkEventType + " error=" + error);
                }
                switch (networkEventType)
                {
                case NetworkEventType.ConnectEvent:
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("Client connected");
                    }
                    if (error != 0)
                    {
                        GenerateConnectError(error);
                        return;
                    }
                    m_AsyncConnect = ConnectState.Connected;
                    m_Connection.InvokeHandlerNoData(32);
                    break;

                case NetworkEventType.DataEvent:
                    if (error != 0)
                    {
                        GenerateDataError(error);
                        return;
                    }
                    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 && error != 6)
                    {
                        GenerateDisconnectError(error);
                    }
                    ClientScene.HandleClientDisconnect(m_Connection);
                    if (m_Connection != null)
                    {
                        m_Connection.InvokeHandlerNoData(33);
                    }
                    break;

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

                case NetworkEventType.Nothing:
                    break;
                }
                if (++num >= 500)
                {
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("MaxEventsPerFrame hit (" + 500 + ")");
                    }
                    break;
                }
            }while (m_ClientId != -1 && networkEventType != NetworkEventType.Nothing);
            if (m_Connection != null && m_AsyncConnect == ConnectState.Connected)
            {
                m_Connection.FlushChannels();
            }
        }
예제 #3
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 = 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();
            }
        }
예제 #4
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;
            }
            }

            int numEvents = 0;
            NetworkEventType networkEvent;

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

                networkEvent = 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((short)MsgType.Connect);
                    break;

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

#if UNITY_EDITOR
                    UnityEditor.NetworkDetailStats.IncrementStat(
                        UnityEditor.NetworkDetailStats.NetworkDirection.Incoming,
                        (short)MsgType.LLAPIMsg, "msg", 1);
#endif
                    // create a buffer with exactly 'receivedSize' size for the handlers so we don't need to read
                    // a size header (saves bandwidth)
                    byte[] data = new byte[receivedSize];
                    Array.Copy(m_MsgBuffer, data, receivedSize);

                    m_Connection.TransportReceive(data, 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((short)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);
        }