|
uvco 0.1
|
A plain stream, permitting reading, writing, and closing. More...
#include <stream.h>

Classes | |
| struct | InStreamAwaiter_ |
| struct | OutStreamAwaiter_ |
| struct | ShutdownAwaiter_ |
Public Member Functions | |
| template<typename Stream> | |
| StreamBase (std::unique_ptr< Stream > stream) | |
| StreamBase (const StreamBase &)=delete | |
| StreamBase (StreamBase &&)=default | |
| StreamBase & | operator= (const StreamBase &)=delete |
| StreamBase & | operator= (StreamBase &&)=default |
| virtual | ~StreamBase () |
| Promise< std::optional< std::string > > | read (size_t maxSize=defaultMaxReadSize) |
| Promise< size_t > | read (std::span< char > buffer) |
| Promise< size_t > | write (std::string buf) |
| Promise< size_t > | writeBorrowed (std::span< const char > buf) |
| Promise< void > | shutdown () |
| void | close () |
| const uv_stream_t * | underlying () const |
| Return the underlying UV stream object. | |
| uv_os_fd_t | fd () const |
Static Public Attributes | |
| static constexpr size_t | defaultMaxReadSize = 4080 |
Protected Member Functions | |
| uv_stream_t & | stream () |
| void | destroyStream () |
Private Attributes | |
| std::unique_ptr< uv_stream_t, UvHandleDeleter > | stream_ |
| std::coroutine_handle | reader_ |
| std::coroutine_handle | writer_ |
A plain stream, permitting reading, writing, and closing.
|
inlineexplicit |
|
delete |
|
default |
|
virtual |
Closes the stream if not already closed. It's always best to explicitly call co_await stream.close(), but as a backup, the destructor will close the stream if still open. To do so, it will schedule a callback on the libuv loop.
| void uvco::StreamBase::close | ( | ) |
Informs pending readers and writers of the close and causes them to return an empty optional.
|
inlineprotected |
|
nodiscard |
|
delete |
|
default |
|
nodiscard |
Read available data (up to maxSize bytes) from stream. Returns std::nullopt on EOF or closed handle (close()).
Throws UvcoException on error.
NOTE: Consider using read(std::span<char>) for better performance.
NOTE: only one reader is allowed to be active at a time. If a read is started while another is still active, uvco will abort the process (in Debug mode), or ignore the first read (in Release mode).
| Promise< size_t > uvco::StreamBase::read | ( | std::span< char > | buffer | ) |
Read available data (up to buffer.size() bytes) from stream. Returns the number of bytes read, or 0 on EOF or closed handle (close()).
Only one read() coroutine may be active at a time. The stream must outlive the coroutine, i.e. live until co_await stream.read(...) returns a result.
Throws UvcoException on error.
|
nodiscard |
Shut down stream for writing. This is a half-close; the other side can still write. The result of shutdown() must be co_awaited.
|
inlineprotected |
|
inlinenodiscard |
Return the underlying UV stream object.
|
nodiscard |
Write a buffer to the stream. A copy of buf is taken because it is undetermined when the actual write will occur. Await the result if the status is important; the write will be executed even without awaiting (as long as the process keeps running).
NOTE: only one writer is allowed to be active at a time. If two writes are started simultaneously, the process will be aborted in Debug mode, or the first write() coroutine will not return in Release mode.
|
nodiscard |
The same as write(std::string), but takes a borrowed buffer. buf MUST absolutely stay valid until the promise resolves. This means: co_await this method and call it with a stored buffer (not a function return value, for example).
|
staticconstexpr |
|
private |
|
private |
|
private |