Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
josephnhtam committed Apr 2, 2024
1 parent fd0663f commit 79debed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/LiveStreamingServerNet.Networking/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public async Task RunAsync(IClientHandler handler, ServerEndPoint serverEndPoint
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { }
catch (Exception ex) when (ex is IOException or EndOfStreamException) { }
catch (Exception)
catch (Exception ex)
{
throw;
_logger.ClientLoopError(ClientId, ex);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace LiveStreamingServerNet.Rtmp.Logging
{
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Error, "ClientId: {ClientId} | An error occurred in the client client loop")]
public static partial void ClientLoopError(this ILogger logger, uint clientId, Exception exception);

[LoggerMessage(LogLevel.Debug, "ClientId: {ClientId} | Command ({commandName}) received")]
public static partial void CommandReceived(this ILogger logger, uint clientId, string commandName);

Expand Down
28 changes: 18 additions & 10 deletions src/LiveStreamingServerNet.Rtmp/Internal/RtmpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,29 @@ public async Task InitializeAsync(IClientHandle client)

public async Task<bool> HandleClientLoopAsync(ReadOnlyStream networkStream, CancellationToken cancellationToken)
{
var result = _clientContext.State switch
try
{
RtmpClientState.HandshakeC0 => await HandleHandshakeC0(_clientContext, networkStream, cancellationToken),
RtmpClientState.HandshakeC1 => await HandleHandshakeC1(_clientContext, networkStream, cancellationToken),
RtmpClientState.HandshakeC2 => await HandleHandshakeC2(_clientContext, networkStream, cancellationToken),
_ => await HandleChunkAsync(_clientContext, networkStream, cancellationToken),
};
var result = _clientContext.State switch
{
RtmpClientState.HandshakeC0 => await HandleHandshakeC0(_clientContext, networkStream, cancellationToken),
RtmpClientState.HandshakeC1 => await HandleHandshakeC1(_clientContext, networkStream, cancellationToken),
RtmpClientState.HandshakeC2 => await HandleHandshakeC2(_clientContext, networkStream, cancellationToken),
_ => await HandleChunkAsync(_clientContext, networkStream, cancellationToken),
};

if (result.Succeeded && _bandwidthLimiter != null && !_bandwidthLimiter.ConsumeBandwidth(result.ConsumedBytes))
if (result.Succeeded && _bandwidthLimiter != null && !_bandwidthLimiter.ConsumeBandwidth(result.ConsumedBytes))
{
_logger.ExceededBandwidthLimit(_clientContext.Client.ClientId);
return false;
}

return result.Succeeded;
}
catch (Exception ex)
{
_logger.ExceededBandwidthLimit(_clientContext.Client.ClientId);
_logger.ClientLoopError(_clientContext.Client.ClientId, ex);
return false;
}

return result.Succeeded;
}

private async Task<RtmpEventConsumingResult> HandleHandshakeC0(IRtmpClientContext clientContext, ReadOnlyStream networkStream, CancellationToken cancellationToken)
Expand Down

0 comments on commit 79debed

Please sign in to comment.