From 7e98c4765a83e689dd3d5a13b04215b21b26d3bf Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 7 Oct 2024 19:56:40 +0200 Subject: [PATCH] add IChunkWebSocketUploader and refactor --- src/BeeNet.Client/BeeClient.cs | 5 ++- .../ChunkUploaderWebSocket.cs | 19 +++++++-- .../Tools/IChunkWebSocketUploader.cs | 39 +++++++++++++++++++ src/BeeNet.Util/IBeeClient.cs | 3 +- 4 files changed, 59 insertions(+), 7 deletions(-) rename src/BeeNet.Core/{Models => Tools}/ChunkUploaderWebSocket.cs (82%) create mode 100644 src/BeeNet.Core/Tools/IChunkWebSocketUploader.cs diff --git a/src/BeeNet.Client/BeeClient.cs b/src/BeeNet.Client/BeeClient.cs index 6824936..34de140 100644 --- a/src/BeeNet.Client/BeeClient.cs +++ b/src/BeeNet.Client/BeeClient.cs @@ -18,6 +18,7 @@ using Etherna.BeeNet.Manifest; using Etherna.BeeNet.Models; using Etherna.BeeNet.Services; +using Etherna.BeeNet.Tools; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -547,7 +548,7 @@ public async Task GetChunkStreamAsync( } [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] - public async Task GetChunkUploaderWebSocketAsync( + public async Task GetChunkUploaderWebSocketAsync( PostageBatchId batchId, TagId? tagId = null, CancellationToken cancellationToken = default) @@ -561,7 +562,7 @@ public async Task GetChunkUploaderWebSocketAsync( ChunkStreamWSReceiveBufferSize, ChunkStreamWSSendBufferSize, cancellationToken).ConfigureAwait(false); - return new ChunkUploaderWebSocket(webSocket); + return new ChunkWebSocketUploader(webSocket); } public async Task GetConsumedBalanceWithPeerAsync( diff --git a/src/BeeNet.Core/Models/ChunkUploaderWebSocket.cs b/src/BeeNet.Core/Tools/ChunkUploaderWebSocket.cs similarity index 82% rename from src/BeeNet.Core/Models/ChunkUploaderWebSocket.cs rename to src/BeeNet.Core/Tools/ChunkUploaderWebSocket.cs index 6b4f57f..a1ffd3b 100644 --- a/src/BeeNet.Core/Models/ChunkUploaderWebSocket.cs +++ b/src/BeeNet.Core/Tools/ChunkUploaderWebSocket.cs @@ -12,17 +12,17 @@ // You should have received a copy of the GNU Lesser General Public License along with Bee.Net. // If not, see . +using Etherna.BeeNet.Models; using System; -using System.Linq; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; -namespace Etherna.BeeNet.Models +namespace Etherna.BeeNet.Tools { - public sealed class ChunkUploaderWebSocket( + public sealed class ChunkWebSocketUploader( WebSocket webSocket) - : IDisposable + : IChunkWebSocketUploader { // Fields. private readonly byte[] responseBuffer = new byte[SwarmHash.HashSize]; //not really used @@ -69,5 +69,16 @@ public Task SendChunkAsync( ArgumentNullException.ThrowIfNull(chunk, nameof(chunk)); return SendChunkAsync(chunk.GetSpanAndData(), cancellationToken); } + + public async Task SendChunkBatchAsync( + SwarmChunk[] chunkBatch, + bool isLastBatch, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(chunkBatch, nameof(chunkBatch)); + + foreach (var chunk in chunkBatch) + await SendChunkAsync(chunk, cancellationToken).ConfigureAwait(false); + } } } \ No newline at end of file diff --git a/src/BeeNet.Core/Tools/IChunkWebSocketUploader.cs b/src/BeeNet.Core/Tools/IChunkWebSocketUploader.cs new file mode 100644 index 0000000..e55485d --- /dev/null +++ b/src/BeeNet.Core/Tools/IChunkWebSocketUploader.cs @@ -0,0 +1,39 @@ +// Copyright 2021-present Etherna SA +// This file is part of Bee.Net. +// +// Bee.Net is free software: you can redistribute it and/or modify it under the terms of the +// GNU Lesser General Public License as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// Bee.Net is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along with Bee.Net. +// If not, see . + +using Etherna.BeeNet.Models; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Etherna.BeeNet.Tools +{ + public interface IChunkWebSocketUploader : IDisposable + { + Task CloseAsync(); + + Task SendChunkAsync( + byte[] chunkPayload, + CancellationToken cancellationToken); + + Task SendChunkAsync( + SwarmChunk chunk, + CancellationToken cancellationToken); + + Task SendChunkBatchAsync( + SwarmChunk[] chunkBatch, + bool isLastBatch, + CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/src/BeeNet.Util/IBeeClient.cs b/src/BeeNet.Util/IBeeClient.cs index f14b23a..dcb758f 100644 --- a/src/BeeNet.Util/IBeeClient.cs +++ b/src/BeeNet.Util/IBeeClient.cs @@ -14,6 +14,7 @@ using Etherna.BeeNet.Exceptions; using Etherna.BeeNet.Models; +using Etherna.BeeNet.Tools; using System; using System.Collections.Generic; using System.IO; @@ -353,7 +354,7 @@ Task GetChunkStreamAsync( ///
Warning! Not available for nodes that run in Gateway mode! /// Returns a Websocket connection on which stream of chunks can be uploaded. Each chunk sent is acknowledged using a binary response `0` which serves as confirmation of upload of single chunk. Chunks should be packaged as binary messages for uploading. /// A server side error occurred. - Task GetChunkUploaderWebSocketAsync( + Task GetChunkUploaderWebSocketAsync( PostageBatchId batchId, TagId? tagId = null, CancellationToken cancellationToken = default);