-
Notifications
You must be signed in to change notification settings - Fork 20
/
AtReader.cs
169 lines (150 loc) · 5.67 KB
/
AtReader.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Buffers;
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace HeboTech.ATLib.Parsers
{
public class AtReader : IAtReader, IDisposable
{
private static readonly byte[] eolSequence = new byte[] { (byte)'\r', (byte)'\n' };
private static readonly byte[] smsPromptSequence = new byte[] { (byte)'>', (byte)' ' };
private bool isDisposed;
private PipeReader pipe;
private Channel<string> channel;
private Task reader;
private CancellationTokenSource cancellationTokenSource;
public AtReader(Stream inputStream)
{
cancellationTokenSource = new CancellationTokenSource();
pipe = PipeReader.Create(inputStream);
channel = Channel.CreateUnbounded<string>();
}
public void Open()
{
reader = Task.Factory.StartNew(() => ReadPipeAsync(pipe, cancellationTokenSource.Token), TaskCreationOptions.LongRunning);
}
public void Close()
{
Dispose();
}
/// <summary>
/// Gets the current number of items available
/// </summary>
/// <returns></returns>
public int AvailableItems()
{
return channel.Reader.Count;
}
public ValueTask<string> ReadAsync(CancellationToken cancellationToken = default)
{
return channel.Reader.ReadAsync(cancellationToken);
}
private async Task ReadPipeAsync(PipeReader reader, CancellationToken cancellationToken = default)
{
while (!cancellationToken.IsCancellationRequested)
{
ReadResult result;
try
{
result = await reader.ReadAsync(cancellationToken);
}
catch (OperationCanceledException)
{
break;
}
ReadOnlySequence<byte> buffer = result.Buffer;
string line;
while (TryReadLine(ref buffer, out line))
{
try
{
await channel.Writer.WriteAsync(line, cancellationToken);
}
catch (OperationCanceledException)
{
break;
}
}
// Tell the PipeReader how much of the buffer has been consumed.
reader.AdvanceTo(buffer.Start, buffer.End);
// Stop reading if there's no more data coming.
if (result.IsCompleted)
{
break;
}
}
// Mark the PipeReader as complete.
await reader.CompleteAsync();
}
private static bool TryReadLine(ref ReadOnlySequence<byte> buffer, out string line)
{
SequenceReader<byte> eolReader = new SequenceReader<byte>(buffer);
SequenceReader<byte> smsReader = new SequenceReader<byte>(buffer);
bool eolSuccess = eolReader.TryReadTo(out ReadOnlySequence<byte> eolSlice, eolSequence.AsSpan(), advancePastDelimiter: true);
bool smsSuccess = smsReader.TryReadTo(out ReadOnlySequence<byte> smsSlice, smsPromptSequence.AsSpan(), advancePastDelimiter: true);
if (eolSuccess && smsSuccess)
{
if (eolSlice.Length == smsSlice.Length)
throw new Exception("Conflicting line endings");
if (eolSlice.Length < smsSlice.Length)
smsSuccess = false;
else
eolSuccess = false;
}
if (eolSuccess)
{
string temp = Encoding.ASCII.GetString(eolSlice.ToArray());
buffer = buffer.Slice(eolReader.Position);
line = temp;
return true;
}
else if (smsSuccess)
{
string temp = Encoding.ASCII.GetString(smsPromptSequence); // Return the SMS prompt sequence instead of an empty string (the sequence is consumed by the reader).
buffer = buffer.Slice(smsReader.Position);
line = temp;
return true;
}
line = default;
return false;
}
#region Dispose
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
cancellationTokenSource.Cancel();
reader?.Wait();
reader.Dispose();
reader = null;
channel = null;
pipe = null;
cancellationTokenSource.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
isDisposed = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~AtReader()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
#endregion
}
}