protected override bool CanHandle(HandlerParams param) { if (!base.CanHandle(param)) { return(false); } var message = param.Update.Message; if (CanHandleMessage(message)) { if (MessageFlags.HasFlag(MessageFlag.HasText)) { if (param.IsCommand && IsCommand) { return(true); } return(IsTextMatch(message.Text)); } return(true); } return(false); }
private void SetPayloadLength(uint payloadLength) { if (payloadLength <= 255) { if (!MessageFlag.HasFlag(MessageFlag.ShortRecord)) { MessageFlag |= MessageFlag.ShortRecord; } } else { if (MessageFlag.HasFlag(MessageFlag.ShortRecord)) { MessageFlag &= ~MessageFlag.ShortRecord; } } _payloadLength = payloadLength; }
/// <summary> /// Create a header from a span of bytes /// </summary> /// <param name="recordToDecode">A span of bytes</param> public RecordHeader(ReadOnlySpan <byte> recordToDecode) { int idxRecord = 0; // First byte is the Message flag and type name format var header = recordToDecode[idxRecord++]; MessageFlag = (MessageFlag)(header & 0b1111_1000); TypeNameFormat = (TypeNameFormat)(header & 0b0000_0111); PayloadTypeLength = recordToDecode[idxRecord++]; if (MessageFlag.HasFlag(MessageFlag.ShortRecord)) { _payloadLength = recordToDecode[idxRecord++]; } else { _payloadLength = BinaryPrimitives.ReadUInt32BigEndian(recordToDecode.Slice(idxRecord, 4)); idxRecord += 4; } if (MessageFlag.HasFlag(MessageFlag.IdLength)) { IdLength = recordToDecode[idxRecord++]; } if (PayloadTypeLength > 0) { PayloadType = new byte[PayloadTypeLength]; recordToDecode.Slice(idxRecord, PayloadTypeLength).CopyTo(PayloadType); idxRecord += PayloadTypeLength; } if (IdLength != null) { if (IdLength.Value > 0) { PayloadId = new byte[IdLength.Value]; recordToDecode.Slice(idxRecord, IdLength.Value).CopyTo(PayloadId); } } }
/// <summary> /// Serialize the header /// </summary> /// <param name="header">Serialized byte span</param> public void Serialize(Span <byte> header) { if (header.Length < Length) { throw new ArgumentException($"Header span must be same or larger size than the header size"); } int idxRecord = 0; // Calculate the size of the header header[idxRecord++] = (byte)((byte)MessageFlag | (byte)TypeNameFormat); header[idxRecord++] = PayloadTypeLength; if (MessageFlag.HasFlag(MessageFlag.ShortRecord)) { header[idxRecord++] = (byte)PayloadLength; } else { BinaryPrimitives.WriteUInt32BigEndian(header.Slice(idxRecord, 4), PayloadLength); idxRecord += 4; } if (MessageFlag.HasFlag(MessageFlag.IdLength)) { header[idxRecord++] = IdLength !.Value; } if (PayloadTypeLength > 0) { PayloadType.CopyTo(header.Slice(idxRecord, PayloadTypeLength)); idxRecord += PayloadTypeLength; } if (IsComposedMessage) { PayloadId.CopyTo(header.Slice(idxRecord)); } }