Skip to content

Commit

Permalink
add IChunkWebSocketUploader and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 7, 2024
1 parent 5dcd8d1 commit 7e98c47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/BeeNet.Client/BeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -547,7 +548,7 @@ public async Task<Stream> GetChunkStreamAsync(
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public async Task<ChunkUploaderWebSocket> GetChunkUploaderWebSocketAsync(
public async Task<IChunkWebSocketUploader> GetChunkUploaderWebSocketAsync(
PostageBatchId batchId,
TagId? tagId = null,
CancellationToken cancellationToken = default)
Expand All @@ -561,7 +562,7 @@ public async Task<ChunkUploaderWebSocket> GetChunkUploaderWebSocketAsync(
ChunkStreamWSReceiveBufferSize,
ChunkStreamWSSendBufferSize,
cancellationToken).ConfigureAwait(false);
return new ChunkUploaderWebSocket(webSocket);
return new ChunkWebSocketUploader(webSocket);
}

public async Task<BzzBalance> GetConsumedBalanceWithPeerAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
// You should have received a copy of the GNU Lesser General Public License along with Bee.Net.
// If not, see <https://www.gnu.org/licenses/>.

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
Expand Down Expand Up @@ -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);
}
}
}
39 changes: 39 additions & 0 deletions src/BeeNet.Core/Tools/IChunkWebSocketUploader.cs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

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);
}
}
3 changes: 2 additions & 1 deletion src/BeeNet.Util/IBeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -353,7 +354,7 @@ Task<Stream> GetChunkStreamAsync(
/// <br/>Warning! Not available for nodes that run in Gateway mode!</param>
/// <returns>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.</returns>
/// <exception cref="BeeNetGatewayApiException">A server side error occurred.</exception>
Task<ChunkUploaderWebSocket> GetChunkUploaderWebSocketAsync(
Task<IChunkWebSocketUploader> GetChunkUploaderWebSocketAsync(
PostageBatchId batchId,
TagId? tagId = null,
CancellationToken cancellationToken = default);
Expand Down

0 comments on commit 7e98c47

Please sign in to comment.