Beispiel #1
0
        public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment <byte> payload)
        {
            NetworkEventType eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, messageBuffer, messageBuffer.Length, out int receivedSize, out byte error);

            clientId = GetMLAPIClientId((byte)hostId, (ushort)connectionId, false);

            NetworkError networkError = (NetworkError)error;

            if (networkError == NetworkError.MessageToLong)
            {
                byte[] tempBuffer;

                if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[])temporaryBufferReference.Target).Length >= receivedSize)
                {
                    tempBuffer = (byte[])temporaryBufferReference.Target;
                }
                else
                {
                    tempBuffer = new byte[receivedSize];
                    temporaryBufferReference = new WeakReference(tempBuffer);
                }

                eventType = RelayTransport.Receive(out hostId, out connectionId, out channelId, tempBuffer, tempBuffer.Length, out receivedSize, out error);
                payload   = new ArraySegment <byte>(tempBuffer, 0, receivedSize);
            }
            else
            {
                payload = new ArraySegment <byte>(messageBuffer, 0, receivedSize);
            }

            channelName = channelIdToName[channelId];

            if (networkError == NetworkError.Timeout)
            {
                // In UNET. Timeouts are not disconnects. We have to translate that here.
                eventType = NetworkEventType.DisconnectEvent;
            }

            // Translate NetworkEventType to NetEventType
            switch (eventType)
            {
            case NetworkEventType.DataEvent:
                return(NetEventType.Data);

            case NetworkEventType.ConnectEvent:
                return(NetEventType.Connect);

            case NetworkEventType.DisconnectEvent:
                return(NetEventType.Disconnect);

            case NetworkEventType.Nothing:
                return(NetEventType.Nothing);

            case NetworkEventType.BroadcastEvent:
                return(NetEventType.Nothing);
            }

            return(NetEventType.Nothing);
        }
        public override NetworkEvent PollEvent(out ulong clientId, out NetworkChannel networkChannel, out ArraySegment <byte> payload, out float receiveTime)
        {
            var eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, m_MessageBuffer, m_MessageBuffer.Length, out int receivedSize, out byte error);

            clientId    = GetMLAPIClientId((byte)hostId, (ushort)connectionId, false);
            receiveTime = UnityEngine.Time.realtimeSinceStartup;

            var networkError = (NetworkError)error;

            if (networkError == NetworkError.MessageToLong)
            {
                byte[] tempBuffer;

                if (m_TemporaryBufferReference != null && m_TemporaryBufferReference.IsAlive && ((byte[])m_TemporaryBufferReference.Target).Length >= receivedSize)
                {
                    tempBuffer = (byte[])m_TemporaryBufferReference.Target;
                }
                else
                {
                    tempBuffer = new byte[receivedSize];
                    m_TemporaryBufferReference = new WeakReference(tempBuffer);
                }

                eventType = RelayTransport.Receive(out hostId, out connectionId, out channelId, tempBuffer, tempBuffer.Length, out receivedSize, out error);
                payload   = new ArraySegment <byte>(tempBuffer, 0, receivedSize);
            }
            else
            {
                payload = new ArraySegment <byte>(m_MessageBuffer, 0, receivedSize);
            }

            if (m_ChannelIdToName.TryGetValue(channelId, out NetworkChannel value))
            {
                networkChannel = value;
            }
            else
            {
                networkChannel = NetworkChannel.Internal;
            }

            if (m_ConnectTask != null && hostId == m_ServerHostId && connectionId == m_ServerConnectionId)
            {
                if (eventType == NetworkEventType.ConnectEvent)
                {
                    // We just got a response to our connect request.
                    m_ConnectTask.Message            = null;
                    m_ConnectTask.SocketError        = networkError == NetworkError.Ok ? System.Net.Sockets.SocketError.Success : System.Net.Sockets.SocketError.SocketError;
                    m_ConnectTask.State              = null;
                    m_ConnectTask.Success            = networkError == NetworkError.Ok;
                    m_ConnectTask.TransportCode      = (byte)networkError;
                    m_ConnectTask.TransportException = null;
                    m_ConnectTask.IsDone             = true;

                    m_ConnectTask = null;
                }
                else if (eventType == NetworkEventType.DisconnectEvent)
                {
                    // We just got a response to our connect request.
                    m_ConnectTask.Message            = null;
                    m_ConnectTask.SocketError        = System.Net.Sockets.SocketError.SocketError;
                    m_ConnectTask.State              = null;
                    m_ConnectTask.Success            = false;
                    m_ConnectTask.TransportCode      = (byte)networkError;
                    m_ConnectTask.TransportException = null;
                    m_ConnectTask.IsDone             = true;

                    m_ConnectTask = null;
                }
            }

            if (networkError == NetworkError.Timeout)
            {
                // In UNET. Timeouts are not disconnects. We have to translate that here.
                eventType = NetworkEventType.DisconnectEvent;
            }

            // Translate NetworkEventType to NetEventType
            switch (eventType)
            {
            case NetworkEventType.DataEvent:
                return(NetworkEvent.Data);

            case NetworkEventType.ConnectEvent:
                return(NetworkEvent.Connect);

            case NetworkEventType.DisconnectEvent:
                return(NetworkEvent.Disconnect);

            case NetworkEventType.Nothing:
                return(NetworkEvent.Nothing);

            case NetworkEventType.BroadcastEvent:
                return(NetworkEvent.Nothing);
            }

            return(NetworkEvent.Nothing);
        }