template <class ServiceHub, class Service>

class ServiceHubConnector

Defined at line 106 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

ServiceHubConnector is a utility class to make connecting to fidl protocol factories easier.

To use this class, it must be extended with implementations of the

methods |ConnectToServiceHub()| and |ConnectToService()|.

NOTE: ServiceHubConnector and its subclasses must be used from the |dispatcher| thread.

This includes construction, destruction, and making calls.

For example, if you have a fidl service like:

type Error = strict enum : int32 {

PERMANENT = 1;

TRANSIENT = 2;

};

protocol ProtocolFactory {

CreateProtocol(resource struct {

protocol server_end:Protocol;

}) -> () error Error;

};

protocol Protocol {

DoAction() -> () error Error;

};

Then you could implement ServiceHubConnector like this:

class ProtocolConnector final : private ServiceHubConnector

<ProtocolFactory

, Protocol, Status>

{

public:

explicit ProtocolConnector(async_dispatcher_t*dispatcher,

fidl::UnownedClientEnd

<fuchsia

_io::Directory> directory)

: ServiceHubConnector(dispatcher), directory_(directory) {}

private:

void ConnectToServiceHub(ServiceHubConnectResolver resolver) override {

auto connection = component::ConnectAt

<ProtocolFactory

>(directory_);

if (connection.is_error()) {

resolver.resolve(std::nullopt);

} else {

resolver.resolve(std::move(connection.value()));

}

}

void ConnectToService(fidl::Client

<ProtocolFactory

>

&

factory,

ServiceConnectResolver resolver) override {

auto endpoints = fidl::CreateEndpoints

<Protocol

>();

factory

->CreateProtocol(

test_protocol::ProtocolFactoryCreateProtocolRequest(std::move(endpoints->server)))

.Then([resolver = std::move(resolver), client_end = std::move(endpoints->client)](

fidl::Result

<ProtocolFactory

::CreateProtocol>

&response

) mutable {

if (response.is_ok()) {

resolver.resolve(std::move(client_end));

} else {

resolver.resolve(std::nullopt);

}

});

}

fidl::UnownedClientEnd

<fuchsia

_io::Directory> directory_;

};

Then you could use it like:

ProtocolConnector connector(...);

connector.Do([](fidl::Client

<Protocol

>

&

protocol, DoResolver resolver) {

protocol->DoAction().Then(

[resolver = std::move(resolver)](

fidl::Result

<test

_protocol::Protocol::DoAction>

&

status) mutable {

resolver.resolve(status.is_error()

&

&

(status.error_value().is_framework_error() ||

status.error_value().domain_error() == Error::kTransient));

});

});

Public Methods

void Do (DoCallback && cb)

The |Do()| method is the only way of performing actions using the underlying |Protocol|. This

method must be called from the dispatcher thread.

It is recommended for classes that extend ServiceHubConnector create wrapper functions to

ease the calling of this method e.g.:

void DoAction() {

Do([](fidl::Client

<Protocol

>

&

protocol, DoResolver resolver) {

protocol->DoAction().Then(

[resolver = std::move(resolver)](

fidl::Result

<test

_protocol::Protocol::DoAction>

&

status) mutable {

resolver.resolve(status.is_error()

&

&

(status.error_value().is_framework_error() ||

status.error_value().domain_error() == Error::kTransient));

});

});

}

Defined at line 324 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

void ServiceHubConnector<ServiceHub, Service> (async_dispatcher_t * dispatcher, size_t max_queued_callbacks)

|dispatcher| the dispatcher thread where the fidl services should be connected from.

|max_queued_callbacks| (default: 20) How many callbacks should each ServiceReconnector cache

before rejecting new ones.

Defined at line 329 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

void ~ServiceHubConnector<ServiceHub, Service> ()

Defined at line 339 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

void ServiceHubConnector<ServiceHub, Service> (const ServiceHubConnector<ServiceHub, Service> & )

ServiceHubConnector should not be copy or movable.

Defined at line 346 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

ServiceHubConnector<ServiceHub, Service> & operator= (const ServiceHubConnector<ServiceHub, Service> & )

Defined at line 347 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

void ServiceHubConnector<ServiceHub, Service> (ServiceHubConnector<ServiceHub, Service> && other)

Defined at line 348 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

ServiceHubConnector<ServiceHub, Service> & operator= (ServiceHubConnector<ServiceHub, Service> && other)

Defined at line 349 of file ../../src/lib/fidl/contrib/connection/service_hub_connector.h

Protected Methods

void ConnectToServiceHub (ServiceHubConnectResolver resolver)

ConnectToServiceHub is used to get a handle for the service hub.

void ConnectToService (fidl::Client<ServiceHub> & service_hub, ServiceConnectResolver resolver)

ConnectToService is used once the factory service has been connected.

Records