template <typename FidlProtocol>
class ServerBindingGroup
Defined at line 819 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 fidl::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|.
}
fidl::ServerBindingGroup
<fuchsia
_lib::MyProtocol> bindings_;
};
// Instantiate the server.
Impl impl;
// Publish the server to an outgoing directory.
component::OutgoingDirectory outgoing{dispatcher};
outgoing.AddUnmanagedProtocol
<fuchsia
_lib::MyProtocol>(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.
fidl::ServerBindingGroup
<fuchsia
_lib::MyProtocol> bindings;
// Add server endpoints and implementations to the group.
fidl::Endpoints
<fuchsia
_lib::MyProtocol>() endpoints1 = ...;
group.AddBinding(dispatcher, endpoints1->server,
&a
, OnClosed);
fidl::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 828 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
void ServerBindingGroup<FidlProtocol> (const ServerBindingGroup<FidlProtocol> & )
Defined at line 829 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
void ServerBindingGroup<FidlProtocol> (ServerBindingGroup<FidlProtocol> && )
Defined at line 830 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
ServerBindingGroup<FidlProtocol> & operator= (const ServerBindingGroup<FidlProtocol> & )
Defined at line 831 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
ServerBindingGroup<FidlProtocol> & operator= (ServerBindingGroup<FidlProtocol> && )
Defined at line 832 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
template <typename ServerImpl, typename CloseHandler>
void AddBinding (async_dispatcher_t * dispatcher, fidl::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 : fidl::WireServer
<Protocol
> {
public:
void OnFidlClosed(fidl::UnbindInfo) { /* handle errors */ }
};
auto impl = Impl(...);
fidl::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 870 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
template <typename ServerImpl, typename CloseHandler>
fidl::ProtocolHandler<FidlProtocol> CreateHandler (ServerImpl * impl, async_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 879 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
void ForEachBinding (fit::function<void (const Binding &)> visitor)
Iterate over the bindings stored in this group.
Defined at line 886 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 894 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 900 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 907 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 913 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h
size_t size ()
The number of active bindings in this |ServerBindingGroup|.
Defined at line 916 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.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 923 of file ../../sdk/lib/fidl/cpp/wire/include/lib/fidl/cpp/wire/channel.h