/// <inheritdoc/> public async ValueTask <WriteResult> WriteAsync(ProtocolLevel protocolLevel, PipeWriter pw, CancellationToken cancellationToken) { Write(protocolLevel, pw); await pw.FlushAsync(cancellationToken).AsNonGenericValueTask(); return(WriteResult.Written); }
protected override void Write(ProtocolLevel protocolLevel, Span <byte> span) { bool hasReason = !string.IsNullOrEmpty(_reasonString); bool hasUserProperties = _userProperties.Count > 0; span[0] = _header; span = span[1..].WriteVariableByteInteger(_contentSize);
/// <summary> /// Write only the topic, then call <see cref="WritePayload(PipeWriter, CancellationToken)"/>. /// </summary> /// <param name="writer"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async ValueTask <WriteResult> WriteAsync(ProtocolLevel protocolLevel, PipeWriter writer, CancellationToken cancellationToken) { int stringSize = _topic.MQTTSize(); writer.GetSpan(stringSize).WriteMQTTString(_topic); writer.Advance(stringSize); await writer.FlushAsync(cancellationToken); return(await WritePayload(writer, cancellationToken)); }
/// <summary> /// Instantiate a new <see cref="ProtocolConfiguration"/>. /// </summary> /// <param name="securePort">The default port when communication are secured.</param> /// <param name="nonSecurePort">The default port when communication are in clear text.</param> /// <param name="supportedLevel">The minimal <a href="http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc385349227">protocol level</a> supported.</param> /// <param name="singleLevelTopicWildcard"></param> /// <param name="multiLevelTopicWildcard"></param> /// <param name="protocolName">The protocol magic string that is send in the connect packet.</param> public ProtocolConfiguration( int securePort, int nonSecurePort, ProtocolLevel supportedLevel, string singleLevelTopicWildcard, string multiLevelTopicWildcard, string protocolName) { SecurePort = securePort; NonSecurePort = nonSecurePort; ProtocolLevel = supportedLevel; SingleLevelTopicWildcard = singleLevelTopicWildcard; MultiLevelTopicWildcard = multiLevelTopicWildcard; ProtocolName = protocolName; }
/// <inheritdoc/> public abstract int GetSize(ProtocolLevel protocolLevel);
void Write(ProtocolLevel protocolLevel, PipeWriter pw) { Write(protocolLevel, pw.GetSpan(GetSize(protocolLevel))); pw.Advance(GetSize(protocolLevel)); }
/// <inheritdoc/> protected override void Write(ProtocolLevel protocolLevel, Span <byte> span) { span[0] = _header; span[1] = 2; span = span[2..];
protected override void WriteContent(ProtocolLevel protocolLevel, Span <byte> span) { BinaryPrimitives.WriteUInt16BigEndian(span, (ushort)PacketId); span = span[2..];
protected abstract void WriteContent(ProtocolLevel protocolLevel, Span <byte> buffer);
/// <summary> /// The size of the Header. /// This is also the size of the <see cref="Span{T}"/> given when <see cref="WriteHeaderContent(Span{byte})"/> will be called. /// </summary> protected abstract int GetHeaderSize(ProtocolLevel protocolLevel);
/// <summary> /// The size of the Payload to write asynchronously. /// This is the amount of bytes that MUST be written when <see cref="WritePayloadAsync(PipeWriter, CancellationToken)"/> will be called. /// </summary> protected abstract int GetPayloadSize(ProtocolLevel protocolLevel);
/// <summary> /// The <see cref="GetSize()"/>, minus the header, and the bytes required to write the <see cref="GetRemainingSize()"/> itself. /// </summary> private int GetRemainingSize(ProtocolLevel protocolLevel) => GetHeaderSize(protocolLevel) + GetPayloadSize(protocolLevel);
/// <inheritdoc/> protected sealed override int GetHeaderSize(ProtocolLevel protocolLevel) => _topic.MQTTSize() + (Qos > QualityOfService.AtMostOnce ? 2 : 0) //On QoS 0, no packet id(2bytes). + protocolLevel switch {
protected abstract int GetRemainingSize(ProtocolLevel protocolLevel);
/// <summary> /// Write the Header, remaining size, and call <see cref="WriteHeaderContent(Span{byte})"/>. /// </summary> /// <param name="pw">The <see cref="PipeWriter"/> to write to.</param> protected void WriteHeader(ProtocolLevel protocolLevel, PipeWriter pw) { int headerSize = GetHeaderSize(protocolLevel); int remainingSize = GetRemainingSize(protocolLevel); int bytesToWrite = headerSize + remainingSize.CompactByteCount() + 1;//Compute how many byte will be written. Span <byte> span = pw.GetSpan(bytesToWrite)[..bytesToWrite];
/// <inheritdoc/> public override int GetSize(ProtocolLevel protocolLevel) => GetRemainingSize(protocolLevel).CompactByteCount() + 1 + GetRemainingSize(protocolLevel);
/// <inheritdoc/> public override int GetSize(ProtocolLevel protocolLevel) => 4;
/// <inheritdoc/> protected override void Write(ProtocolLevel protocolLevel, Span <byte> span) { span[0] = Header; span = span[1..].WriteVariableByteInteger(GetRemainingSize(protocolLevel));
protected override int GetRemainingSize(ProtocolLevel protocolLevel) => 2 //PacketId + _subscriptions.Sum(s => s.TopicFilter.MQTTSize() + 1) //topics + (protocolLevel > ProtocolLevel.MQTT3 ? _propertiesLength.CompactByteCount() : 0) + _propertiesLength;