blob: 3612a1d2cba01b7de802ab5c6c70169ac965e353 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#ifndef DEF_SERVER_H
#define DEF_SERVER_H
#include "syscall.h"
#include "obj.h"
//parameter/return values types
#define PT_VOID 0
#define PT_OBJDESC 1
#define PT_LONG 2
#define PT_LONGLONG 3 //for return values
#define PT_SHM 3
struct method_data {
struct {
union {
int i;
void* p;
};
int type;
int keepShm; //for messages : keep shared memory segment after return or unmap ? (default : 0 = unmap)
size_t shmsize;
} parameters[3];
uint32_t func;
int pid;
int blocking; //1 : blocking request, 0 : message
struct object_srv *obj;
};
struct method_ret {
union {
int i;
int64_t l;
};
int type;
int status; //= error code if any
};
//helper function for creating return values
struct method_ret mr_long(int val);
struct method_ret mr_llong(int64_t val);
struct method_ret mr_obj(Object obj);
struct method_ret mr_srv(struct object_srv* obj);
struct method_ret mr_void();
struct method_ret mr_err(int error);
// for checking if a string passed in shared memory is valid
#define CHKSSTR(md, n) { size_t _i, _ok = 0; if (md->parameters[n].p == 0) return mr_err(-1); \
for (_i = 0; _i < md->parameters[n].shmsize; _i++) { \
if (*((char*)md->parameters[n].p + _i) == 0) { \
_ok = 1; break; \
} } \
if (!_ok && md->parameters[n].shmsize != 0) return mr_err(-1); }
typedef struct method_ret (*method_handler)(struct method_data*);
struct method_srv {
uint32_t id;
method_handler h;
struct method_srv *next;
};
struct object_srv {
int id; //descriptor
struct method_srv *methods;
void *data;
struct object_srv *next;
};
typedef struct object_srv Server;
extern Server procServer; //corresponds to descriptor 0.
//possible actions for srv_handle
#define HA_ONCE 1 //check if requests are waiting, if so handle them
#define HA_WAIT 2 //check if requests are waiting, if so handle them, else wait for one to come and handle it
#define HA_LOOP 3 //wait for requests to come, handling them in an infinite loop
void srv_handleAll(); //check all objects once
void srv_handle(Server* o, int act);
Server *srv_create();
void srv_delete(Server *o);
Server *srv_get(int descriptor);
void srv_addHandler(Server* o, uint32_t method, method_handler h);
#endif
|