Esempio n. 1
0
        public bool SendNotify(Connection connection, byte[] data, object userObject)
        {
            if (connection.SendWindow.IsFull)
            {
                return(false);
            }

            // 1: packet type enum                      - 1 byte
            // 2: sequence number for this packet       - _config.SequenceNumberBytes bytes
            // 3: recv sequence                         - _config.SequenceNumberBytes bytes
            // 4: recv mask                             - 8 bytes

            if (data.Length > (_config.Mtu - NotifyPacketHeaderSize))
            {
                throw new InvalidOperationException();
            }

            var sequenceNumberForPacket = connection.SendSequencer.Next();

            var buffer = GetMtuBuffer();

            // copy from data into buffer
            Buffer.BlockCopy(data, 0, buffer, NotifyPacketHeaderSize, data.Length);

            // write packet type
            buffer[0] = (byte)PacketTypes.Notify;
            var offset = 1;

            // write sequence number of *this* packet
            ByteUtils.WriteULong(buffer, offset, _config.SequenceNumberBytes, sequenceNumberForPacket);
            offset += _config.SequenceNumberBytes;

            // write *recv sequence* for this connection
            ByteUtils.WriteULong(buffer, offset, _config.SequenceNumberBytes, connection.RecvSequence);
            offset += _config.SequenceNumberBytes;

            // write *recv mask* for this connection
            ByteUtils.WriteULong(buffer, offset, 8, connection.RecvMask);

            // push on send window
            connection.SendWindow.Push(new SendEnvelope {
                Sequence = sequenceNumberForPacket,
                SendTime = _clock.ElapsedInSeconds,
                UserData = userObject
            });

            Send(connection, buffer, NotifyPacketHeaderSize + data.Length);
            return(true);
        }
Esempio n. 2
0
        public bool SendNotify(Connection connection, byte[] data, object?userObject)
        {
            if (connection.SendWindow.IsFull)
            {
                return(false);
            }


            if (data.Length > (_config.MTU - NotifyPacketHeaderSize))
            {
                Log.Error($"[SendNotify]: Data too large, above MTU-Header_Size({NotifyPacketHeaderSize}) {data.Length}");
                return(false);
            }

            var sequenceNumberForPacket = connection.SendSequencer.Next();

            var buffer = GetMtuBuffer();

            Buffer.BlockCopy(data, 0, buffer, NotifyPacketHeaderSize, data.Length);

            // Fill header data
            buffer[0] = (byte)PacketType.Notify;
            var offset = 1;

            ByteUtils.WriteULong(buffer, offset, _config.SequenceNumberBytes, sequenceNumberForPacket);
            offset += _config.SequenceNumberBytes;
            ByteUtils.WriteULong(buffer, offset, _config.SequenceNumberBytes, connection.LastReceivedSequence);
            offset += _config.SequenceNumberBytes;
            ByteUtils.WriteULong(buffer, offset, sizeof(ulong), connection.ReceiveMask);

            connection.SendWindow.Push(new SendEnvelope()
            {
                Sequence = sequenceNumberForPacket,
                SendTime = _timer.Now,
                UserData = userObject
            });

            SendInternal(connection, buffer, NotifyPacketHeaderSize + data.Length);
            return(true);
        }