4using System.Threading.Tasks;
10 internal class ClientChunkRecord : ChunkRecord
12 private readonly HttpxClient client;
13 private readonly HttpxResponseEventArgs e;
14 private readonly
object state;
15 private readonly
string streamId;
16 private readonly
string from;
17 private readonly
string to;
18 private readonly
string endpointReference;
19 private readonly
bool e2e;
20 private readonly EventHandlerAsync<HttpxResponseDataEventArgs> dataCallback;
21 private readonly IE2eSymmetricCipher symmetricCipher;
22 private readonly SemaphoreSlim synchObj =
new SemaphoreSlim(1);
23 private SortedDictionary<int, Chunk> chunks =
null;
25 private int nextChunk = 0;
26 private bool disposed =
false;
28 internal ClientChunkRecord(HttpxClient Client, HttpxResponseEventArgs e,
HttpResponse Response,
29 EventHandlerAsync<HttpxResponseDataEventArgs> DataCallback,
object State,
string StreamId,
string From,
string To,
bool E2e,
30 string EndpointReference, IE2eSymmetricCipher SymmetricCipher)
35 this.response = Response;
36 this.dataCallback = DataCallback;
38 this.streamId = StreamId;
42 this.endpointReference = EndpointReference;
43 this.symmetricCipher = SymmetricCipher;
46 public string From => this.from;
47 public string To => this.to;
48 public bool E2e => this.e2e;
49 public string EndpointReference => this.endpointReference;
50 public IE2eSymmetricCipher SymmetricCipher => this.symmetricCipher;
52 internal override async Task<bool> ChunkReceived(
int Nr,
bool Last,
bool ConstantBuffer,
byte[] Data)
55 throw new ObjectDisposedException(nameof(ClientChunkRecord));
57 await this.synchObj.WaitAsync();
60 if (Nr == this.nextChunk)
62 if (Data.Length > 0 || Last)
64 HttpxResponseDataEventArgs e =
new HttpxResponseDataEventArgs(
null, ConstantBuffer, Data, this.streamId, Last, this.state);
65 if (!await this.dataCallback.Raise(
this.client, e,
false))
67 await this.client.CancelTransfer(this.e.From,
this.streamId);
75 await this.DoneLocked();
78 while (!(this.chunks is
null))
80 if (this.chunks.Count == 0)
84 foreach (Chunk Chunk
in this.chunks.Values)
86 if (Chunk.Nr ==
this.nextChunk)
88 HttpxResponseDataEventArgs e =
new HttpxResponseDataEventArgs(
null, Chunk.ConstantBuffer, Chunk.Data,
this.streamId, Chunk.Last,
this.state);
89 if (!await this.dataCallback.Raise(
this.client, e,
false))
93 this.chunks.Remove(Chunk.Nr);
96 await this.DoneLocked();
107 else if (Nr > this.nextChunk)
109 this.chunks ??=
new SortedDictionary<int, Chunk>();
110 this.chunks[Nr] =
new Chunk(Nr, Last, ConstantBuffer, Data);
117 this.synchObj.Release();
121 private async Task DoneLocked()
123 if (!(this.response is
null))
135 this.chunks?.Clear();
138 internal override async Task Fail(
string Message)
141 throw new ObjectDisposedException(nameof(ClientChunkRecord));
143 await this.synchObj.WaitAsync();
146 if (this.response is
null)
149 HttpxResponseDataEventArgs e =
new HttpxResponseDataEventArgs(
null,
true, Array.Empty<
byte>(),
this.streamId,
true,
this.state);
150 await this.dataCallback.Raise(this.client, e,
false);
155 await this.client.CancelTransfer(this.e.From,
this.streamId);
157 await this.DoneLocked();
161 this.synchObj.Release();
165 public override async Task DisposeAsync()
169 this.disposed =
true;
171 await this.synchObj.WaitAsync();
174 if (!(this.response is
null))
177 this.response =
null;
180 this.chunks?.Clear();
185 this.synchObj.Dispose();
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Represets a response of an HTTP client request.
async Task DisposeAsync()
Closes the connection and disposes of all resources.
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
bool HeaderSent
If the header has been sent.
The server encountered an unexpected condition which prevented it from fulfilling the request.