class Channel

Defined at line 48 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

C++ wrapper for a channel, with RAII semantics. Automatically closes

the channel when it goes out of scope.

# Thread safety

This class is thread-unsafe.

# Example

constexpr uint32_t kTag = 'EXAM';

fdf::Arena arena(kTag);

void* data = arena.Allocate(0x1000);

// Set the data to transfer

...

auto channels = fdf::ChannelPair::Create(0);

// Transfer end1 of the channel pair elsewhere.

...

auto write_status = channels->end0.Write(0, arena, data, 0x1000,

cpp20::span

<zx

_handle_t>());

auto channel_read = std::make_unique

<fdf

::ChannelRead>(

channels->end0, 0,

[

&

](fdf_dispatcher_t* dispatcher, fdf::ChannelRead* channel_read, zx_status_t status) {

fdf::Channel channel(channel_read->channel());

auto read_return = channel.Read(0);

...

});

zx_status_t status = channel_read->Begin(dispatcher_.get());

Public Methods

void Channel ()

Defined at line 52 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void Channel (fdf_handle_t channel)

Defined at line 53 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void Channel (const Channel & to_copy)

Channel cannot be copied.

Defined at line 56 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

Channel & operator= (const Channel & other)

Defined at line 57 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void Channel (Channel && other)

Channel can be moved. Once moved, invoking a method on an instance will

yield undefined behavior.

Defined at line 61 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

Channel & operator= (Channel && other)

Defined at line 62 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void ~Channel ()

Closes the handle, causing the underlying object to be reclaimed by the runtime

if no other handles to it exist.

If there is a pending callback registered via |fdf_channel_wait_async|,

it must be cancelled before this is called. For unsynchronized dispatchers,

cancellation is not considered complete until the callback is invoked.

It is not an error to close the special "never a valid handle" FDF_HANDLE_INVALID,

similar to free(NULL) being a valid call.

Closing the last handle to a peered object using |fdf_handle_close| can affect the

state of the object's peer (if any).

Defined at line 79 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

zx::result<> Write (uint32_t options, const Arena & arena, void * data, uint32_t num_bytes, cpp20::span<zx_handle_t> handles)

Attempts to write a message to the channel.

The caller retains ownership of the |arena| reference, which must be destroyed via

|fdf_arena_drop_ref|. It is okay to call |fdf_arena_drop_ref| on the arena as soon as

the write call returns, as |fdf_arena_write| will take its own reference to the arena,

extending the lifetime of the arena is until the data is read.

The pointers |data| and |handles| may be NULL if their respective sizes are zero.

|data| and |handles| must be pointers managed by |arena| if they are not NULL.

|handles| may be a mix of zircon handles and fdf handles.

Handles with a pending callback registered via |fdf_channel_wait_async| cannot be transferred.

On success, all |num_handles| of the handles in the handles array are attached to the

message and will become available to the reader of that message from the opposite end of the

channel.

All handles are consumed and are no longer available to the caller, on success or failure.

# Errors

ZX_ERR_INVALID_ARGS: |data| or |handles| are not pointers managed by |arena|,

or any element in |handles| is not a valid handle,

or at least one of |handles| has a pending callback registered via |fdf_channel_wait_async|.

ZX_ERR_NO_MEMORY: Failed due to a lack of memory.

ZX_ERR_PEER_CLOSED: The other side of the channel is closed.

This operation is thread-safe.

Defined at line 111 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

zx::result<> Write (uint32_t options, const Arena & arena, void * data, uint32_t num_bytes, cpp20::span<zx_handle_t> handles)

Attempts to write a message to the channel.

The caller retains ownership of the |arena| reference, which must be destroyed via

|fdf_arena_drop_ref|. It is okay to call |fdf_arena_drop_ref| on the arena as soon as

the write call returns, as |fdf_arena_write| will take its own reference to the arena,

extending the lifetime of the arena is until the data is read.

The pointers |data| and |handles| may be NULL if their respective sizes are zero.

|data| and |handles| must be pointers managed by |arena| if they are not NULL.

|handles| may be a mix of zircon handles and fdf handles.

Handles with a pending callback registered via |fdf_channel_wait_async| cannot be transferred.

On success, all |num_handles| of the handles in the handles array are attached to the

message and will become available to the reader of that message from the opposite end of the

channel.

All handles are consumed and are no longer available to the caller, on success or failure.

# Errors

ZX_ERR_INVALID_ARGS: |data| or |handles| are not pointers managed by |arena|,

or any element in |handles| is not a valid handle,

or at least one of |handles| has a pending callback registered via |fdf_channel_wait_async|.

ZX_ERR_NO_MEMORY: Failed due to a lack of memory.

ZX_ERR_PEER_CLOSED: The other side of the channel is closed.

This operation is thread-safe.

Defined at line 111 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

zx::result<ReadReturn> Read (uint32_t options)

Attempts to read the first message from the channel and returns a |ReadReturn|.

# Errors

ZX_ERR_SHOULD_WAIT: The channel contained no messages to read.

ZX_ERR_PEER_CLOSED: There are no available messages and the other

side of the channel is closed.

This operation is thread-safe.

Defined at line 139 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

zx::result<ReadReturn> Call (uint32_t options, zx::time deadline, const Arena & arena, void * data, uint32_t num_bytes, cpp20::span<zx_handle_t> handles)

Channel::Call() is like a combined Channel::Write(), ChannelRead::Begin(),

and Channel::Call(), with the addition of a feature where a transaction id at

the front of the message payload bytes is used to match reply messages with send messages,

enabling multiple calling threads to share a channel without any additional client-side

bookkeeping.

The first four bytes of the written and read back messages are treated as a

transaction ID of type fdf_txid_t. The runtime generates a txid for the

written message, replacing that part of the message as read from the user.

The runtime generated txid will be between 0x80000000 and 0xFFFFFFFF,

and will not collide with any txid from any other Channel::Call()

in progress against this channel endpoint. If the written message has a

length of fewer than four bytes, an error is reported.

While |deadline| has not passed, if an inbound message arrives with a matching txid,

instead of being added to the tail of the general inbound message queue,

it is delivered directly to the thread waiting in Channel::Call().

If such a reply arrives after |deadline| has passed, it will arrive in the

general inbound message queue.

All written handles are consumed and are no longer available to the caller,

on success or failure.

# Errors

ZX_ERR_INVALID_ARGS: |data| or |handles| are not pointers managed by |arena|,

are not managed by |arena|, or element in |handles| is not a valid handle,

or |num_bytes| is less than four,

or at least one of |handles| has a pending callback registered via a ChannelRead.

ZX_ERR_PEER_CLOSED: The other side of the channel is closed.

ZX_ERR_TIMED_OUT: |deadline| passed before a reply matching

the correct txid was received.

ZX_ERR_BAD_STATE: This is called from a driver runtime managed thread

that does not allow sync calls.

This operation is thread-safe.

Defined at line 194 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

zx::result<ReadReturn> Call (uint32_t options, zx::time deadline, const Arena & arena, void * data, uint32_t num_bytes, cpp20::span<zx_handle_t> handles)

Channel::Call() is like a combined Channel::Write(), ChannelRead::Begin(),

and Channel::Call(), with the addition of a feature where a transaction id at

the front of the message payload bytes is used to match reply messages with send messages,

enabling multiple calling threads to share a channel without any additional client-side

bookkeeping.

The first four bytes of the written and read back messages are treated as a

transaction ID of type fdf_txid_t. The runtime generates a txid for the

written message, replacing that part of the message as read from the user.

The runtime generated txid will be between 0x80000000 and 0xFFFFFFFF,

and will not collide with any txid from any other Channel::Call()

in progress against this channel endpoint. If the written message has a

length of fewer than four bytes, an error is reported.

While |deadline| has not passed, if an inbound message arrives with a matching txid,

instead of being added to the tail of the general inbound message queue,

it is delivered directly to the thread waiting in Channel::Call().

If such a reply arrives after |deadline| has passed, it will arrive in the

general inbound message queue.

All written handles are consumed and are no longer available to the caller,

on success or failure.

# Errors

ZX_ERR_INVALID_ARGS: |data| or |handles| are not pointers managed by |arena|,

are not managed by |arena|, or element in |handles| is not a valid handle,

or |num_bytes| is less than four,

or at least one of |handles| has a pending callback registered via a ChannelRead.

ZX_ERR_PEER_CLOSED: The other side of the channel is closed.

ZX_ERR_TIMED_OUT: |deadline| passed before a reply matching

the correct txid was received.

ZX_ERR_BAD_STATE: This is called from a driver runtime managed thread

that does not allow sync calls.

This operation is thread-safe.

Defined at line 194 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

bool is_valid ()

Defined at line 222 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

fdf_handle_t get ()

Defined at line 224 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void reset (fdf_handle_t channel)

Defined at line 226 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

void close ()

Defined at line 231 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

fdf_handle_t release ()

Defined at line 238 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

Unowned<Channel> borrow ()

Defined at line 244 of file ../../sdk/lib/driver/runtime/include/lib/fdf/cpp/channel.h

Records