Esempio n. 1
0
        /// <summary>
        ///     Handles the receiving of data.
        /// </summary>
        /// <param name="message">The buffer containing the bytes received.</param>
        protected async ValueTask HandleReceive(MessageReader message)
        {
            // Check if the first message received is the hello packet.
            if (_isFirst)
            {
                _isFirst = false;

                // Slice 4 bytes to get handshake data.
                if (_listener != null)
                {
                    using (var handshake = message.Copy(4))
                    {
                        await _listener.InvokeNewConnection(handshake, this);
                    }
                }
            }

            switch (message.Buffer[0])
            {
            //Handle reliable receives
            case (byte)MessageType.Reliable:
                await ReliableMessageReceive(message);

                break;

            //Handle acknowledgments
            case (byte)UdpSendOption.Acknowledgement:
                AcknowledgementMessageReceive(message.Buffer);
                break;

            //We need to acknowledge hello and ping messages but dont want to invoke any events!
            case (byte)UdpSendOption.Ping:
                await ProcessReliableReceive(message.Buffer, 1);

                Statistics.LogHelloReceive(message.Length);
                break;

            case (byte)UdpSendOption.Hello:
                await ProcessReliableReceive(message.Buffer, 1);

                Statistics.LogHelloReceive(message.Length);
                break;

            case (byte)UdpSendOption.Disconnect:
                using (var reader = message.Copy(1))
                {
                    await DisconnectRemote("The remote sent a disconnect request", reader);
                }
                break;

            //Treat everything else as unreliable
            default:
                using (var reader = message.Copy(1))
                {
                    await InvokeDataReceived(reader, MessageType.Unreliable);
                }
                Statistics.LogUnreliableReceive(message.Length - 1, message.Length);
                break;
            }
        }