template <typename Protocol>
class ServerBinding
Defined at line 140 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.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 fdf::Server
<fuchsia
_my_library::MyProtocol> {
public:
Impl(fdf::ServerEnd
<fuchsia
_my_library::Protocol> server_end, fdf_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:
fdf::ServerBinding
<fuchsia
_my_library::MyProtocol> binding_;
};
## See also
* |WireClient|, |Client|: which are the client analogues of this class.
## 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 : fdf::WireServer
<Protocol
> {
public:
void OnFidlClosed(fidl::UnbindInfo) { /* handle errors */ }
};
fidl::ServerBinding
<Protocol
> binding(
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 |CloseHandler|, as follows:
fidl::ServerBinding
<Protocol
> binding(
dispatcher, std::move(server_end), impl,
fidl::kIgnoreBindingClosure);
Defined at line 179 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h
template <typename Impl, typename CloseHandler>
void ServerBinding<Protocol> (fdf_dispatcher_t * dispatcher, 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 |fdf::Server
<FidlProtocol
>| or
|fdf::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 196 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.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 206 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h
ServerBinding<FidlProtocol> & operator= (ServerBinding<FidlProtocol> && other)
Defined at line 207 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h
void ServerBinding<Protocol> (const ServerBinding<FidlProtocol> & other)
Defined at line 209 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h
ServerBinding<FidlProtocol> & operator= (const ServerBinding<FidlProtocol> & other)
Defined at line 210 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.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 216 of file ../../sdk/lib/fidl_driver/include/lib/fidl_driver/cpp/server.h