template <typename Protocol>

class ServerBinding

Defined at line 462 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

|ServerBinding| binds the implementation of a FIDL protocol to a server

endpoint.

|ServerBinding| listens for incoming messages on the channel, decodes them,

and calls the appropriate method on the bound implementation.

When the |ServerBinding| object is destroyed, the binding between the

protocol endpoint and the server implementation is torn down and the channel

is closed. Once destroyed, it will not make any method calls on the server

implementation. Thus the idiomatic usage of a |ServerBinding| is to embed it

as a member variable of a server implementation, such that they are destroyed

together.

## Example

class Impl : public fidl::Server

<fuchsia

_my_library::MyProtocol> {

public:

Impl(fidl::ServerEnd

<fuchsia

_my_library::Protocol> server_end, async_dispatcher_t* dispatcher)

: binding_(dispatcher, std::move(server_end), this, std::mem_fn(

&Impl

::OnFidlClosed)) {}

void OnFidlClosed(fidl::UnbindInfo info) override {

// Handle errors..

}

// More method implementations omitted...

private:

fidl::ServerBinding

<fuchsia

_my_library::MyProtocol> binding_;

};

## See also

* |WireClient|, |Client|: which are the client analogues of this class.

* |WireSendEvent|, |SendEvent|: which can be used to send events over the

bound endpoint.

## Thread safety

|ServerBinding| is thread unsafe. Tearing down a |ServerBinding| guarantees

no more method calls on the borrowed |Impl|. This is only possible when

the teardown is synchronized with message dispatch. The binding will enforce

[synchronization guarantees][synchronization-guarantees] at runtime with

threading checks.

[synchronization-guarantees]:

https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/sdk/lib/async/README.md#verifying-synchronization-requirements

Public Methods

template <typename Impl, typename CloseHandler>
void CloseHandlerRequirement ()

|CloseHandler| is invoked when the endpoint managed by the |ServerBinding| is closed, due to a

terminal error or because the user initiated binding teardown.

|CloseHandler| is silently discarded if |ServerBinding| is destroyed, to avoid calling into a

destroyed server implementation.

The handler may have one of these signatures:

void(fidl::UnbindInfo info);

void(Impl* impl, fidl::UnbindInfo info);

|info| contains the detailed reason for stopping message dispatch.

|impl| is the pointer to the server implementation borrowed by the binding.

The second overload allows one to bind the close handler to an instance

method on the server implementation, without capturing extra state:

class Impl : fidl::WireServer

<Protocol

> {

public:

void OnFidlClosed(fidl::UnbindInfo) { /* handle errors */ }

};

fidl::ServerBinding

<Protocol

> binding(

dispatcher, std::move(server_end), impl,

std::mem_fn(

&Impl

::OnFidlClosed));

The close handler will be invoked on a dispatcher thread, unless the user shuts down the async

dispatcher while there are active server bindings associated with it. In that case, the handler

will be synchronously invoked on the thread calling dispatcher shutdown.

In cases where the binding implementation never cares to handle any errors or be notified about

binding closure, one can pass |fidl::kIgnoreBindingClosure| as the |CloseHandler|, as follows:

fidl::ServerBinding

<Protocol

> binding(

dispatcher, std::move(server_end), impl,

fidl::kIgnoreBindingClosure);

Defined at line 504 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

template <typename Impl, typename CloseHandler>
void ServerBinding<Protocol> (async_dispatcher_t * dispatcher, fidl::ServerEnd<FidlProtocol> server_end, Impl * impl, CloseHandler && close_handler)

Constructs a binding that dispatches messages from |server_end| to |impl|,

using |dispatcher|.

|Impl| should implement |fidl::Server

<FidlProtocol

>| or

|fidl::WireServer

<FidlProtocol

>|.

|impl| and any state captured in |close_handler| should outlive the bindings.

It's not safe to move |impl| while the binding is still referencing it.

|close_handler| is invoked when the endpoint managed by the |ServerBinding|

is closed, due to a terminal error or because the user initiated binding

teardown. See |CloseHandlerRequirement| for details on the error handler.

Defined at line 521 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

void ServerBinding<Protocol> (ServerBinding<FidlProtocol> && other)

The usual usage style of |ServerBinding| puts it as a member variable of a

server object, to which it unsafely borrows. Thus it's unsafe to move the

server objects. As a precaution, we do not allow moving the bindings. If

one needs to move a server object, consider wrapping it in a

|std::unique_ptr|.

Defined at line 530 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

ServerBinding<FidlProtocol> & operator= (ServerBinding<FidlProtocol> && other)

Defined at line 531 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

void ServerBinding<Protocol> (const ServerBinding<FidlProtocol> & other)

Defined at line 533 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

ServerBinding<FidlProtocol> & operator= (const ServerBinding<FidlProtocol> & other)

Defined at line 534 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

void ~ServerBinding<Protocol> ()

Tears down the binding and closes the connection.

After the binding destructs, it will release references on |impl|.

Destroying the binding will discard the |close_handler| without calling it.

Defined at line 540 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

void Close (zx_status_t epitaph)

Tears down the binding and closes the connection with an epitaph.

|close_handler| will be called with the appropriate unbind reason.

Defined at line 545 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h

template <typename ServerImpl>
void AsImpl (fit::function<void (const ServerImpl *)> impl_handler)

Retrieve the implementation used by this |ServerBindingRef| to process incoming messages, and

get exclusive const access to it before passing it to a lambda for further introspection.

Defined at line 552 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h