Base module

Stream

Basic stream type for sockets and pipes.

#include <icy/stream.h>

template<typename T>
class Stream

Defined in src/base/include/icy/stream.h:30

Inherits
Handle< T >

List of all members

NameKindOwner
ReadvariableDeclared here
StreamfunctionDeclared here
~StreamfunctionDeclared here
closefunctionDeclared here
shutdownfunctionDeclared here
writefunctionDeclared here
writeOwnedfunctionDeclared here
setHighWaterMarkfunctionDeclared here
writefunctionDeclared here
streamfunctionDeclared here
_buffervariableDeclared here
_startedvariableDeclared here
_highWaterMarkvariableDeclared here
_writeReqFreevariableDeclared here
_ownedWriteReqFreevariableDeclared here
readStartfunctionDeclared here
readStopfunctionDeclared here
onReadfunctionDeclared here
allocWriteReqfunctionDeclared here
freeWriteReqfunctionDeclared here
allocOwnedWriteReqfunctionDeclared here
freeOwnedWriteReqfunctionDeclared here
canQueueWritefunctionDeclared here
HandletypedefDeclared here
HandlefunctionInherited from Handle
~HandlefunctionInherited from Handle
initfunctionInherited from Handle
invokefunctionInherited from Handle
invokeOrThrowfunctionInherited from Handle
closefunctionInherited from Handle
reffunctionInherited from Handle
unreffunctionInherited from Handle
initializedfunctionInherited from Handle
activefunctionInherited from Handle
closingfunctionInherited from Handle
closedfunctionInherited from Handle
errorfunctionInherited from Handle
setErrorfunctionInherited from Handle
setUVErrorfunctionInherited from Handle
setAndThrowErrorfunctionInherited from Handle
throwLastErrorfunctionInherited from Handle
loopfunctionInherited from Handle
resetfunctionInherited from Handle
getfunctionInherited from Handle
tidfunctionInherited from Handle
contextfunctionInherited from Handle
rawPtrfunctionInherited from Handle
setCloseCleanupfunctionInherited from Handle
clearCloseCleanupfunctionInherited from Handle
assertThreadfunctionInherited from Handle
_loopvariableInherited from Handle
_contextvariableInherited from Handle
_tidvariableInherited from Handle
_errorvariableInherited from Handle
onErrorfunctionInherited from Handle
onClosefunctionInherited from Handle
HandlefunctionInherited from Handle
operator=functionInherited from Handle
HandlefunctionInherited from Handle
operator=functionInherited from Handle
TypetypedefInherited from Handle

Inherited from Handle

KindNameDescription
functionHandle inlineConstruct the handle bound to the given event loop.
function~Handle virtual inline
functioninit inlineInitialize the underlying libuv handle by calling f with the loop, the raw handle pointer, and any additional args.
functioninvoke inlineInvoke a libuv function f with args on the initialized handle.
functioninvokeOrThrow inlineInvoke a libuv function f with args, throwing on failure.
functionclose virtual inlineClose and destroy the handle.
functionref inlineRe-reference the handle with the event loop after a previous unref().
functionunref inlineUnreference the handle from the event loop.
functioninitialized const inlineReturn true if the handle has been successfully initialized via init().
functionactive virtual const inlineReturn true when the handle is active (libuv uv_is_active).
functionclosing virtual const inlineReturn true if uv_close has been called and the handle is awaiting its close callback (libuv uv_is_closing).
functionclosed virtual const inlineReturn true if the handle has been fully closed (context released).
functionerror const inlineReturn the last error set on this handle, or a default-constructed Error if no error has occurred.
functionsetError virtual inlineSet the error state and invoke onError().
functionsetUVError inlineTranslate a libuv error code into an Error and call setError().
functionsetAndThrowError inlineSet the error state from a libuv error code and throw a std::runtime_error.
functionthrowLastError inlineThrow a std::runtime_error if the handle currently holds an error.
functionloop const inlineReturn the event loop this handle is bound to.
functionreset inlineClose the current handle (if open) and allocate a fresh Context, leaving the handle ready to be re-initialized via init().
functionget const inlineReturn the raw libuv handle pointer cast to Handle.
functiontid const inlineReturn the ID of the thread that constructed this handle.
functioncontext const inlineReturn the raw Context that owns the libuv handle memory.
functionrawPtr const inlineReturn the raw libuv handle pointer without retaining the Context.
functionsetCloseCleanup inline
functionclearCloseCleanup inline
functionassertThread const inlineThrow std::logic_error if called from any thread other than the thread that constructed this handle.
variable_loop
variable_context
variable_tid
variable_error
functiononError virtual inlineCalled by setError() after the error state has been updated.
functiononClose virtual inlineCalled by close() after the context has been released.
functionHandleNonCopyable and NonMovable.
functionoperator=Deleted assignment operator.
functionHandleDeleted constructor.
functionoperator=Deleted assignment operator.
typedefTypeDefine the native handle type.

Public Attributes

ReturnNameDescription
Signal< void(const char *, const int &)>ReadEmitted when data has been received from the peer.

Read

Signal< void(const char *, const int &)> Read

Type: Signal< void(const char *, const int &)>

Defined in src/base/include/icy/stream.h:211

Emitted when data has been received from the peer.

Slot signature: void(const char* data, const int& len)

Public Methods

ReturnNameDescription
Stream inlineConstruct the stream bound to loop with a 64 KiB read buffer.
~Stream virtual inlineDestroy the stream, stopping reads and freeing pooled write requests.
voidclose virtual inline overrideCloses and resets the stream handle. This will close the active socket/pipe and destroy the handle.
boolshutdown inlineSend a TCP/pipe shutdown request to the connected peer.
boolwrite inlineWrite len bytes from data to the stream.
boolwriteOwned inlineWrite an owned payload buffer to the stream.
voidsetHighWaterMark inlineSet the high water mark for the write queue (default 16MB). When the write queue exceeds this size, write() returns false.
boolwrite inlineWrite len bytes from data together with a stream handle over an IPC pipe (uses uv_write2).
uv_stream_t *stream inlineReturn the underlying uv_stream_t pointer cast from the native handle.

Stream

inline

inline Stream(uv::Loop * loop = uv::defaultLoop())

Defined in src/base/include/icy/stream.h:48

Construct the stream bound to loop with a 64 KiB read buffer.

Parameters

ParameterTypeDescription
loopuv::Loop *Event loop to associate this stream with.

~Stream

virtual inline

virtual inline ~Stream()

Defined in src/base/include/icy/stream.h:55

Destroy the stream, stopping reads and freeing pooled write requests.


close

virtual inline override

virtual inline void close() override

Defined in src/base/include/icy/stream.h:68

Closes and resets the stream handle. This will close the active socket/pipe and destroy the handle.

If the stream is already closed this call will have no side-effects.

Reimplements

Reimplemented by


shutdown

inline

inline bool shutdown()

Defined in src/base/include/icy/stream.h:83

Send a TCP/pipe shutdown request to the connected peer.

Issues a half-close: no further writes will be accepted after this, but the stream remains open for reading until the peer also closes.

Returns

true if the shutdown request was submitted successfully; false if the stream is not active.


write

inline

inline bool write(const char * data, size_t len)

Defined in src/base/include/icy/stream.h:106

Write len bytes from data to the stream.

The write is non-blocking; data is buffered by libuv. Returns false without throwing if the stream is inactive, reads have not started, or the internal write queue exceeds the high-water mark.

Returns

true if the write was queued; false on backpressure or if the stream is not in a writable state.

Parameters

ParameterTypeDescription
dataconst char *Pointer to the bytes to send. Must remain valid until the write completion callback fires.
lensize_tNumber of bytes to send.

writeOwned

inline

inline bool writeOwned(Buffer && buffer)

Defined in src/base/include/icy/stream.h:141

Write an owned payload buffer to the stream.

The buffer is moved into the queued write request and retained until the libuv completion callback fires. Use this path whenever the caller does not naturally own storage beyond the current stack frame.

Returns

true if the write was queued; false on backpressure or if the stream is not in a writable state.

Parameters

ParameterTypeDescription
bufferBuffer &&Payload buffer moved into the async write request.

setHighWaterMark

inline

inline void setHighWaterMark(size_t bytes)

Defined in src/base/include/icy/stream.h:167

Set the high water mark for the write queue (default 16MB). When the write queue exceeds this size, write() returns false.


write

inline

inline bool write(const char * data, size_t len, uv_stream_t * send)

Defined in src/base/include/icy/stream.h:179

Write len bytes from data together with a stream handle over an IPC pipe (uses uv_write2).

Only valid for named-pipe handles opened with IPC mode enabled. Throws std::logic_error if called on a non-IPC pipe.

Returns

true if the write was queued; false on error.

Parameters

ParameterTypeDescription
dataconst char *Bytes to send alongside the handle.
lensize_tNumber of bytes to send.
senduv_stream_t *Stream handle to pass to the receiving process.

stream

inline

inline uv_stream_t * stream()

Defined in src/base/include/icy/stream.h:203

Return the underlying uv_stream_t pointer cast from the native handle.

Returns

Pointer to the uv_stream_t, or nullptr if the handle is closed.

Protected Attributes

ReturnNameDescription
Buffer_buffer
bool_started
size_t_highWaterMark16MB default write queue limit
std::vector< uv_write_t * >_writeReqFreeFreelist for write requests.
std::vector< OwnedWriteReq * >_ownedWriteReqFreeFreelist for owned write requests.

_buffer

Buffer _buffer

Type: Buffer

Defined in src/base/include/icy/stream.h:375


_started

bool _started {false}

Defined in src/base/include/icy/stream.h:376


_highWaterMark

size_t _highWaterMark {16 * 1024 * 1024}

Defined in src/base/include/icy/stream.h:377

16MB default write queue limit


_writeReqFree

std::vector< uv_write_t * > _writeReqFree

Defined in src/base/include/icy/stream.h:378

Freelist for write requests.


_ownedWriteReqFree

std::vector< OwnedWriteReq * > _ownedWriteReqFree

Type: std::vector< OwnedWriteReq * >

Defined in src/base/include/icy/stream.h:379

Freelist for owned write requests.

Protected Methods

ReturnNameDescription
boolreadStart virtual inlineBegin reading from the stream by registering libuv read callbacks.
boolreadStop virtual inlineStop reading from the stream.
voidonRead virtual inlineCalled by handleRead when len bytes of data arrive.
uv_write_t *allocWriteReq inlineReturn a uv_write_t from the freelist, or allocate a new one if the pool is empty.
voidfreeWriteReq inlineReturn req to the freelist, or delete it if the pool is at capacity.
OwnedWriteReq *allocOwnedWriteReq inline
voidfreeOwnedWriteReq inline
boolcanQueueWrite inline

readStart

virtual inline

virtual inline bool readStart()

Defined in src/base/include/icy/stream.h:220

Begin reading from the stream by registering libuv read callbacks.

Sets the stream's data pointer to this so callbacks can recover the C++ object. Has no effect and returns false if already started.

Returns

true if uv_read_start was called successfully.

Reimplemented by


readStop

virtual inline

virtual inline bool readStop()

Defined in src/base/include/icy/stream.h:237

Stop reading from the stream.

No further read callbacks will fire after this returns. Has no effect and returns false if not currently started.

Returns

true if uv_read_stop was called successfully.


onRead

virtual inline

virtual inline void onRead(const char * data, size_t len)

Defined in src/base/include/icy/stream.h:254

Called by handleRead when len bytes of data arrive.

The default implementation emits the Read signal. Override to intercept data before it reaches signal subscribers.

Reimplemented by

Parameters

ParameterTypeDescription
dataconst char *Pointer into the read buffer; valid only for this call.
lensize_tNumber of valid bytes in data.

allocWriteReq

inline

inline uv_write_t * allocWriteReq()

Defined in src/base/include/icy/stream.h:319

Return a uv_write_t from the freelist, or allocate a new one if the pool is empty.

Returns

Pointer to an unused uv_write_t.


freeWriteReq

inline

inline void freeWriteReq(uv_write_t * req)

Defined in src/base/include/icy/stream.h:332

Return req to the freelist, or delete it if the pool is at capacity.

Parameters

ParameterTypeDescription
requv_write_t *Write request to recycle or free.

allocOwnedWriteReq

inline

inline OwnedWriteReq * allocOwnedWriteReq()

Defined in src/base/include/icy/stream.h:341


freeOwnedWriteReq

inline

inline void freeOwnedWriteReq(OwnedWriteReq * req)

Defined in src/base/include/icy/stream.h:351


canQueueWrite

inline

inline bool canQueueWrite(size_t len)

Defined in src/base/include/icy/stream.h:362

Public Types

NameDescription
Handle

Handle

using Handle = uv::Handle< T >

Type: uv::Handle< T >

Defined in src/base/include/icy/stream.h:33