Skip to content

Commit

Permalink
Update Client.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Nov 12, 2024
1 parent 1958b65 commit de7c497
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions Obsidian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public sealed class Client : IDisposable
/// </summary>
private CachedProfile? profile;

private readonly Channel<IClientboundPacket> packetQueue;

/// <summary>
/// The cancellation token source used to cancel the packet queue loop and disconnect the client.
/// </summary>
Expand Down Expand Up @@ -150,8 +152,6 @@ public sealed class Client : IDisposable
/// </summary>
public string? Brand { get; internal set; }

private Channel<IClientboundPacket> packetQueue;

public Client(ConnectionContext connectionContext,
ILoggerFactory loggerFactory, IUserCache playerCache,
Server server)
Expand Down Expand Up @@ -213,21 +213,19 @@ private async ValueTask<PacketData> GetNextPacketAsync()
return error ? PacketData.Default : new PacketData { Id = packetId, Data = packetData, IsDisposable = true };
}

private async Task StartPacketQueueAsync()
private async Task HandlePacketQueueAsync()
{
while (!cancellationSource.IsCancellationRequested && connectionContext.IsConnected())
while (!cancellationSource.IsCancellationRequested && this.connectionContext.IsConnected())
{
var packet = await this.packetQueue.Reader.ReadAsync(this.cancellationSource.Token);

this.SendPacket(packet);
}
}

public async Task StartConnectionAsync()
private async Task HandlePacketsAsync()
{
_ = this.StartPacketQueueAsync();

while (!cancellationSource.IsCancellationRequested && connectionContext.IsConnected())
while (!cancellationSource.IsCancellationRequested && this.connectionContext.IsConnected())
{
using var packetData = await GetNextPacketAsync();

Expand Down Expand Up @@ -293,6 +291,11 @@ public async Task StartConnectionAsync()
break;
}
}
}

public async Task StartConnectionAsync()
{
await Task.WhenAll([this.HandlePacketsAsync(), this.HandlePacketQueueAsync()]);

Logger.LogInformation("Disconnected client");

Expand All @@ -302,18 +305,10 @@ public async Task StartConnectionAsync()
await this.server.EventDispatcher.ExecuteEventAsync(new PlayerLeaveEventArgs(Player, this.server, DateTimeOffset.Now));
}

Disconnected?.Invoke(this);
this.Dispose();//Dispose client after
this.Disconnect();
}

internal void ThrowIfInvalidEncryptionRequest()
{
if (this.Player is null)
throw new InvalidOperationException("Received Encryption Response before sending Login Start.");

if (this.randomToken is null)
throw new InvalidOperationException("Received Encryption Response before sending Encryption Request.");
}

public async ValueTask<bool> TrySetCachedProfileAsync(string username)
{
Expand Down Expand Up @@ -414,12 +409,27 @@ private void InitializeId()
this.Logger = this.loggerFactory.CreateLogger($"Client({this.id})");
}

private async ValueTask<bool> HandlePacketAsync(PacketData packetData) => await this.handlers[this.State].HandleAsync(packetData);
private async ValueTask<bool> HandlePacketAsync(PacketData packetData)
{
try
{
return await this.handlers[this.State].HandleAsync(packetData);
}
catch (Exception ex)
{
this.Logger.LogDebug(ex, "An error has occured handling packet");
}

return false;
}

public async ValueTask DisconnectAsync(ChatMessage reason) => await this.QueuePacketAsync(new DisconnectPacket(reason, State));

public async ValueTask QueuePacketAsync(IClientboundPacket packet)
{
if (this.cancellationSource.IsCancellationRequested)
return;

var args = new QueuePacketEventArgs(this.server, this, packet);

var result = await this.server.EventDispatcher.ExecuteEventAsync(args);
Expand Down Expand Up @@ -468,6 +478,15 @@ internal void Disconnect()
this.Dispose();
}

internal void ThrowIfInvalidEncryptionRequest()
{
if (this.Player is null)
throw new InvalidOperationException("Received Encryption Response before sending Login Start.");

if (this.randomToken is null)
throw new InvalidOperationException("Received Encryption Response before sending Encryption Request.");
}

internal void SetState(ClientState state) => this.State = state;

public void Dispose()
Expand Down

0 comments on commit de7c497

Please sign in to comment.