class RpcServer

Defined at line 49 of file ../../third_party/android/platform/frameworks/native/libs/binder/include/binder/RpcServer.h

This represents a server of an interface, which may be connected to by any

number of clients over sockets.

Usage:

auto server = RpcServer::make();

// only supports one now

if (!server->setup*Server(...)) {

:(

}

server->join();

Public Methods

sp<RpcServer> make (std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory)
status_t setupUnixDomainSocketBootstrapServer (binder::unique_fd serverFd)

Creates an RPC server that bootstraps sessions using an existing

Unix domain socket pair.

Callers should create a pair of SOCK_STREAM Unix domain sockets, pass

one to RpcServer::setupUnixDomainSocketBootstrapServer and the other

to RpcSession::setupUnixDomainSocketBootstrapClient. Multiple client

session can be created from the client end of the pair.

status_t setupUnixDomainServer (const char * path)

This represents a session for responses, e.g.:

process A serves binder a

process B opens a session to process A

process B makes binder b and sends it to A

A uses this 'back session' to send things back to B

status_t setupRawSocketServer (binder::unique_fd socket_fd)

Sets up an RPC server with a raw socket file descriptor.

The socket should be created and bound to a socket address already, e.g.

the socket can be created in init.rc.

This method is used in the libbinder_rpc_unstable API

RunInitUnixDomainRpcServer().

status_t setupVsockServer (unsigned int bindCid, unsigned int port, unsigned int * assignedPort)

Creates an RPC server binding to the given CID at the given port.

Set |port| to VMADDR_PORT_ANY to pick an ephemeral port. In this case, |assignedPort|

will be set to the picked port number, if it is not null.

status_t setupInetServer (const char * address, unsigned int port, unsigned int * assignedPort)

Creates an RPC server at the current port using IPv4.

TODO(b/182914638): IPv6 support

Set |port| to 0 to pick an ephemeral port; see discussion of

/proc/sys/net/ipv4/ip_local_port_range in ip(7). In this case, |assignedPort|

will be set to the picked port number, if it is not null.

Set the IPv4 address for the socket to be listening on.

"127.0.0.1" allows for local connections from the same device.

"0.0.0.0" allows for connections on any IP address that the device may

have

bool hasServer ()

If setup*Server has been successful, return true. Otherwise return false.

binder::unique_fd releaseServer ()

If hasServer(), return the server FD. Otherwise return invalid FD.

status_t setupExternalServer (binder::unique_fd serverFd)

Set up server using an external FD previously set up by releaseServer().

Return false if there's already a server.

void setMaxThreads (size_t threads)

This must be called before adding a client session. This corresponds

to the number of incoming connections to RpcSession objects in the

server, which will correspond to the number of outgoing connections

in client RpcSession objects.

If this is not specified, this will be a single-threaded server.

TODO(b/167966510): these are currently created per client, but these

should be shared.

size_t getMaxThreads ()
bool setProtocolVersion (uint32_t version)

By default, the latest protocol version which is supported by a client is

used. However, this can be used in order to prevent newer protocol

versions from ever being used. This is expected to be useful for testing.

void setSupportedFileDescriptorTransportModes (const std::vector<RpcSession::FileDescriptorTransportMode> & modes)

Set the supported transports for sending and receiving file descriptors.

Clients will propose a mode when connecting. If the mode is not in the

provided list, the connection will be rejected.

void setRootObject (const sp<IBinder> & binder)

The root object can be retrieved by any client, without any

authentication. TODO(b/183988761)

Holds a strong reference to the root object.

void setRootObjectWeak (const wp<IBinder> & binder)

Holds a weak reference to the root object.

void setPerSessionRootObject (std::function<sp<IBinder> (wp<RpcSession>, const void *, size_t)> && object)

Allows a root object to be created for each session.

Takes one argument: a callable that is invoked once per new session.

The callable takes three arguments:

- a weak pointer to the session. If you want to hold onto this in the root object, then

you should keep a weak pointer, and promote it when needed. For instance, if you refer

to this from the root object, then you could get ahold of transport-specific information.

- a type-erased pointer to an OS- and transport-specific address structure, e.g.,

sockaddr_vm for vsock

- an integer representing the size in bytes of that structure. The callable should

validate the size, then cast the type-erased pointer to a pointer to the actual type of the

address, e.g., const void* to const sockaddr_vm*.

sp<IBinder> getRootObject ()
void setConnectionFilter (std::function<bool (const void *, size_t)> && filter)

Set optional filter of incoming connections based on the peer's address.

Takes one argument: a callable that is invoked on each accept()-ed

connection and returns false if the connection should be dropped.

See the description of setPerSessionRootObject() for details about

the callable's arguments.

void setServerSocketModifier (std::function<void (binder::borrowed_fd)> && modifier)

Set optional modifier of each newly created server socket.

The only argument is a successfully created file descriptor, not bound to an address yet.

std::vector<uint8_t> getCertificate (RpcCertificateFormat )

See RpcTransportCtx::getCertificate

void start ()

Runs join() in a background thread. Immediately returns.

void join ()

You must have at least one client session before calling this.

If a client needs to actively terminate join, call shutdown() in a separate thread.

At any given point, there can only be one thread calling join().

Warning: if shutdown is called, this will return while the shutdown is

still occurring. To ensure that the service is fully shutdown, you might

want to call shutdown after 'join' returns.

bool shutdown ()

Shut down any existing join(). Return true if successfully shut down, false otherwise

(e.g. no join() is running). Will wait for the server to be fully

shutdown.

Warning: this will hang if it is called from its own thread.

std::vector<sp<RpcSession>> listSessions ()

For debugging!

size_t numUninitializedSessions ()
bool hasActiveRequests ()

Whether any requests are currently being processed.

void ~RpcServer ()

Friends

class sp
class RpcServerTrusty