Sane C++ Libraries
C++ Platform Abstraction Libraries
AsyncRequestStreams.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Async/Async.h"
5#include "AsyncStreams.h"
6
9namespace SC
10{
11template <typename AsyncRequestType>
13{
15
18
19 AsyncRequestType request;
20
21 protected:
22 struct Internal;
23
24 Result read();
25
26 void afterRead(typename AsyncRequestType::Result& result, AsyncBufferView::ID bufferID);
27 void onEndCloseDescriptor();
28};
29
30template <typename AsyncRequestType>
32{
34
35 template <typename DescriptorType>
36 Result init(AsyncBuffersPool& buffersPool, Span<Request> requests, AsyncEventLoop& loop,
37 const DescriptorType& descriptor);
38
41
42 AsyncRequestType request;
43
44 protected:
45 struct Internal;
46
47 Function<void(AsyncBufferView::ID)> callback;
48
49 Result write(AsyncBufferView::ID bufferID, Function<void(AsyncBufferView::ID)> cb);
50
51 void onEndCloseDescriptor();
52};
53
55struct ReadableFileStream : public AsyncRequestReadableStream<AsyncFileRead>
56{
57 Result init(AsyncBuffersPool& buffersPool, Span<Request> requests, AsyncEventLoop& loop,
58 const FileDescriptor& descriptor);
59};
60
62struct WritableFileStream : public AsyncRequestWritableStream<AsyncFileWrite>
63{
64 Result init(AsyncBuffersPool& buffersPool, Span<Request> requests, AsyncEventLoop& loop,
65 const FileDescriptor& descriptor);
66};
67
69struct ReadableSocketStream : public AsyncRequestReadableStream<AsyncSocketReceive>
70{
71 Result init(AsyncBuffersPool& buffersPool, Span<Request> requests, AsyncEventLoop& loop,
72 const SocketDescriptor& descriptor);
73};
74
76struct WritableSocketStream : public AsyncRequestWritableStream<AsyncSocketSend>
77{
78 Result init(AsyncBuffersPool& buffersPool, Span<Request> requests, AsyncEventLoop& loop,
79 const SocketDescriptor& descriptor);
80};
81
82} // namespace SC
Holds a Span of AsyncBufferView (allocated by user) holding available memory for the streams.
Definition: AsyncStreams.h:66
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:965
Async source abstraction emitting data events in caller provided byte buffers.
Definition: AsyncStreams.h:100
Definition: AsyncRequestStreams.h:13
Result registerAutoCloseDescriptor(bool value)
Registers or unregisters a listener to AsyncReadableStream::eventEnd to close descriptor.
Definition: AsyncRequestStreams.h:32
Result registerAutoCloseDescriptor(bool value)
Registers or unregisters a listener to AsyncWritableStream::eventFinish to close descriptor.
Async destination abstraction where bytes can be written to.
Definition: AsyncStreams.h:190
File Descriptor (use SC::File to open and use it with strings and buffers).
Definition: FileDescriptor.h:52
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:19
Uses an SC::AsyncFileRead to stream data from a file.
Definition: AsyncRequestStreams.h:56
Uses an SC::AsyncFileWrite to stream data from a socket.
Definition: AsyncRequestStreams.h:70
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
Low-level OS socket handle.
Definition: SocketDescriptor.h:147
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
Uses an SC::AsyncFileWrite to stream data to a file.
Definition: AsyncRequestStreams.h:63
Uses an SC::AsyncFileWrite to stream data to a socket.
Definition: AsyncRequestStreams.h:77