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< void > | shutdown () |
Promise< void > | close () |
const uv_stream_t * | underlying () const |
Return the underlying UV stream object. | |
Static Public Attributes | |
static constexpr size_t | defaultMaxReadSize = 4096 |
Protected Member Functions | |
uv_stream_t & | stream () |
void | destroyStream () |
Private Attributes | |
std::unique_ptr< uv_stream_t, UvHandleDeleter > | stream_ |
std::optional< std::coroutine_handle<> > | reader_ |
std::optional< std::coroutine_handle<> > | writer_ |
A plain stream, permitting reading, writing, and closing.
|
inlineexplicit |
|
delete |
|
default |
|
virtual |
Promise< void > uvco::StreamBase::close | ( | ) |
The result of close()
must be co_await
ed; otherwise memory may be leaked. (this is not an issue just before termination of a process)
Informs pending readers and writers of the close and causes them to return an empty optional.
|
inlineprotected |
|
delete |
|
default |
Promise< std::optional< std::string > > uvco::StreamBase::read | ( | size_t | maxSize = defaultMaxReadSize | ) |
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.
Throws UvcoException
on error.
Promise< void > uvco::StreamBase::shutdown | ( | ) |
Shut down stream for writing. This is a half-close; the other side can still write. The result of shutdown()
must be co_await
ed.
|
inlineprotected |
|
inline |
Return the underlying UV stream object.
Promise< size_t > uvco::StreamBase::write | ( | std::string | buf | ) |
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.
|
staticconstexpr |
|
private |
|
private |
|
private |