-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBObject.cs
More file actions
105 lines (94 loc) · 4.05 KB
/
BObject.cs
File metadata and controls
105 lines (94 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.IO;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
namespace BencodeNET.Objects
{
/// <summary>
/// Abstract base class with default implementation of most methods of <see cref="IBObject"/>.
/// </summary>
public abstract class BObject : IBObject
{
internal BObject()
{ }
/// <summary>
/// Calculates the (encoded) size of the object in bytes.
/// </summary>
public abstract int GetSizeInBytes();
/// <summary>
/// Writes the object as bencode to the specified stream.
/// </summary>
/// <typeparam name="TStream">The type of stream.</typeparam>
/// <param name="stream">The stream to write to.</param>
/// <returns>The used stream.</returns>
public TStream EncodeTo<TStream>(TStream stream) where TStream : Stream
{
var size = GetSizeInBytes();
stream.TrySetLength(size);
EncodeObject(stream);
return stream;
}
/// <summary>
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> without flushing the writer,
/// you should do that manually.
/// </summary>
/// <param name="writer">The writer to write to.</param>
public void EncodeTo(PipeWriter writer)
{
EncodeObject(writer);
}
/// <summary>
/// Writes the object as bencode to the specified <see cref="PipeWriter"/> and flushes the writer afterwards.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="cancellationToken"></param>
public ValueTask<FlushResult> EncodeToAsync(PipeWriter writer, CancellationToken cancellationToken = default)
{
return EncodeObjectAsync(writer, cancellationToken);
}
/// <summary>
/// Writes the object asynchronously as bencode to the specified <see cref="Stream"/> using a <see cref="PipeWriter"/>.
/// </summary>
/// <param name="stream">The stream to write to.</param>
/// <param name="writerOptions">The options for the <see cref="PipeWriter"/>.</param>
/// <param name="cancellationToken"></param>
public ValueTask<FlushResult> EncodeToAsync(Stream stream, StreamPipeWriterOptions writerOptions = null, CancellationToken cancellationToken = default)
{
return EncodeObjectAsync(PipeWriter.Create(stream, writerOptions), cancellationToken);
}
/// <summary>
/// Implementations of this method should encode their
/// underlying value to bencode and write it to the stream.
/// </summary>
/// <param name="stream">The stream to encode to.</param>
protected abstract void EncodeObject(Stream stream);
/// <summary>
/// Implementations of this method should encode their underlying value to bencode and write it to the <see cref="PipeWriter"/>.
/// </summary>
/// <param name="writer">The writer to encode to.</param>
protected abstract void EncodeObject(PipeWriter writer);
/// <summary>
/// Encodes and writes the underlying value to the <see cref="PipeWriter"/> and flushes the writer afterwards.
/// </summary>
/// <param name="writer">The writer to encode to.</param>
/// <param name="cancellationToken"></param>
protected virtual ValueTask<FlushResult> EncodeObjectAsync(PipeWriter writer, CancellationToken cancellationToken)
{
EncodeObject(writer);
return writer.FlushAsync(cancellationToken);
}
}
/// <summary>
/// Base class of bencode objects with a specific underlying value type.
/// </summary>
/// <typeparam name="T">Type of the underlying value.</typeparam>
public abstract class BObject<T> : BObject
{
internal BObject()
{ }
/// <summary>
/// The underlying value of the <see cref="BObject{T}"/>.
/// </summary>
public abstract T Value { get; }
}
}