// This file is provided under The MIT License as part of RiptideNetworking.
// Copyright (c) Tom Weiland
// For additional information please see the included LICENSE.md file or view it on GitHub:
// https://github.com/RiptideNetworking/Riptide/blob/main/LICENSE.md
using System;
namespace Riptide.Utils
{
/// Contains miscellaneous helper methods.
internal class Helper
{
/// The text to log when disconnected due to .
private const string DCNeverConnected = "Never connected";
/// The text to log when disconnected due to .
private const string DCTransportError = "Transport error";
/// The text to log when disconnected due to .
private const string DCTimedOut = "Timed out";
/// The text to log when disconnected due to .
private const string DCKicked = "Kicked";
/// The text to log when disconnected due to .
private const string DCServerStopped = "Server stopped";
/// The text to log when disconnected due to .
private const string DCDisconnected = "Disconnected";
/// The text to log when disconnected due to .
private const string DCPoorConnection = "Poor connection";
/// The text to log when disconnected or rejected due to an unknown reason.
private const string UnknownReason = "Unknown reason";
/// The text to log when the connection failed due to .
private const string CRNoConnection = "No connection";
/// The text to log when the connection failed due to .
private const string CRAlreadyConnected = "This client is already connected";
/// The text to log when the connection failed due to .
private const string CRServerFull = "Server is full";
/// The text to log when the connection failed due to .
private const string CRRejected = "Rejected";
/// The text to log when the connection failed due to .
private const string CRCustom = "Rejected (with custom data)";
/// Determines whether or form should be used based on the .
/// The amount that and refer to.
/// The singular form.
/// The plural form.
/// if is 1; otherwise .
internal static string CorrectForm(int amount, string singular, string plural = "")
{
if (string.IsNullOrEmpty(plural))
plural = $"{singular}s";
return amount == 1 ? singular : plural;
}
/// Calculates the signed gap between sequence IDs, accounting for wrapping.
/// The new sequence ID.
/// The previous sequence ID.
/// The signed gap between the two given sequence IDs. A positive gap means is newer than . A negative gap means is older than .
internal static int GetSequenceGap(ushort seqId1, ushort seqId2)
{
int gap = seqId1 - seqId2;
if (Math.Abs(gap) <= 32768) // Difference is small, meaning sequence IDs are close together
return gap;
else // Difference is big, meaning sequence IDs are far apart
return (seqId1 <= 32768 ? ushort.MaxValue + 1 + seqId1 : seqId1) - (seqId2 <= 32768 ? ushort.MaxValue + 1 + seqId2 : seqId2);
}
/// Retrieves the appropriate reason string for the given .
/// The to retrieve the string for.
/// The appropriate reason string.
internal static string GetReasonString(DisconnectReason forReason)
{
switch (forReason)
{
case DisconnectReason.NeverConnected:
return DCNeverConnected;
case DisconnectReason.TransportError:
return DCTransportError;
case DisconnectReason.TimedOut:
return DCTimedOut;
case DisconnectReason.Kicked:
return DCKicked;
case DisconnectReason.ServerStopped:
return DCServerStopped;
case DisconnectReason.Disconnected:
return DCDisconnected;
case DisconnectReason.PoorConnection:
return DCPoorConnection;
default:
return $"{UnknownReason} '{forReason}'";
}
}
/// Retrieves the appropriate reason string for the given .
/// The to retrieve the string for.
/// The appropriate reason string.
internal static string GetReasonString(RejectReason forReason)
{
switch (forReason)
{
case RejectReason.NoConnection:
return CRNoConnection;
case RejectReason.AlreadyConnected:
return CRAlreadyConnected;
case RejectReason.ServerFull:
return CRServerFull;
case RejectReason.Rejected:
return CRRejected;
case RejectReason.Custom:
return CRCustom;
default:
return $"{UnknownReason} '{forReason}'";
}
}
}
}