-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathByteStreams.cs
69 lines (61 loc) · 1.87 KB
/
ByteStreams.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
using System;
using System.Threading.Tasks;
namespace Http2
{
/// <summary>
/// The result of a ReadAsync operation
/// </summary>
public struct StreamReadResult
{
/// <summary>
/// The amount of bytes that were read
/// </summary>
public int BytesRead;
/// <summary>
/// Whether the end of the stream was reached
/// In this case no bytes should be read
/// </summary>
public bool EndOfStream;
}
public interface IReadableByteStream
{
/// <summary>
/// Reads data from a stream into the given buffer segment.
/// The amound of bytes that will be read is up to the given buffer length
/// The return value signals how many bytes were actually read.
/// </summary>
ValueTask<StreamReadResult> ReadAsync(ArraySegment<byte> buffer);
}
public interface IWriteableByteStream
{
/// <summary>
/// Writes the buffer to the stream.
/// </summary>
Task WriteAsync(ArraySegment<byte> buffer);
}
public interface ICloseableByteStream
{
/// <summary>
/// Closes the stream gracefully.
/// This should signal EndOfStream to the receiving side once all prior
/// data has been read.
/// </summary>
Task CloseAsync();
}
public interface IWriteAndCloseableByteStream
: IWriteableByteStream, ICloseableByteStream
{
}
/// <summary>
/// A marker class that is used to signal the completion of an Async operation.
/// This purely exists since ValueTask<Void> is not valid in C#.
/// </summary>
public class DoneHandle
{
private DoneHandle() {}
/// <summary>
/// A static instance of the Handle
/// </summary>
public static readonly DoneHandle Instance = new DoneHandle();
}
}