mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 01:38:01 +08:00
Expose rpc guts
This commit is contained in:
parent
f02f88f336
commit
581a0698af
@ -7,7 +7,7 @@
|
||||
|
||||
static const char *s_listen_on = "ws://localhost:8000";
|
||||
static const char *s_web_root = "web_root";
|
||||
static void *s_rpc_head = NULL;
|
||||
static struct mg_rpc *s_rpc_head = NULL;
|
||||
|
||||
static void rpc_sum(struct mg_rpc_req *r) {
|
||||
double a = 0.0, b = 0.0;
|
||||
@ -46,7 +46,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
// Got websocket frame. Received data is wm->data
|
||||
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
||||
char *resp = NULL;
|
||||
struct mg_rpc_req r = {&s_rpc_head, mg_pfn_realloc, &resp, 0, 0, wm->data};
|
||||
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_realloc, &resp, 0, wm->data};
|
||||
mg_rpc_process(&r);
|
||||
if (resp) mg_ws_send(c, resp, strlen(resp), WEBSOCKET_OP_TEXT);
|
||||
free(resp);
|
||||
@ -81,6 +81,6 @@ int main(void) {
|
||||
mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
mg_mgr_free(&mgr); // Deallocate event manager
|
||||
mg_rpc_free(&s_rpc_head); // Deallocate RPC handlers
|
||||
mg_rpc_del(&s_rpc_head, NULL); // Deallocate RPC handlers
|
||||
return 0;
|
||||
}
|
||||
|
23
mongoose.c
23
mongoose.c
@ -3559,29 +3559,22 @@ void mg_mgr_init(struct mg_mgr *mgr) {
|
||||
#endif
|
||||
|
||||
|
||||
struct mg_rpc {
|
||||
struct mg_rpc *next;
|
||||
struct mg_str method;
|
||||
void (*fn)(struct mg_rpc_req *);
|
||||
void *fn_data;
|
||||
};
|
||||
|
||||
void mg_rpc_add(void **head, struct mg_str method,
|
||||
void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
|
||||
void (*fn)(struct mg_rpc_req *), void *fn_data) {
|
||||
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
|
||||
rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data;
|
||||
rpc->next = (struct mg_rpc *) *head, *head = rpc;
|
||||
rpc->next = *head, *head = rpc;
|
||||
}
|
||||
|
||||
void mg_rpc_del(void **head, void (*fn)(struct mg_rpc_req *)) {
|
||||
struct mg_rpc *r, **h = (struct mg_rpc **) head;
|
||||
while ((r = *h) != NULL) {
|
||||
void mg_rpc_del(struct mg_rpc **head, void (*fn)(struct mg_rpc_req *)) {
|
||||
struct mg_rpc *r;
|
||||
while ((r = *head) != NULL) {
|
||||
if (r->fn == fn || fn == NULL) {
|
||||
*h = r->next;
|
||||
*head = r->next;
|
||||
free((void *) r->method.ptr);
|
||||
free(r);
|
||||
} else {
|
||||
h = &(*h)->next;
|
||||
head = &(*head)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3593,7 +3586,7 @@ void mg_rpc_process(struct mg_rpc_req *r) {
|
||||
struct mg_rpc *h = *(struct mg_rpc **) r->head;
|
||||
while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next;
|
||||
if (h != NULL) {
|
||||
r->handler_data = h->fn_data;
|
||||
r->rpc = h;
|
||||
h->fn(r);
|
||||
} else {
|
||||
mg_rpc_err(r, -32601, "\"%.*s not found\"", (int) m.len, m.ptr);
|
||||
|
24
mongoose.h
24
mongoose.h
@ -1364,17 +1364,25 @@ char *mg_json_get_b64(struct mg_str json, const char *path, int *len);
|
||||
|
||||
// JSON-RPC request descriptor
|
||||
struct mg_rpc_req {
|
||||
void **head; // List of all RPC handlers
|
||||
mg_pfn_t pfn; // Response printing function
|
||||
void *pfn_data; // Response printing function data
|
||||
void *handler_data; // Endpoint handler data
|
||||
void *process_data; // Arbitrary user data
|
||||
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
|
||||
struct mg_rpc **head; // RPC handlers list head
|
||||
struct mg_rpc *rpc; // RPC handler being called
|
||||
mg_pfn_t pfn; // Response printing function
|
||||
void *pfn_data; // Response printing function data
|
||||
void *req_data; // Arbitrary request data
|
||||
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
|
||||
};
|
||||
|
||||
void mg_rpc_add(void **head, struct mg_str method_pattern,
|
||||
// JSON-RPC method handler
|
||||
struct mg_rpc {
|
||||
struct mg_rpc *next; // Next in list
|
||||
struct mg_str method; // Method pattern
|
||||
void (*fn)(struct mg_rpc_req *); // Handler function
|
||||
void *fn_data; // Handler function argument
|
||||
};
|
||||
|
||||
void mg_rpc_add(struct mg_rpc **head, struct mg_str method_pattern,
|
||||
void (*handler)(struct mg_rpc_req *), void *handler_data);
|
||||
void mg_rpc_del(void **head, void (*handler)(struct mg_rpc_req *));
|
||||
void mg_rpc_del(struct mg_rpc **head, void (*handler)(struct mg_rpc_req *));
|
||||
void mg_rpc_process(struct mg_rpc_req *);
|
||||
|
||||
// Helper functions to print result or error frame
|
||||
|
23
src/rpc.c
23
src/rpc.c
@ -1,28 +1,21 @@
|
||||
#include "rpc.h"
|
||||
|
||||
struct mg_rpc {
|
||||
struct mg_rpc *next;
|
||||
struct mg_str method;
|
||||
void (*fn)(struct mg_rpc_req *);
|
||||
void *fn_data;
|
||||
};
|
||||
|
||||
void mg_rpc_add(void **head, struct mg_str method,
|
||||
void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
|
||||
void (*fn)(struct mg_rpc_req *), void *fn_data) {
|
||||
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
|
||||
rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data;
|
||||
rpc->next = (struct mg_rpc *) *head, *head = rpc;
|
||||
rpc->next = *head, *head = rpc;
|
||||
}
|
||||
|
||||
void mg_rpc_del(void **head, void (*fn)(struct mg_rpc_req *)) {
|
||||
struct mg_rpc *r, **h = (struct mg_rpc **) head;
|
||||
while ((r = *h) != NULL) {
|
||||
void mg_rpc_del(struct mg_rpc **head, void (*fn)(struct mg_rpc_req *)) {
|
||||
struct mg_rpc *r;
|
||||
while ((r = *head) != NULL) {
|
||||
if (r->fn == fn || fn == NULL) {
|
||||
*h = r->next;
|
||||
*head = r->next;
|
||||
free((void *) r->method.ptr);
|
||||
free(r);
|
||||
} else {
|
||||
h = &(*h)->next;
|
||||
head = &(*head)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -34,7 +27,7 @@ void mg_rpc_process(struct mg_rpc_req *r) {
|
||||
struct mg_rpc *h = *(struct mg_rpc **) r->head;
|
||||
while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next;
|
||||
if (h != NULL) {
|
||||
r->handler_data = h->fn_data;
|
||||
r->rpc = h;
|
||||
h->fn(r);
|
||||
} else {
|
||||
mg_rpc_err(r, -32601, "\"%.*s not found\"", (int) m.len, m.ptr);
|
||||
|
24
src/rpc.h
24
src/rpc.h
@ -4,17 +4,25 @@
|
||||
|
||||
// JSON-RPC request descriptor
|
||||
struct mg_rpc_req {
|
||||
void **head; // List of all RPC handlers
|
||||
mg_pfn_t pfn; // Response printing function
|
||||
void *pfn_data; // Response printing function data
|
||||
void *handler_data; // Endpoint handler data
|
||||
void *process_data; // Arbitrary user data
|
||||
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
|
||||
struct mg_rpc **head; // RPC handlers list head
|
||||
struct mg_rpc *rpc; // RPC handler being called
|
||||
mg_pfn_t pfn; // Response printing function
|
||||
void *pfn_data; // Response printing function data
|
||||
void *req_data; // Arbitrary request data
|
||||
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
|
||||
};
|
||||
|
||||
void mg_rpc_add(void **head, struct mg_str method_pattern,
|
||||
// JSON-RPC method handler
|
||||
struct mg_rpc {
|
||||
struct mg_rpc *next; // Next in list
|
||||
struct mg_str method; // Method pattern
|
||||
void (*fn)(struct mg_rpc_req *); // Handler function
|
||||
void *fn_data; // Handler function argument
|
||||
};
|
||||
|
||||
void mg_rpc_add(struct mg_rpc **head, struct mg_str method_pattern,
|
||||
void (*handler)(struct mg_rpc_req *), void *handler_data);
|
||||
void mg_rpc_del(void **head, void (*handler)(struct mg_rpc_req *));
|
||||
void mg_rpc_del(struct mg_rpc **head, void (*handler)(struct mg_rpc_req *));
|
||||
void mg_rpc_process(struct mg_rpc_req *);
|
||||
|
||||
// Helper functions to print result or error frame
|
||||
|
@ -2436,9 +2436,9 @@ static void test_json(void) {
|
||||
}
|
||||
|
||||
static void test_rpc(void) {
|
||||
void *head = NULL;
|
||||
struct mg_rpc *head = NULL;
|
||||
char *s = NULL;
|
||||
struct mg_rpc_req req = {&head, mg_pfn_realloc, &s, 0, 0, {0, 0}};
|
||||
struct mg_rpc_req req = {&head, 0, mg_pfn_realloc, &s, 0, {0, 0}};
|
||||
mg_rpc_add(&head, mg_str("rpc.list"), mg_rpc_list, NULL);
|
||||
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user