Skip to content

Commit

Permalink
Use actual disconnect message in ClientBase#DisconnectAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Apr 11, 2021
1 parent f0434f0 commit a56eed2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Impostor.Api/Net/IHazelConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public interface IHazelConnection
/// Disconnects the client and invokes the disconnect handler.
/// </summary>
/// <param name="reason">A reason.</param>
/// <param name="writer">A message to send with disconnect packet.</param>
/// <returns>Task that must be awaited.</returns>
ValueTask DisconnectAsync(string? reason);
ValueTask DisconnectAsync(string? reason, IMessageWriter? writer = null);
}
}
53 changes: 53 additions & 0 deletions src/Impostor.Api/Net/Messages/S2C/MessageDisconnect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using Impostor.Api.Innersloth;

namespace Impostor.Api.Net.Messages.S2C
{
public class MessageDisconnect
{
public static void Serialize(IMessageWriter writer, bool hasReason, DisconnectReason? reason, string? message)
{
writer.Write(hasReason);

if (hasReason)
{
if (reason == null)
{
throw new ArgumentNullException(nameof(reason));
}

writer.StartMessage(0);
writer.Write((byte)reason);

if (reason == DisconnectReason.Custom)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}

writer.Write(message);
}

writer.EndMessage();
}
}

public static void Deserialize(IMessageReader reader, out bool hasReason, out DisconnectReason? reason, out string? message)
{
hasReason = reader.ReadBoolean();

if (hasReason)
{
var inner = reader.ReadMessage();
reason = (DisconnectReason)inner.ReadByte();
message = reason == DisconnectReason.Custom ? inner.ReadString() : null;
}
else
{
reason = null;
message = null;
}
}
}
}
11 changes: 3 additions & 8 deletions src/Impostor.Server/Net/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,10 @@ public async ValueTask DisconnectAsync(DisconnectReason reason, string? message
return;
}

using var packet = MessageWriter.Get(MessageType.Reliable);
Message01JoinGameS2C.SerializeError(packet, false, reason, message);
using var writer = MessageWriter.Get();
MessageDisconnect.Serialize(writer, true, reason, message);

await Connection.SendAsync(packet);

// Need this to show the correct message, otherwise it shows a generic disconnect message.
await Task.Delay(TimeSpan.FromMilliseconds(250));

await Connection.DisconnectAsync(message ?? reason.ToString());
await Connection.DisconnectAsync(message ?? reason.ToString(), writer);
}
}
}
4 changes: 2 additions & 2 deletions src/Impostor.Server/Net/Hazel/HazelConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ValueTask SendAsync(IMessageWriter writer)
return InnerConnection.SendAsync(writer);
}

public ValueTask DisconnectAsync(string? reason)
public ValueTask DisconnectAsync(string? reason, IMessageWriter? writer = null)
{
return InnerConnection.Disconnect(reason);
return InnerConnection.Disconnect(reason, writer as MessageWriter);
}

public void DisposeInnerConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ValueTask SendAsync(IMessageWriter writer)
return ValueTask.CompletedTask;
}

public ValueTask DisconnectAsync(string reason)
public ValueTask DisconnectAsync(string reason, IMessageWriter writer = null)
{
return ValueTask.CompletedTask;
}
Expand Down

0 comments on commit a56eed2

Please sign in to comment.