diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-14 16:21:21 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-14 16:21:21 +0200 |
commit | f58ed35f0b73e933b6dd1f98472941c1d5d7ce9c (patch) | |
tree | c547ca4338fe04f4117b390eec6c855c9c3abef6 /shared/protocol.c | |
parent | 137f62eb368101dd0371f0c54e1a4a2a7be48890 (diff) |
request handler function executed
Diffstat (limited to 'shared/protocol.c')
-rw-r--r-- | shared/protocol.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/shared/protocol.c b/shared/protocol.c index 49d1795..6e3bc73 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -46,16 +46,32 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) { return; } +#define WS_CMD_MAP(parsed_cmd, name, code) \ + if (strlen(parsed_cmd->argv[0]) == strlen(name) && strncmp(parsed_cmd->argv[0], name, strlen(name)) == 0) return code; + +static ws_e_protocol_cmd ws_protocol_get_cmd_code(ws_s_protocol_parsed_cmd* parsed_cmd) { + WS_CMD_MAP(parsed_cmd, "last-records", WS_PROTOCOL_CMD_LAST_RECORDS); + + return WS_PROTOCOL_CMD_UNKNOWN; +} + ws_s_protocol_response* ws_protocol_parse_finished(ws_s_protocol_parsed_cmd* parsed_cmd) { ws_s_protocol_response* response = malloc(sizeof(ws_s_protocol_response)); + response->success = WS_PROTOCOL_CMD_RETURN_ERROR; + response->msg = NULL; - if (strncmp("last-records", parsed_cmd->argv[0], 12) == 0) { - printf("last-records found!\n"); - } + ws_e_protocol_cmd cmd_code = ws_protocol_get_cmd_code(parsed_cmd); + if (cmd_code == WS_PROTOCOL_CMD_UNKNOWN) goto ws_protocol_parse_exit; + if (cmd_code >= WS_PROTOCOL_CMD_AMOUNT) goto ws_protocol_parse_exit; + + void (*ws_protocol_res_handler)(ws_s_protocol_parsed_cmd*, ws_s_protocol_response*) = + g_ws_protocol_res_handlers[cmd_code]; + if (ws_protocol_res_handler == NULL) goto ws_protocol_parse_exit; + (*ws_protocol_res_handler)(parsed_cmd, response); - response->msg = ws_bin_s_alloc(50); - strncpy((char*) response->msg->data, "hello world!\n\0", 14); +ws_protocol_parse_exit: + if (response->msg == NULL) response->msg = ws_bin_s_alloc(0); return response; } |