Merge pull request #464 from miniduikboot/fix/custom-dc-msg

Fix/custom dc msg
This commit is contained in:
miniduikboot authored and GitHub committed 2022-02-28 18:04:47 +01:00
commit c26d03a8bc
6 files changed
+39 -28

No files matched your search

@@ -22,6 +22,7 @@ namespace Impostor.Api.Net.Messages.S2C
writer.EndMessage();
}
[Obsolete("JoinGame errors are no longer used by 2021.11.9 and up, disconnect clients instead.")]
public static void SerializeError(IMessageWriter writer, bool clear, DisconnectReason reason, string? message = null)
{
if (clear)
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Messages.S2C;
using Impostor.Hazel;
namespace Impostor.Server
{
public static class IHazelConnectionExtensions
{
/// <summary>
/// Disconnect a connection using a custom message.
/// </summary>
/// <param name="connection">The connection to disconnect.</param>
/// <param name="message">The message to disconnect with.</param>
/// <returns>Task that should be awaited to ensure disconnection.</returns>
public static async ValueTask CustomDisconnectAsync(this IHazelConnection connection, string message)
{
if (!connection.IsConnected)
{
return;
}
using var writer = MessageWriter.Get();
MessageDisconnect.Serialize(writer, true, DisconnectReason.Custom, message);
await connection.DisconnectAsync(DisconnectReason.Custom.ToString(), writer);
}
}
}
+1 -11
View File
@@ -5,8 +5,6 @@ using Impostor.Api;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Messages;
using Impostor.Api.Net.Messages.S2C;
using Impostor.Hazel;
using Impostor.Server.Net.State;
namespace Impostor.Server.Net
@@ -55,15 +53,7 @@ namespace Impostor.Server.Net
public async ValueTask DisconnectAsync(DisconnectReason reason, string? message = null)
{
if (!Connection.IsConnected)
{
return;
}
using var writer = MessageWriter.Get();
MessageDisconnect.Serialize(writer, true, reason, message);
await Connection.DisconnectAsync(message ?? reason.ToString(), writer);
await Connection.CustomDisconnectAsync(message ?? reason.ToString());
}
}
}
@@ -8,7 +8,6 @@ using Impostor.Api.Events.Managers;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Messages;
using Impostor.Api.Net.Messages.S2C;
using Impostor.Hazel;
using Impostor.Server.Config;
using Impostor.Server.Events.Client;
@@ -85,25 +84,19 @@ namespace Impostor.Server.Net.Manager
_ => throw new ArgumentOutOfRangeException(),
};
Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.Custom, message);
await connection.SendAsync(packet);
await connection.CustomDisconnectAsync(message);
return;
}
if (name.Length > 10)
{
using var packet = MessageWriter.Get(MessageType.Reliable);
Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.Custom, DisconnectMessages.UsernameLength);
await connection.SendAsync(packet);
await connection.CustomDisconnectAsync(DisconnectMessages.UsernameLength);
return;
}
if (string.IsNullOrWhiteSpace(name))
{
using var packet = MessageWriter.Get(MessageType.Reliable);
Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.Custom, DisconnectMessages.UsernameIllegalCharacters);
await connection.SendAsync(packet);
await connection.CustomDisconnectAsync(DisconnectMessages.UsernameIllegalCharacters);
return;
}
@@ -56,27 +56,25 @@ namespace Impostor.Server.Net.Redirector
{
Message01JoinGameC2S.Deserialize(reader, out var gameCode);
using var packet = MessageWriter.Get(MessageType.Reliable);
var endpoint = await _nodeLocator.FindAsync(gameCode);
if (endpoint == null)
{
Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.GameMissing);
await DisconnectAsync(DisconnectReason.GameMissing);
}
else
{
using var packet = MessageWriter.Get(MessageType.Reliable);
Message13RedirectS2C.Serialize(packet, false, endpoint);
await Connection.SendAsync(packet);
}
await Connection.SendAsync(packet);
break;
}
case MessageFlags.GetGameListV2:
{
// TODO: Implement.
using var packet = MessageWriter.Get(MessageType.Reliable);
Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.Custom, DisconnectMessages.NotImplemented);
await Connection.SendAsync(packet);
await DisconnectAsync(DisconnectReason.Custom, DisconnectMessages.NotImplemented);
break;
}
@@ -382,7 +382,6 @@ namespace Impostor.Server.Net.State
case InnerPlayerControl control:
{
if (GameState != GameStates.Started)
if (GameState != GameStates.Started && GameState != GameStates.Starting)
{
GameNet.GameData?.RemovePlayer(control);