Skip to content

Commit caec437

Browse files
committed
Implement both sync/async versions of IBObject.EncodeTo()
1 parent e999bd7 commit caec437

12 files changed

Lines changed: 132 additions & 24 deletions

File tree

BencodeNET.Tests/Objects/BDictionaryTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,20 @@ public async Task WriteToPipeWriter()
404404
var dict = new BDictionary { { "key", "value" } };
405405
var (reader, writer) = new Pipe();
406406

407+
dict.EncodeTo(writer);
408+
await writer.FlushAsync();
409+
reader.TryRead(out var readResult);
410+
411+
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
412+
result.Should().Be("d3:key5:valuee");
413+
}
414+
415+
[Fact]
416+
public async Task WriteToPipeWriterAsync()
417+
{
418+
var dict = new BDictionary { { "key", "value" } };
419+
var (reader, writer) = new Pipe();
420+
407421
await dict.EncodeToAsync(writer);
408422
reader.TryRead(out var readResult);
409423

BencodeNET.Tests/Objects/BListTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ public async Task WriteToPipeWriter()
217217
var blist = new BList { 1, 2, "abc" };
218218
var (reader, writer) = new Pipe();
219219

220+
blist.EncodeTo(writer);
221+
await writer.FlushAsync();
222+
reader.TryRead(out var readResult);
223+
224+
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
225+
result.Should().Be("li1ei2e3:abce");
226+
}
227+
228+
[Fact]
229+
public async Task WriteToPipeWriterAsync()
230+
{
231+
var blist = new BList { 1, 2, "abc" };
232+
var (reader, writer) = new Pipe();
233+
220234
await blist.EncodeToAsync(writer);
221235
reader.TryRead(out var readResult);
222236

BencodeNET.Tests/Objects/BNumberTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,20 @@ public async Task WriteToPipeWriter()
349349
var bnumber = new BNumber(1234);
350350
var (reader, writer) = new Pipe();
351351

352+
bnumber.EncodeTo(writer);
353+
await writer.FlushAsync();
354+
reader.TryRead(out var readResult);
355+
356+
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
357+
result.Should().Be("i1234e");
358+
}
359+
360+
[Fact]
361+
public async Task WriteToPipeWriterAsync()
362+
{
363+
var bnumber = new BNumber(1234);
364+
var (reader, writer) = new Pipe();
365+
352366
await bnumber.EncodeToAsync(writer);
353367
reader.TryRead(out var readResult);
354368

BencodeNET.Tests/Objects/BStringTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ public async Task WriteToPipeWriter()
289289
var bstring = new BString("æøå äö èéê ñ");
290290
var (reader, writer) = new Pipe();
291291

292+
bstring.EncodeTo(writer);
293+
await writer.FlushAsync();
294+
reader.TryRead(out var readResult);
295+
296+
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
297+
result.Should().Be("21:æøå äö èéê ñ");
298+
}
299+
300+
[Fact]
301+
public async Task WriteToPipeWriterAsync()
302+
{
303+
var bstring = new BString("æøå äö èéê ñ");
304+
var (reader, writer) = new Pipe();
305+
292306
await bstring.EncodeToAsync(writer);
293307
reader.TryRead(out var readResult);
294308

BencodeNET/Objects/BDictionary.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,31 @@ protected override void EncodeObject(Stream stream)
157157
}
158158

159159
/// <inheritdoc/>
160-
protected override async ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken = default)
160+
protected override void EncodeObject(PipeWriter writer)
161161
{
162-
await writer.WriteCharAsync('d', cancellationToken);
162+
writer.WriteChar('d');
163+
foreach (var entry in this)
164+
{
165+
entry.Key.EncodeTo(writer);
166+
entry.Value.EncodeTo(writer);
167+
}
163168

169+
writer.WriteChar('e');
170+
}
171+
172+
/// <inheritdoc/>
173+
protected override async ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken)
174+
{
175+
writer.WriteChar('d');
164176
foreach (var entry in this)
165177
{
166-
await entry.Key.EncodeToAsync(writer, cancellationToken);
167-
await entry.Value.EncodeToAsync(writer, cancellationToken);
178+
cancellationToken.ThrowIfCancellationRequested();
179+
await entry.Key.EncodeToAsync(writer, cancellationToken).ConfigureAwait(false);
180+
await entry.Value.EncodeToAsync(writer, cancellationToken).ConfigureAwait(false);
168181
}
182+
writer.WriteChar('e');
169183

170-
return await writer.WriteCharAsync('e', cancellationToken);
184+
return await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
171185
}
172186

173187
#region IDictionary<BString, IBObject> Members

BencodeNET/Objects/BList.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,28 @@ protected override void EncodeObject(Stream stream)
176176
}
177177

178178
/// <inheritdoc/>
179-
protected override async ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken = default)
179+
protected override void EncodeObject(PipeWriter writer)
180180
{
181-
await writer.WriteCharAsync('l', cancellationToken);
181+
writer.WriteChar('l');
182+
for (var i = 0; i < this.Count; i++)
183+
{
184+
this[i].EncodeTo(writer);
185+
}
186+
writer.WriteChar('e');
187+
}
182188

189+
/// <inheritdoc/>
190+
protected override async ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken)
191+
{
192+
writer.WriteChar('l');
183193
for (var i = 0; i < this.Count; i++)
184194
{
185-
await this[i].EncodeToAsync(writer, cancellationToken);
195+
cancellationToken.ThrowIfCancellationRequested();
196+
await this[i].EncodeToAsync(writer, cancellationToken).ConfigureAwait(false);
186197
}
198+
writer.WriteChar('e');
187199

188-
return await writer.WriteCharAsync('e', cancellationToken);
200+
return await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
189201
}
190202

191203
#region IList<IBObject> Members

BencodeNET/Objects/BNumber.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.IO;
33
using System.IO.Pipelines;
44
using System.Text;
5-
using System.Threading;
6-
using System.Threading.Tasks;
75

86
namespace BencodeNET.Objects
97
{
@@ -58,7 +56,7 @@ protected override void EncodeObject(Stream stream)
5856
}
5957

6058
/// <inheritdoc/>
61-
protected override ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken = default)
59+
protected override void EncodeObject(PipeWriter writer)
6260
{
6361
var size = GetSizeInBytes();
6462
var buffer = writer.GetSpan(size).Slice(0, size);
@@ -75,7 +73,6 @@ protected override ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, C
7573
buffer[buffer.Length - 1] = (byte) 'e';
7674

7775
writer.Advance(size);
78-
return writer.FlushAsync(cancellationToken);
7976
}
8077

8178
#pragma warning disable 1591

BencodeNET/Objects/BObject.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.IO.Pipelines;
43
using System.Threading;
54
using System.Threading.Tasks;
@@ -34,7 +33,17 @@ public TStream EncodeTo<TStream>(TStream stream) where TStream : Stream
3433
}
3534

3635
/// <summary>
37-
/// Writes the object as bencode to the specified <see cref="PipeWriter"/>.
36+
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> without flushing the writer,
37+
/// you should do that manually.
38+
/// </summary>
39+
/// <param name="writer">The writer to write to.</param>
40+
public void EncodeTo(PipeWriter writer)
41+
{
42+
EncodeObject(writer);
43+
}
44+
45+
/// <summary>
46+
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> and flushes the writer afterwards.
3847
/// </summary>
3948
/// <param name="writer">The writer to write to.</param>
4049
/// <param name="cancellationToken"></param>
@@ -54,8 +63,18 @@ public ValueTask<FlushResult> EncodeToAsync(PipeWriter writer, CancellationToken
5463
/// Implementations of this method should encode their underlying value to bencode and write it to the <see cref="PipeWriter"/>.
5564
/// </summary>
5665
/// <param name="writer">The writer to encode to.</param>
66+
protected abstract void EncodeObject(PipeWriter writer);
67+
68+
/// <summary>
69+
/// Encodes and writes the underlying value to the <see cref="PipeWriter"/> and flushes the writer afterwards.
70+
/// </summary>
71+
/// <param name="writer">The writer to encode to.</param>
5772
/// <param name="cancellationToken"></param>
58-
protected abstract ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken = default);
73+
protected virtual ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken)
74+
{
75+
EncodeObject(writer);
76+
return writer.FlushAsync(cancellationToken);
77+
}
5978
}
6079

6180
/// <summary>

BencodeNET/Objects/BString.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.IO.Pipelines;
55
using System.Linq;
66
using System.Text;
7-
using System.Threading;
8-
using System.Threading.Tasks;
97

108
namespace BencodeNET.Objects
119
{
@@ -94,7 +92,7 @@ protected override void EncodeObject(Stream stream)
9492
}
9593

9694
/// <inheritdoc/>
97-
protected override ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken = default)
95+
protected override void EncodeObject(PipeWriter writer)
9896
{
9997
// Init
10098
var size = GetSizeInBytes();
@@ -123,7 +121,6 @@ protected override ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, C
123121

124122
// Commit
125123
writer.Advance(size);
126-
return writer.FlushAsync(cancellationToken);
127124
}
128125

129126
#pragma warning disable 1591

BencodeNET/Objects/IBObject.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ public interface IBObject
2424
TStream EncodeTo<TStream>(TStream stream) where TStream : Stream;
2525

2626
/// <summary>
27-
/// Writes the object as bencode to the specified <see cref="PipeWriter"/>.
27+
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> without flushing the writer,
28+
/// you should do that manually.
29+
/// </summary>
30+
/// <param name="writer">The writer to write to.</param>
31+
void EncodeTo(PipeWriter writer);
32+
33+
/// <summary>
34+
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> and flushes the writer afterwards.
2835
/// </summary>
2936
/// <param name="writer">The writer to write to.</param>
3037
/// <param name="cancellationToken"></param>

0 commit comments

Comments
 (0)