-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBencodeParser.cs
More file actions
140 lines (124 loc) · 5.17 KB
/
BencodeParser.cs
File metadata and controls
140 lines (124 loc) · 5.17 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BencodeNET.Exceptions;
using BencodeNET.IO;
using BencodeNET.Objects;
using BencodeNET.Torrents;
namespace BencodeNET.Parsing
{
/// <summary>
/// Main class used for parsing bencode.
/// </summary>
public class BencodeParser : IBencodeParser
{
/// <summary>
/// List of parsers used by the <see cref="BencodeParser"/>.
/// </summary>
public BObjectParserList Parsers { get; }
/// <summary>
/// The encoding use for parsing.
/// </summary>
public Encoding Encoding
{
get => _encoding;
set
{
_encoding = value ?? throw new ArgumentNullException(nameof(value));
Parsers.GetSpecific<BStringParser>()?.ChangeEncoding(value);
}
}
private Encoding _encoding;
/// <summary>
/// Creates an instance using the specified encoding and the default parsers.
/// Encoding defaults to <see cref="System.Text.Encoding.UTF8"/> if not specified.
/// </summary>
/// <param name="encoding">The encoding to use when parsing.</param>
public BencodeParser(Encoding encoding = null)
{
_encoding = encoding ?? Encoding.UTF8;
Parsers = new BObjectParserList
{
new BNumberParser(),
new BStringParser(_encoding),
new BListParser(this),
new BDictionaryParser(this),
new TorrentParser(this)
};
}
/// <summary>
/// Parses an <see cref="IBObject"/> from the reader.
/// </summary>
public virtual IBObject Parse(BencodeReader reader)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
switch (reader.PeekChar())
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': return Parse<BString>(reader);
case 'i': return Parse<BNumber>(reader);
case 'l': return Parse<BList>(reader);
case 'd': return Parse<BDictionary>(reader);
case default(char): return null;
}
throw InvalidBencodeException<IBObject>.InvalidBeginningChar(reader.PeekChar(), reader.Position);
}
/// <summary>
/// Parse an <see cref="IBObject"/> of type <typeparamref name="T"/> from the reader.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
public virtual T Parse<T>(BencodeReader reader) where T : class, IBObject
{
var parser = Parsers.Get<T>();
if (parser == null)
throw new BencodeException($"Missing parser for the type '{typeof(T).FullName}'. Stream position: {reader.Position}");
return parser.Parse(reader);
}
/// <summary>
/// Parse an <see cref="IBObject"/> from the <see cref="PipeBencodeReader"/>.
/// </summary>
public virtual async ValueTask<IBObject> ParseAsync(PipeBencodeReader pipeReader, CancellationToken cancellationToken = default)
{
if (pipeReader == null) throw new ArgumentNullException(nameof(pipeReader));
switch (await pipeReader.PeekCharAsync(cancellationToken).ConfigureAwait(false))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': return await ParseAsync<BString>(pipeReader, cancellationToken).ConfigureAwait(false);
case 'i': return await ParseAsync<BNumber>(pipeReader, cancellationToken).ConfigureAwait(false);
case 'l': return await ParseAsync<BList>(pipeReader, cancellationToken).ConfigureAwait(false);
case 'd': return await ParseAsync<BDictionary>(pipeReader, cancellationToken).ConfigureAwait(false);
case default(char): return null;
}
throw InvalidBencodeException<IBObject>.InvalidBeginningChar(
await pipeReader.PeekCharAsync(cancellationToken).ConfigureAwait(false),
pipeReader.Position);
}
/// <summary>
/// Parse an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeBencodeReader"/>.
/// </summary>
public virtual async ValueTask<T> ParseAsync<T>(PipeBencodeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject
{
var parser = Parsers.Get<T>();
if (parser == null)
throw new BencodeException($"Missing parser for the type '{typeof(T).FullName}'. Stream position: {pipeReader.Position}");
return await parser.ParseAsync(pipeReader, cancellationToken).ConfigureAwait(false);
}
}
}