Skip to content

Commit

Permalink
refactor ChunkUploaderWebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 7, 2024
1 parent 04bbec1 commit 5dcd8d1
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions src/BeeNet.Core/Models/ChunkUploaderWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,19 @@

namespace Etherna.BeeNet.Models
{
public class ChunkUploaderWebSocket(
public sealed class ChunkUploaderWebSocket(
WebSocket webSocket)
: IDisposable
{
// Fields.
private readonly byte[] responseBuffer = new byte[SwarmHash.HashSize]; //not really used

// Dispose.
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
webSocket.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose() =>
webSocket.Dispose();

// Methods.
public virtual async Task CloseAsync()
public async Task CloseAsync()
{
if (webSocket.State == WebSocketState.Open)
{
Expand All @@ -58,7 +48,7 @@ await webSocket.CloseOutputAsync(
}
}

public virtual async Task SendChunkAsync(
public async Task SendChunkAsync(
byte[] chunkPayload,
CancellationToken cancellationToken)
{
Expand All @@ -72,22 +62,12 @@ public virtual async Task SendChunkAsync(
$"Connection closed by server, message: {response.CloseStatusDescription}");
}

public virtual Task SendChunkAsync(
public Task SendChunkAsync(
SwarmChunk chunk,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(chunk, nameof(chunk));
return SendChunkAsync(chunk.GetSpanAndData(), cancellationToken);
}

public virtual async Task SendChunksAsync(
SwarmChunk[] chunkBatch,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(chunkBatch, nameof(chunkBatch));

foreach (var (chunk, i) in chunkBatch.Select((c, i) => (c, i)))
await SendChunkAsync(chunk, cancellationToken).ConfigureAwait(false);
}
}
}

0 comments on commit 5dcd8d1

Please sign in to comment.