Annotation of libaitrpc/inc/aitrpc.h, revision 1.29.4.1
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.29.4.1! misho 6: * $Id: aitrpc.h,v 1.29 2024/03/20 17:32:30 misho Exp $
1.1 misho 7: *
1.2 misho 8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.29 misho 15: Copyright 2004 - 2024
1.2 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.1 misho 46: #ifndef __AITRPC_H
47: #define __AITRPC_H
48:
49:
50: #include <assert.h>
1.28 misho 51: #include <pthread.h>
1.9 misho 52: #include <sys/queue.h>
1.28 misho 53: #include <aitrpc_pkt.h>
54: #include <aitrpc_cli.h>
1.6 misho 55: #include <aitsched.h>
1.1 misho 56:
57:
1.9 misho 58: #define RPC_CALLBACK_CHECK_INPUT(x) do { \
59: assert((x)); \
60: if (!(x)) { \
1.6 misho 61: rpc_SetErr(EINVAL, \
62: "Invalid callback parameters ..."); \
1.2 misho 63: return -1; \
64: } \
65: } while (0)
1.1 misho 66:
67:
1.28 misho 68: /* Network RPC server elements */
1.1 misho 69:
1.9 misho 70: /* RPC function registration element! */
71: typedef struct tagRPCFunc {
72: ait_val_t func_name;
1.1 misho 73:
1.9 misho 74: void *func_parent;
1.11 misho 75:
76: SLIST_ENTRY(tagRPCFunc) func_next;
77: AVL_ENTRY(tagRPCFunc) func_node;
1.9 misho 78: } rpc_func_t;
79: #define RPC_FUNC_SERVER(x) ((rpc_srv_t*) (x)->func_parent)
1.8 misho 80:
1.11 misho 81: /* Tree root node */
82: typedef struct tagRPCFuncs {
83: pthread_mutex_t mtx;
84:
85: struct tagRPCFunc *slh_first;
86: struct tagRPCFunc *avlh_root;
87: } rpc_funcs_t;
88: #define RPC_FUNCS_LOCK(x) pthread_mutex_lock(&(x)->mtx)
89: #define RPC_FUNCS_UNLOCK(x) pthread_mutex_unlock(&(x)->mtx)
90: #define RPC_FUNCS_ISEMPTY(x) AVL_EMPTY((x))
91:
1.1 misho 92:
1.9 misho 93: /* BLOB register element */
1.2 misho 94: typedef struct tagBLOB {
1.9 misho 95: uint32_t blob_var; /* BLOB id */
1.2 misho 96:
1.9 misho 97: size_t blob_len; /* size of allocated BLOB data */
98: void *blob_data; /* mapped BLOB data */
1.2 misho 99:
1.9 misho 100: TAILQ_ENTRY(tagBLOB) blob_node;
1.2 misho 101: } rpc_blob_t;
102:
1.9 misho 103:
1.1 misho 104: typedef struct {
1.9 misho 105: rpc_sess_t srv_session; /* RPC session registration info */
106: int srv_netbuf; /* size of network buffer */
1.12 misho 107: int srv_proto; /* Server protocol */
1.2 misho 108:
1.9 misho 109: pthread_t srv_tid; /* RPC exec pthread */
110: sched_root_task_t *srv_root; /* RPC server scheduler */
111: intptr_t srv_kill; /* Scheduler condition variable */
112:
113: rpc_cli_t srv_server; /* RPC server socket */
114: array_t *srv_clients; /* connected rpc client sockets */
115:
1.11 misho 116: rpc_funcs_t srv_funcs; /* RPC functions */
1.2 misho 117:
118: struct {
1.9 misho 119: pthread_t tid; /* BLOB exec pthread */
120: sched_root_task_t *root; /* BLOB server scheduler */
121: intptr_t kill; /* BLOB server state: ==0 disable | !=0 enable */
1.2 misho 122:
1.9 misho 123: ait_val_t dir; /* BLOB states directory */
1.1 misho 124:
1.9 misho 125: rpc_cli_t server; /* BLOB server socket */
126: array_t *clients; /* connected blob client sockets */
127:
128: TAILQ_HEAD(, tagBLOB) blobs; /* registered blob variables list */
129: } srv_blob;
1.1 misho 130: } rpc_srv_t;
131:
132:
1.2 misho 133: /*
1.6 misho 134: * (*rpc_callback_t)() - Callback type definition for RPC call in server process
135: *
1.9 misho 136: * @arg1 = RPC client
137: * @arg2 = RPC packet header
1.2 misho 138: * @arg3 = input array with values from RPC call execution request
139: * return: -1 error or >-1 success execution
140: */
1.9 misho 141: typedef int (*rpc_callback_t)(rpc_cli_t *, struct tagRPCCall *, array_t *);
1.1 misho 142:
1.24 misho 143: #define RPC_CALL_DEFINE(x) int (x)(rpc_cli_t*, struct tagRPCCall*, array_t*)
144: #define RPC_CALL_ARGS(arg1, arg2, arg3) rpc_cli_t* arg1, struct tagRPCCall* arg2, array_t* arg3
145: #define RPC_CALL_STDARGS RPC_CALL_ARGS(cli, rpc, iv)
146:
1.1 misho 147:
1.29 misho 148: /*
149: * rpc_srv_DispatchSignal() - Enable/Disable Signal dispatcher for RPC scheduler
150: *
151: * @x = RPC server instance
152: * @y = Enable or =0 Disable
153: * return: 0 enabled signal dispatcher. See schedSignalDispatch() in libaitsched
154: */
155: #define rpc_srv_DispatchSignal(x, y) schedSignalDispatch((x)->srv_root, (y))
156:
1.9 misho 157: /* ----------------------------------------------------------------------- */
1.1 misho 158:
1.29.4.1! misho 159: #ifdef __cplusplus
! 160: extern "C" {
! 161: #endif
! 162:
1.9 misho 163: /*
164: * rpc_register_srvPing() - Register ping service function
165: *
166: * @srv = RPC server instance
167: * return: -1 error or 0 ok
168: */
1.15 misho 169: int rpc_register_srvPing(rpc_srv_t * __restrict srv);
1.9 misho 170: /*
171: * rpc_register_srvServices() - Register internal service functions
172: *
173: * @srv = RPC server instance
174: * return: -1 error or 0 ok
175: */
176: int rpc_register_srvServices(rpc_srv_t * __restrict srv);
177: /*
178: * rpc_register_blobServices() - Register internal service functions
179: *
180: * @srv = RPC server instance
181: * return: -1 error or 0 ok
182: */
183: int rpc_register_blobServices(rpc_srv_t * __restrict srv);
1.5 misho 184:
185:
1.1 misho 186: /* RPC Server side functions */
187:
188: /*
1.6 misho 189: * rpc_srv_initServer() - Init & create RPC Server
190: *
1.14 misho 191: * @InstID = Instance for authentication & recognition
1.1 misho 192: * @concurentClients = Concurent clients at same time to this server
1.9 misho 193: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.3 misho 194: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 195: * @Port = Port for bind server, if Port == 0 default port is selected
1.12 misho 196: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 197: * return: NULL == error or !=NULL bind and created RPC server instance
198: */
1.14 misho 199: rpc_srv_t *rpc_srv_initServer(unsigned char InstID, int concurentClients, int netBuf,
1.12 misho 200: const char *csHost, unsigned short Port, int proto);
1.1 misho 201: /*
1.6 misho 202: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
203: *
1.5 misho 204: * @psrv = RPC Server instance
1.1 misho 205: * return: none
206: */
1.15 misho 207: void rpc_srv_endServer(rpc_srv_t ** __restrict psrv);
1.1 misho 208: /*
1.6 misho 209: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
210: *
1.1 misho 211: * @srv = RPC Server instance
212: * return: -1 error or 0 ok, infinite loop ...
213: */
1.4 misho 214: int rpc_srv_loopServer(rpc_srv_t * __restrict srv);
1.25 misho 215: #define rpc_srv_execServer(_srv, _sync) \
216: do { assert((_srv)); \
217: if (!(_srv)->srv_kill) { \
218: pthread_create(&(_srv)->srv_tid, NULL, (void*(*)(void*)) \
219: rpc_srv_loopServer, (_srv)); \
220: if ((_sync)) \
221: pthread_join((_srv)->srv_tid, (void**) (_sync)); \
222: else \
223: pthread_detach((_srv)->srv_tid); \
224: } } while (0)
225: #define rpc_srv_killServer(_srv) \
226: (assert((_srv)), (_srv)->srv_blob.kill = 1, (_srv)->srv_kill = 1)
1.1 misho 227:
228: /*
1.6 misho 229: * rpc_srv_initBLOBServer() - Init & create BLOB Server
230: *
1.3 misho 231: * @srv = RPC server instance
1.2 misho 232: * @Port = Port for bind server, if Port == 0 default port is selected
233: * @diskDir = Disk place for BLOB file objects
234: * return: -1 == error or 0 bind and created BLOB server instance
235: */
1.9 misho 236: int rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, unsigned short Port, const char *diskDir);
1.2 misho 237: /*
1.6 misho 238: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
239: *
1.2 misho 240: * @srv = RPC Server instance
241: * return: none
242: */
1.15 misho 243: void rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv);
1.2 misho 244: /*
1.6 misho 245: * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
246: *
1.2 misho 247: * @srv = RPC Server instance
248: * return: -1 error or 0 ok, infinite loop ...
249: */
1.9 misho 250: int rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv);
1.25 misho 251: #define rpc_srv_execBLOBServer(_srv) \
252: do { assert((_srv)); \
253: if (!(_srv)->srv_kill && !(_srv)->srv_blob.kill) { \
254: pthread_create(&(_srv)->srv_blob.tid, NULL, \
255: (void*(*)(void*)) rpc_srv_loopBLOBServer, (_srv)); \
256: pthread_detach((_srv)->srv_blob.tid); \
257: } \
258: } while (0)
259:
260: /*
261: * rpc_srv_initServer2() - Init & create layer2 RPC Server
262: *
263: * @InstID = Instance for authentication & recognition
264: * @concurentClients = Concurent clients at same time to this server
265: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
266: * @csIface = Interface name for bind server, if NULL first interface on host
267: * return: NULL == error or !=NULL bind and created RPC server instance
268: */
269: rpc_srv_t *rpc_srv_initServer2(u_char InstID, int concurentClients, int netBuf,
270: const char *csIface);
1.2 misho 271:
272: /*
1.26 misho 273: * rpc_srv_initServerExt() - Init & create pipe RPC Server
274: *
275: * @InstID = Instance for authentication & recognition
276: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
277: * @fd = File descriptor
278: * return: NULL == error or !=NULL bind and created RPC server instance
279: */
280: rpc_srv_t *rpc_srv_initServerExt(u_char InstID, int netBuf, int fd);
281:
282: /*
1.6 misho 283: * rpc_srv_registerCall() - Register call to RPC server
284: *
1.1 misho 285: * @srv = RPC Server instance
1.9 misho 286: * @tag = Function tag
287: * @funcaddr = Function address
288: * return: -1 error, 0 already registered tag or 1 register ok
1.1 misho 289: */
1.9 misho 290: int rpc_srv_registerCall(rpc_srv_t * __restrict srv, unsigned short tag, void *funcaddr);
1.1 misho 291: /*
1.6 misho 292: * rpc_srv_unregisterCall() - Unregister call from RPC server
293: *
1.1 misho 294: * @srv = RPC Server instance
1.9 misho 295: * @tag = Function tag
1.1 misho 296: * return: -1 error, 0 not found call, 1 unregister ok
297: */
1.9 misho 298: int rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, unsigned short tag);
1.1 misho 299: /*
1.9 misho 300: * rpc_srv_getCall() - Get registered call from RPC server
1.6 misho 301: *
1.1 misho 302: * @srv = RPC Server instance
303: * @tag = tag for function
304: * return: NULL not found call, !=NULL return call
305: */
1.15 misho 306: rpc_func_t *rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag);
1.1 misho 307: /*
1.9 misho 308: * rpc_srv_execCall() Execute registered call from RPC server
1.6 misho 309: *
1.9 misho 310: * @cli = RPC client
1.1 misho 311: * @rpc = IN RPC call structure
1.9 misho 312: * @funcname = Execute RPC function
1.4 misho 313: * @args = IN RPC calling arguments from RPC client
1.1 misho 314: * return: -1 error, !=-1 ok
315: */
1.9 misho 316: int rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
317: ait_val_t funcname, array_t * __restrict args);
1.1 misho 318:
319:
1.2 misho 320: /*
1.9 misho 321: * rpc_srv_blobCreate() - Create and map blob to memory region and return object
1.6 misho 322: *
1.2 misho 323: * @srv = RPC Server instance
324: * @len = BLOB length object
1.16 misho 325: * @tout = BLOB live timeout in seconds
1.2 misho 326: * return: NULL error or !=NULL allocated BLOB object
327: */
1.16 misho 328: rpc_blob_t *rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len, int tout);
1.2 misho 329: /*
1.6 misho 330: * rpc_srv_blobMap() - Map blob to memory region
331: *
1.2 misho 332: * @srv = RPC Server instance
333: * @blob = Map to this BLOB element
334: * return: -1 error or 0 ok
335: */
1.15 misho 336: int rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
1.2 misho 337: /*
1.6 misho 338: * rpc_srv_blobUnmap() - Unmap blob memory region
339: *
1.2 misho 340: * @blob = Mapped BLOB element
341: * return: none
342: */
1.15 misho 343: void rpc_srv_blobUnmap(rpc_blob_t * __restrict blob);
1.2 misho 344: /*
1.6 misho 345: * rpc_srv_blobFree() - Free blob from disk & memory
346: *
1.2 misho 347: * @srv = RPC Server instance
348: * @blob = Mapped BLOB element
349: * return: -1 error or 0 ok
350: */
1.15 misho 351: int rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
1.2 misho 352:
353: /*
1.6 misho 354: * rpc_srv_registerBLOB() - Register new BLOB to server
355: *
1.2 misho 356: * @srv = RPC Server instance
357: * @len = BLOB length
1.16 misho 358: * @tout = BLOB live timeout in seconds
1.2 misho 359: * return: NULL error or new registered BLOB
360: */
1.16 misho 361: rpc_blob_t *rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len, int tout);
1.2 misho 362: /*
1.6 misho 363: * rpc_srv_unregisterBLOB() - Unregister BLOB from server
364: *
1.2 misho 365: * @srv = RPC Server instance
366: * @var = BLOB Variable for unregister
367: * return: -1 error, 0 not found call, 1 unregister ok
368: */
369: int rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var);
370: /*
1.6 misho 371: * rpc_srv_getBLOB() - Get registered BLOB
372: *
1.2 misho 373: * @srv = RPC Server instance
374: * @var = hash for variable
375: * return: NULL not found, !=NULL return blob var
376: */
1.15 misho 377: rpc_blob_t *rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var);
1.2 misho 378:
379: /*
1.6 misho 380: * rpc_srv_sendBLOB() - Send mapped BLOB to client
381: *
1.2 misho 382: * @cli = Client instance
383: * @blob = Mapped BLOB element
384: * return: -1 error, 0 ok
385: */
386: int rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
387: /*
1.6 misho 388: * rpc_srv_recvBLOB() - Receive BLOB from client
389: *
1.2 misho 390: * @cli = Client instance
391: * @blob = Mapped BLOB element
392: * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
393: */
394: int rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
395:
1.26 misho 396:
1.29 misho 397: /*
398: * rpc_srv_Return() - Prepare IPC return answer to RPC client
399: *
400: * @c = RPC client
401: * return: number of arguments in response
402: */
403: int rpc_srv_Return(rpc_cli_t *c);
404:
1.29.4.1! misho 405: #ifdef __cplusplus
! 406: }
! 407: #endif
1.29 misho 408:
1.1 misho 409: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>