mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-03 13:28:44 +00:00
Expose rpc guts
This commit is contained in:
1 parent
f02f88f336
commit
581a0698af
6 files changed
+53
-51
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user