c backend refact, implementing serilizer
[PxpRpc.git] / c / include / pxprpc.h
blob4f9a66e9c8a8e40e6fc73029caa8d329d0ff1d01
2 #ifndef _PXPRPC_H
3 #define _PXPRPC_H
5 /* config */
6 #define pxprpc__malloc malloc
7 #define pxprpc__free free
8 #define pxprpc__realloc realloc
10 /* use include pthread to create mutex */
11 #define USE_PTHREAD_HEADER
13 #define MAX_REFSLOTS_COUNT 256
17 /* definition */
18 #include <string.h>
19 #include <stdint.h>
22 struct pxprpc_abstract_io{
23 //onCompleted is called when expected length data is read or error occured.
24 void (*read)(struct pxprpc_abstract_io *self,uint32_t length,uint8_t *buf,void (*onCompleted)(void *args),void *p);
25 //onCompleted is called when buf is processed and can be free or error occured.
26 //Write request should be processed in order.
27 void (*write)(struct pxprpc_abstract_io *self,uint32_t length,const uint8_t *buf,void (*onCompleted)(void *args),void *p);
28 //get last error. For example, to get error caused by read, call "io->get_error(io,io->read)". return NULL if no error
29 const char *(*get_error)(struct pxprpc_abstract_io *self,void *fn);
30 void *userData;
33 struct pxprpc_bytes{
34 uint32_t length;
35 //variable length array
36 uint8_t data[1];
39 struct pxprpc_serilizer{
40 uint8_t *buf;
41 uint32_t cap;
42 uint32_t pos;
45 extern void pxprpc_ser_prepare_ser(struct pxprpc_serilizer *ser,int initBufSize);
46 extern void pxprpc_ser_prepare_unser(struct pxprpc_serilizer *ser,void *buf,uint32_t size);
47 extern void pxprpc_ser_put_int(struct pxprpc_serilizer *ser,uint32_t val);
48 extern void pxprpc_ser_put_long(struct pxprpc_serilizer *ser,uint64_t val);
49 extern void pxprpc_ser_put_float(struct pxprpc_serilizer *ser,float val);
50 extern void pxprpc_ser_put_double(struct pxprpc_serilizer *ser,double val);
51 extern void pxprpc_ser_put_bytes(struct pxprpc_serilizer *ser,struct pxprpc_bytes *val);
52 extern uint32_t pxprpc_ser_get_int(struct pxprpc_serilizer *ser);
53 extern uint64_t pxprpc_ser_get_long(struct pxprpc_serilizer *ser);
54 extern float pxprpc_ser_get_float(struct pxprpc_serilizer *ser);
55 extern double pxprpc_ser_get_double(struct pxprpc_serilizer *ser);
56 extern struct pxprpc_bytes *pxprpc_ser_get_bytes(struct pxprpc_serilizer *ser);
60 typedef void *pxprpc_server_context;
62 struct pxprpc_object{
63 void *object1;
64 uint32_t (*addRef)(struct pxprpc_object *self);
65 uint32_t (*release)(struct pxprpc_object *self);
66 //count is used by object internally to save the reference count,
67 //pxprpc server only use 'addRef' and 'release'.
68 uint16_t count;
69 //type is used to identify the object type.
70 //For pxprpc internal object 'pxprpc_bytes' ,type=1.
71 //type<16 are reserved by pxprpc for future use,
72 //type>=16 are user-defined types.
73 uint16_t type;
76 struct pxprpc_request{
77 struct pxprpc_abstract_io *io1;
78 struct pxprpc_object **ref_slots;
79 uint32_t dest_addr;
80 uint32_t session;
81 struct pxprpc_callable *callable;
82 void *callable_data;
83 pxprpc_server_context server_context;
84 struct pxprpc_object *result;
88 struct pxprpc_callable{
89 void (*readParameter)(struct pxprpc_callable *self,struct pxprpc_request *r,void (*doneCallback)(struct pxprpc_request *r));
90 void (*call)(struct pxprpc_callable *self,struct pxprpc_request *r,void (*onResult)(struct pxprpc_request *r,struct pxprpc_object *result));
91 void (*writeResult)(struct pxprpc_callable *self,struct pxprpc_request *r);
92 void *userData;
95 struct pxprpc_namedfunc{
96 const char *name;
97 struct pxprpc_callable *callable;
100 typedef struct pxprpc_server_api{
101 int (*context_new)(pxprpc_server_context *server_context,struct pxprpc_abstract_io *io1,
102 struct pxprpc_namedfunc *namedfuncs,int len_namedfuncs);
103 int (*context_start)(pxprpc_server_context);
104 int (*context_closed)(pxprpc_server_context);
105 int (*context_close)(pxprpc_server_context);
106 int (*context_delete)(pxprpc_server_context *);
107 struct pxprpc_object *(*new_object)(void *obj);
108 struct pxprpc_object *(*new_bytes_object)(uint32_t size);
109 void (*fill_bytes_object)(struct pxprpc_object *obj,uint8_t *data,int size);
110 }pxprpc_server_api;
112 extern int pxprpc_server_query_interface(pxprpc_server_api **outapi);
115 #endif