-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
77 lines (64 loc) · 1.76 KB
/
server.c
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
#include "rpc.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
rpc_data *add2_i8(rpc_data *);
rpc_data *multi2_i8(rpc_data *in);
int main(int argc, char *argv[]) {
rpc_server *state;
state = rpc_init_server(3000);
if (state == NULL) {
fprintf(stderr, "Failed to init\n");
exit(EXIT_FAILURE);
}
if (rpc_register(state, "add2", add2_i8) == -1) {
fprintf(stderr, "Failed to register add2\n");
exit(EXIT_FAILURE);
}
if (rpc_register(state, "multi2", multi2_i8) == -1) {
fprintf(stderr, "Failed to register add2\n");
exit(EXIT_FAILURE);
}
rpc_serve_all(state);
return 0;
}
/* Adds 2 signed 8 bit numbers */
/* Uses data1 for left operand, data2 for right operand */
rpc_data *add2_i8(rpc_data *in) {
/* Check data2 */
if (in->data2 == NULL || in->data2_len != 1) {
return NULL;
}
/* Parse request */
char n1 = in->data1;
char n2 = ((char *)in->data2)[0];
/* Perform calculation */
printf("add2: arguments %d and %d\n", n1, n2);
int res = n1 + n2;
/* Prepare response */
rpc_data *out = malloc(sizeof(rpc_data));
assert(out != NULL);
out->data1 = res;
out->data2_len = 0;
out->data2 = NULL;
return out;
}
rpc_data *multi2_i8(rpc_data *in) {
/* Check data2 */
if (in->data2 == NULL || in->data2_len != 1) {
return NULL;
}
/* Parse request */
char n1 = in->data1;
char n2 = ((char *)in->data2)[0];
/* Perform calculation */
printf("multi2: arguments %d and %d\n", n1, n2);
int res = n1 * n2;
/* Prepare response */
rpc_data *out = malloc(sizeof(rpc_data));
assert(out != NULL);
out->data1 = res;
out->data2_len = 0;
out->data2 = NULL;
return out;
}