To implement a server, create a class that inherits from your_module_capnp.YourInterface.Server.
Rules for implementation:
- Method Names: Must match the interface exactly, or have
_context appended to them. - Standard Methods: If the name matches the interface, you receive arguments as keyword arguments (matching the spec) and a special
_context parameter. It is recommended to include **kwargs to maintain compatibility if the interface evolves. - Context Methods: If the name ends in
_context, you only receive the context parameter. You must manually access context.params and set context.results. - Return Values: If you return a promise, it is handled as a Cap'n Proto promise. Otherwise, the return statement values are mapped to the results struct in the order defined in the schema.
Example Schema:
interface TestInterface {
foo @0 (i :UInt32, j :Bool) -> (x: Text, i:UInt32);
}
class TestInterface(capability_capnp.TestInterface.Server):
# Standard method: arguments match spec, returns a tuple for results
def foo(self, i, j, **kwargs):
return str(j), i
# Context method: manual param/result handling
def defFunction_context(self, context):
params = context.params
context.results.func = FunctionImpl(params.paramCount, params.body)