vibe.web.rpc
Web based, bi-directional, concurrent RPC implementation.
This module implements a generic RPC mechanism that allows transparently calling remote functions over an HTTP based network connection. The current implementation is based on a WebSocket based protocol, serializing method arguments and return types as BSON.
The RPC API is defined using interfaces, very similar to the system in vibe.web.rest. It supports methods with or without a return value, normal, ref and out parameters, exceptions, properties returning interfaces, and properties returning vibe.web.rest.Collection!I.
Authorization and authentication is supported via the vibe.web.auth framework. When using it, the authenticate method should be defined as @noRoute T authenticate(ref const WebRPCPerrInfo), where T is the type passed to the @requiresAuth UDA.
Any remote function calls can execute concurrently, so that the connection never gets blocked by an unfinished function call.
Note that this system will establish a bi-directional communication facility, allowing both, the client and the server, to initiate calls. This effectively forms a peer-to-peer connection instead of a client-server connection. The advantage of using HTTP as the basis is that this makes it easy to establish P2P connections where one of the peers is behind a firewall or NAT layer, but the other peer can be reached through a public port or through a (reverse) proxy server.
Defining_a_simple_RPC_interface:
The API for the interface is defined as a normal D interface:
interface ExampleAPI {
void performSomeAction();
int getSomeInformation();
}An implementation of this interface is required on both, the server and the client side:
class ExampleAPIImplementation : ExampleAPI {
void performSomeAction() { ... }
int getSomeInformation() { return ...; }
}With this defined, this is the basic code needed to set up the server side:
void handleIncomingPeer(WebRPCPeer!ExampleAPI peer)
@safe nothrow {
// this gets executed for any client that connects to the server
try {
peer.performSomeAction();
} catch (Exception e) {
logException(e, "Failed to perform peer action");
}
}
auto r = new URLRouter;
r.registerWebRPC!ExampleAPI(r, "/rpc", new ExampleAPIImplementation, &handlePeer);
// could register other routes here, such as for a web or REST interface
auto l = listenHTTP("127.0.0.1:1234", r);A client can now connect to the server and access the API as well:
auto peer = connectWebRPC(URL("http://127.0.0.1:1234/rpc"),
new ExampleAPIImplementation);
peer.performSomeAction();Copyright
Types 9
Provides information about a peer;
Reference counted type used to access a peer's API.
This struct defines an alias this to its implementation property in order to provide an interface implementation of I. Any calls on the methods of this implementation will be forwarded to the remote peer.
Note that the WebRPC connection will be closed as soon as the last instance of a connected WebRPCPeer gets destroyed.
WebRPCPeerImpl!(I, I, "") m_implconst(WebRPCPeerInfo) peerInformation() @property ref constProvides information about the remote peer.this(WebRPCPeerImpl!(I, I, "") impl)WebSocketHandler!RootI m_handlerstaticMap!(SubPeerImpl, Info.SubInterfaceFunctions) m_subInterfacesReturnType!method performCall(alias method, PARAMS...)(auto ref PARAMS params)this(WebSocketHandler!RootI handler)I m_implWebRPCPeerInfo m_peerInfoint m_refCountWebSocket m_socketTaskMutex m_sendMutexulong m_sequenceRes[ulong] m_availableResponsesLocalManualEvent m_responseEventvoid addRef()void releaseRef()ulong sendCall(string method, Bson arguments)void sendResponse(ulong sequence, Bson result)void sendErrorResponse(ulong sequence, string error_message)Bson waitForResponse(ulong sequence)void runReadLoop() nothrowBson invokeMethod(string name, Bson arguments)Bson invokeMethodF(SI, alias method, string qualified_name)(Bson arguments)auto resolveImpl(string qualified_name, RI)(RI base, Bson arguments) if (is(RI == interface))void resolveArguments(alias method, SI)(SI impl, Bson arguments, out typeof(ParameterTypeTuple!method.init) args)this(return WebSocket ws, I impl, ref const(WebRPCPeerInfo) peer_info)Resulong sequencestring methodBson argumentsulong sequenceBson resultulong sequencestring messageFunctions 5
void registerWebRPC(I)(URLRouter router, string path, I implementation,
WebRPCPeerCallback!I peer_callback) if (is(I == interface))Registers a route for handling incoming WebRPC requests.WebRPCPeer!I connectWebRPC(I)(URL url, I implementation) if (is(I == interface))Connects to a WebRPC endpoint.void handleWebRPC(I)(I implementation, WebRPCPeerCallback!I peer_callback,
scope HTTPServerRequest req, scope HTTPServerResponse res)