capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

dynamic-capability.c++ (4485B)


      1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
      2 // Licensed under the MIT License:
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a copy
      5 // of this software and associated documentation files (the "Software"), to deal
      6 // in the Software without restriction, including without limitation the rights
      7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 // copies of the Software, and to permit persons to whom the Software is
      9 // furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 // THE SOFTWARE.
     21 
     22 // This file contains the parts of dynamic.h that live in capnp-rpc.so.
     23 
     24 #include "dynamic.h"
     25 #include <kj/debug.h>
     26 
     27 namespace capnp {
     28 
     29 DynamicCapability::Client DynamicCapability::Client::upcast(InterfaceSchema requestedSchema) {
     30   KJ_REQUIRE(schema.extends(requestedSchema), "Can't upcast to non-superclass.") {}
     31   return DynamicCapability::Client(requestedSchema, hook->addRef());
     32 }
     33 
     34 Request<DynamicStruct, DynamicStruct> DynamicCapability::Client::newRequest(
     35     InterfaceSchema::Method method, kj::Maybe<MessageSize> sizeHint) {
     36   auto methodInterface = method.getContainingInterface();
     37 
     38   KJ_REQUIRE(schema.extends(methodInterface), "Interface does not implement this method.");
     39 
     40   auto paramType = method.getParamType();
     41   auto resultType = method.getResultType();
     42 
     43   auto typeless = hook->newCall(
     44       methodInterface.getProto().getId(), method.getIndex(), sizeHint);
     45 
     46   return Request<DynamicStruct, DynamicStruct>(
     47       typeless.getAs<DynamicStruct>(paramType), kj::mv(typeless.hook), resultType);
     48 }
     49 
     50 Request<DynamicStruct, DynamicStruct> DynamicCapability::Client::newRequest(
     51     kj::StringPtr methodName, kj::Maybe<MessageSize> sizeHint) {
     52   return newRequest(schema.getMethodByName(methodName), sizeHint);
     53 }
     54 
     55 Capability::Server::DispatchCallResult DynamicCapability::Server::dispatchCall(
     56     uint64_t interfaceId, uint16_t methodId,
     57     CallContext<AnyPointer, AnyPointer> context) {
     58   KJ_IF_MAYBE(interface, schema.findSuperclass(interfaceId)) {
     59     auto methods = interface->getMethods();
     60     if (methodId < methods.size()) {
     61       auto method = methods[methodId];
     62       auto resultType = method.getResultType();
     63       return {
     64         call(method, CallContext<DynamicStruct, DynamicStruct>(*context.hook,
     65             method.getParamType(), resultType)),
     66         resultType.isStreamResult()
     67       };
     68     } else {
     69       return internalUnimplemented(
     70           interface->getProto().getDisplayName().cStr(), interfaceId, methodId);
     71     }
     72   } else {
     73     return internalUnimplemented(schema.getProto().getDisplayName().cStr(), interfaceId);
     74   }
     75 }
     76 
     77 RemotePromise<DynamicStruct> Request<DynamicStruct, DynamicStruct>::send() {
     78   auto typelessPromise = hook->send();
     79   hook = nullptr;  // prevent reuse
     80   auto resultSchemaCopy = resultSchema;
     81 
     82   // Convert the Promise to return the correct response type.
     83   // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the
     84   // Pipeline part of the RemotePromise.
     85   auto typedPromise = kj::implicitCast<kj::Promise<Response<AnyPointer>>&>(typelessPromise)
     86       .then([=](Response<AnyPointer>&& response) -> Response<DynamicStruct> {
     87         return Response<DynamicStruct>(response.getAs<DynamicStruct>(resultSchemaCopy),
     88                                        kj::mv(response.hook));
     89       });
     90 
     91   // Wrap the typeless pipeline in a typed wrapper.
     92   DynamicStruct::Pipeline typedPipeline(resultSchema,
     93       kj::mv(kj::implicitCast<AnyPointer::Pipeline&>(typelessPromise)));
     94 
     95   return RemotePromise<DynamicStruct>(kj::mv(typedPromise), kj::mv(typedPipeline));
     96 }
     97 
     98 kj::Promise<void> Request<DynamicStruct, DynamicStruct>::sendStreaming() {
     99   KJ_REQUIRE(resultSchema.isStreamResult());
    100 
    101   auto promise = hook->sendStreaming();
    102   hook = nullptr;  // prevent reuse
    103   return promise;
    104 }
    105 
    106 }  // namespace capnp