/* * Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM * File: FBMMessageHeader.cs * * FBMMessageHeader.cs is part of VNLib.Net.Messaging.FBM which is part of the larger * VNLib collection of libraries and utilities. * * VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ using System; namespace VNLib.Net.Messaging.FBM { /// /// Represents a Key-Value pair FBM response message header /// public readonly struct FBMMessageHeader : IEquatable { private readonly IFBMHeaderBuffer _buffer; /* * Header values are all stored consecutively inside a single * buffer, position the value is stored within the buffer * is designated by it's absolute offset and length within * the buffer segment */ private readonly int _headerOffset; private readonly int _headerLength; /// /// The header value or command /// public readonly HeaderCommand Header { get; } /// /// The header value, or if default /// public readonly ReadOnlySpan Value => _buffer != null ? _buffer.GetSpan(_headerOffset, _headerLength) : null; /// /// Gets the header value as a or null if default instance /// /// The allocates string of the value public readonly string? GetValueString() => _buffer != null ? Value.ToString() : null; /// /// Initializes a new of the sepcified command /// that utilizes memory from the specified buffer /// /// The buffer that owns the memory our header is stored in /// The command this header represents /// The char offset within the buffer our header begins /// The character length of our header value internal FBMMessageHeader(IFBMHeaderBuffer buffer, HeaderCommand command, int offset, int length) { Header = command; _buffer = buffer; _headerLength = length; _headerOffset = offset; } /// public override bool Equals(object? obj) => obj is FBMMessageHeader other && Equals(other); /// /// Calculates a hash code from the parameter /// using original string hashcode computation /// /// The unique hashcode for the character sequence public override int GetHashCode() => string.GetHashCode(Value, StringComparison.Ordinal); /// public static bool operator ==(FBMMessageHeader left, FBMMessageHeader right) => left.Equals(right); /// public static bool operator !=(FBMMessageHeader left, FBMMessageHeader right) => !(left == right); /// /// Determines if the other response header is equal to the current header by /// comparing its command and its value /// /// The other header to compare /// True if both headers have the same commad and value sequence public bool Equals(FBMMessageHeader other) => Header == other.Header && Value.SequenceEqual(other.Value); /// /// Gets a concatinated string of the current instance for debugging purposes /// public readonly override string ToString() => $"{Header}:{Value.ToString()}"; } }