using System.Globalization;
namespace KianaBH.KcpSharp.Base;
///
/// The result of a receive or peek operation.
///
public readonly struct KcpConversationReceiveResult : IEquatable
{
private readonly bool _connectionAlive;
///
/// The number of bytes received.
///
public int BytesReceived { get; }
///
/// Whether the underlying transport is marked as closed.
///
public bool TransportClosed => !_connectionAlive;
///
/// Construct a with the specified number of bytes received.
///
/// The number of bytes received.
public KcpConversationReceiveResult(int bytesReceived)
{
BytesReceived = bytesReceived;
_connectionAlive = true;
}
///
/// Checks whether the two instance is equal.
///
/// The one instance.
/// The other instance.
/// Whether the two instance is equal
public static bool operator ==(KcpConversationReceiveResult left, KcpConversationReceiveResult right)
{
return left.Equals(right);
}
///
/// Checks whether the two instance is not equal.
///
/// The one instance.
/// The other instance.
/// Whether the two instance is not equal
public static bool operator !=(KcpConversationReceiveResult left, KcpConversationReceiveResult right)
{
return !left.Equals(right);
}
///
public bool Equals(KcpConversationReceiveResult other)
{
return BytesReceived == other.BytesReceived && TransportClosed == other.TransportClosed;
}
///
public override bool Equals(object? obj)
{
return obj is KcpConversationReceiveResult other && Equals(other);
}
///
public override int GetHashCode()
{
return HashCode.Combine(BytesReceived, TransportClosed);
}
///
public override string ToString()
{
return _connectionAlive ? BytesReceived.ToString(CultureInfo.InvariantCulture) : "Transport is closed.";
}
}