template <typename FidlProtocol>

class ServerBindingGroup

Defined at line 276 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

|ServerBindingGroup| manages a collection of FIDL |ServerBinding|s. It does not own the |impl|s

backing those bindings. All members of a |ServerBindingGroup| collection must implement a common

FIDL protocol, but implementations themselves may be distinct from one another.

Destroying a |ServerBindingGroup| will close all managed connections and release the references

to the implementations. If one does not require per-connection state, a common pattern is to

have a common server implementation own its |ServerBindingGroup|.

## Example

// Define the protocol implementation.

class Impl : public fdf::Server

<fuchsia

_lib::MyProtocol> {

public:

fidl::ProtocolHandler

<fuchsia

_lib::MyProtocol> GetHandler() {

return bindings_.CreateHandler(this, dispatcher, std::mem_fn(

&Impl

::OnClosed));

}

private:

void OnClosed(fidl::UnbindInfo info) {

// Called when a connection to this server is closed.

// This is provided to the binding group during |CreateHandler|.

}

fdf::ServerBindingGroup

<fuchsia

_lib::MyProtocol> bindings_;

};

// Instantiate the server.

Impl impl;

// Publish the server to an outgoing directory.

auto outgoing = fdf::OutgoingDirectory::Create(dispatcher);

outgoing.AddService(fuchsia_lib::MyService::InstanceHandler {

.member = impl.GetHandler(),

});

One may also explicitly let the binding group manage new connections using |AddBinding|:

// Instantiate some servers.

auto a = ImplA{...};

auto b = ImplB{...};

// Create the group.

fdf::ServerBindingGroup

<fuchsia

_lib::MyProtocol> bindings;

// Add server endpoints and implementations to the group.

fdf::Endpoints

<fuchsia

_lib::MyProtocol>() endpoints1 = ...;

group.AddBinding(dispatcher, endpoints1->server,

&a

, OnClosed);

fdf::Endpoints

<fuchsia

_lib::MyProtocol>() endpoints2 = ...;

group.AddBinding(dispatcher, endpoints2->server,

&a

, OnClosed);

# Thread safety

This class is thread-unsafe. Instances must be managed and used from a

synchronized async dispatcher. See

https://fuchsia.dev/fuchsia-src/development/languages/c-cpp/thread-safe-async#synchronized-dispatcher

Public Methods

void ServerBindingGroup<FidlProtocol> ()

Defined at line 287 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

void ServerBindingGroup<FidlProtocol> (const ServerBindingGroup<FidlProtocol> & )

Defined at line 288 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

void ServerBindingGroup<FidlProtocol> (ServerBindingGroup<FidlProtocol> && )

Defined at line 289 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

ServerBindingGroup<FidlProtocol> & operator= (const ServerBindingGroup<FidlProtocol> & )

Defined at line 290 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

ServerBindingGroup<FidlProtocol> & operator= (ServerBindingGroup<FidlProtocol> && )

Defined at line 291 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

template <typename ServerImpl, typename CloseHandler>
void AddBinding (fdf_dispatcher_t * dispatcher, ServerEnd<FidlProtocol> server_end, ServerImpl * impl, CloseHandler && close_handler)

Add a binding to an unowned impl to the group.

|CloseHandler| is silently discarded if |ServerBindingGroup| 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.

This method allows one to bind a |CloseHandler| to the newly created server binding instance.

See |ServerBinding| for more information on the behavior of the |CloseHandler|, when and how it

is called, etc. This is particularly useful when passing in a |std::unique_ptr

<ServerImpl

>|,

because one does not have to capture the server implementation pointer again:

// Define and instantiate the impl.

class Impl : fdf::WireServer

<Protocol

> {

public:

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

};

auto impl = Impl(...);

fdf::ServerBindingGroup

<Protocol

> binding_group;

// Bind the server endpoint to the |Impl| instance, and hook up the

// |CloseHandler| to its |OnFidlClosed| member function.

binding_group.AddBinding(

dispatcher, std::move(server_end),

&impl

,

std::mem_fn(

&Impl

::OnFidlClosed));

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 |close_handler|.

Defined at line 329 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

template <typename ServerImpl, typename CloseHandler>
fidl::ProtocolHandler<FidlProtocol> CreateHandler (ServerImpl * impl, fdf_dispatcher_t * dispatcher, CloseHandler && close_handler)

Returns a protocol handler that binds the incoming |ServerEnd| to the passed in |impl|.

All bindings will use the same |CloseHandler|.

Defined at line 338 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

void ForEachBinding (fit::function<void (const Binding &)> visitor)

Iterate over the bindings stored in this group.

Defined at line 344 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

template <class ServerImpl>
bool RemoveBindings (const ServerImpl * impl)

Removes all bindings associated with a particular |impl| without calling their close handlers.

None of the removed bindings will have its close handler called. Returns true if at least one

binding was removed.

Defined at line 352 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

bool RemoveAll ()

Removes all bindings. None of the removed bindings close handlers' is called. Returns true if

at least one binding was removed.

Defined at line 358 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

template <class ServerImpl>
bool CloseBindings (const ServerImpl * impl, zx_status_t epitaph_value)

Closes all bindings associated with the specified |impl|. The supplied epitaph is passed to

each closed binding's close handler, which is called in turn. Returns true if at least one

binding was closed. The teardown operation is asynchronous, and will not necessarily have been

completed by the time this function returns.

Defined at line 365 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

bool CloseAll (zx_status_t epitaph_value)

Closes all bindings. All of the closed bindings' close handlers are called. Returns true if at

least one binding was closed.

Defined at line 371 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

size_t size ()

The number of active bindings in this |ServerBindingGroup|.

Defined at line 374 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h

void set_empty_set_handler (fit::closure empty_set_handler)

Called when a previously full |ServerBindingGroup| has been emptied. A |ServerBindingGroup| is

"empty" once it contains no active bindings, and all closed bindings that it held since the

last time it was empty have finished their tear down routines.

This function is not called by |~ServerBindingGroup|.

Defined at line 381 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h