librpc
server.c

An example that shows how to set up a basic librpc server and register methods on it.

To run with the default URI: ./build/examples/server/example-server To specify the URI: ./build/examples/server/example-server unix://test2.sock Note that the unix domain socket example requires that client and server be run from the same directory specifying the same socket.

/*
* Copyright 2017 Two Pore Guys, Inc.
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <glib.h>
#include <rpc/object.h>
#include <rpc/service.h>
#include <rpc/server.h>
#ifndef __unused
#define __unused __attribute__((unused))
#endif
static void
server_event(void *arg __unused, rpc_connection_t conn,
rpc_server_event_t event)
{
const char *addr;
if (event == RPC_SERVER_CLIENT_CONNECT)
printf("client %s connected\n", addr);
if (event == RPC_SERVER_CLIENT_DISCONNECT)
printf("client %s disconnected\n", addr);
}
hello(void *cookie __unused, rpc_object_t args)
{
(void)cookie;
return rpc_string_create_with_format("hello %s!",
}
int
main(int argc, const char *argv[])
{
rpc_object_t error;
__block rpc_server_t srv;
__block GRand *rand = g_rand_new();
__block gint setcnt = g_rand_int_range(rand, 50, 500);
__block char *strg = g_malloc(27);
rpc_context_register_func(ctx, NULL, "hello",
NULL, hello);
rpc_context_register_block(ctx, NULL, "block",
NULL, ^(void *cookie __unused, rpc_object_t args __unused) {
return (rpc_string_create("haha lol"));
});
rpc_context_register_block(ctx, NULL, "delay",
NULL, ^(void *cookie __unused, rpc_object_t args __unused) {
sleep(5);
return (rpc_int64_create(42));
});
rpc_context_register_block(ctx, NULL, "event",
NULL, ^(void *cookie __unused, rpc_object_t args __unused) {
rpc_server_broadcast_event(srv, NULL, NULL, "server.hello",
rpc_string_create("world"));
rpc_server_broadcast_event(srv, NULL, NULL, "oh_noes",
return (rpc_null_create());
});
strcpy(strg, "abcdefghijklmnopqrstuvwxyz");
rpc_context_register_block(ctx, NULL, "stream",
NULL, ^rpc_object_t (void *cookie, rpc_object_t args __unused) {
int cnt = 0;
gint i;
while (cnt < setcnt) {
cnt++;
i = g_rand_int_range (rand, 0, 26);
res = rpc_object_pack("[s, i, i]",
strg + i, (int64_t)(26-i), (int64_t)cnt);
fprintf(stderr, "returning %s, %d letters, %d of %d\n",
rpc_array_get_string(res, 0), 26-i, cnt, setcnt);
if (rpc_function_yield(cookie, res) != 0) {
fprintf(stderr, "yield failed\n");
return (rpc_null_create());
}
}
return (rpc_null_create());
});
if (argc > 1)
srv = rpc_server_create(argv[1], ctx);
else
srv = rpc_server_create("tcp://0.0.0.0:5000", ctx);
if (srv == NULL) {
error = rpc_get_last_error();
fprintf(stderr, "Cannot create server: %s\n",
return (1);
}
sleep(2);
printf("Resuming server now\n");
pause();
return (0);
}