Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
[ws] Add emit_error function for variadic formatted messages
Browse files Browse the repository at this point in the history
Also, use this for direct emit instead of deferring errors where it
was no longer necessary.

Signed-off-by: Geoff Gustafson <[email protected]>
  • Loading branch information
grgustaf committed Sep 5, 2017
1 parent cf5951e commit 83ec422
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions src/zjs_web_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,10 @@ static inline ws_connection_t *find_connection(server_handle_t *server_h,
}

enum {
ERROR_OOM,
ERROR_PAYLOAD,
ERROR_PACKET_DECODE,
ERROR_CONN_FAIL
};

static const char *error_messages[] = {
"out of memory",
"payload too large",
"error decoding packet",
"connection failed",
};

Expand All @@ -140,6 +134,9 @@ typedef struct error_desc {
jerry_value_t function_obj;
} error_desc_t;

// FIXME: create_error_desc is only used once now so all this could probably be
// removed

static error_desc_t create_error_desc(u32_t error_id, jerry_value_t this,
jerry_value_t function_obj)
{
Expand Down Expand Up @@ -173,6 +170,21 @@ static void handle_error_arg(void *unused, jerry_value_t argv[], u32_t *argc,
*argc = 1;
}

static void emit_error(jerry_value_t obj, const char *format, ...)
{
// effects: emits an error event on obj w/ formatted string message
const int LEN = 128;
char buf[LEN];
va_list args;
va_start(args, format);
vsnprintf(buf, LEN, format, args);
va_end(args);

jerry_value_t error = zjs_error_context(buf, 0, 0);
jerry_value_clear_error_flag(&error);
zjs_emit_event(obj, "error", &error, 1);
}

// a zjs_event_free callback
static void free_server(void *native)
{
Expand Down Expand Up @@ -490,22 +502,19 @@ static void close_connection(void *h, jerry_value_t argv[], u32_t argc)
// process a TCP WS packet
static void process_packet(ws_connection_t *con, u8_t *data, u32_t len)
{
// requires: expects to be called from main thread; emits events directly
ws_packet_t *packet = zjs_malloc(sizeof(ws_packet_t));
if (!packet) {
ERR_PRINT("allocation failed\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(con->conn, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(con->conn, "out of memory");
return;
}
memset(packet, 0, sizeof(ws_packet_t));
if (decode_packet(packet, data, len) < 0) {
ERR_PRINT("error decoding packet\n");
zjs_free(packet->payload);
zjs_free(packet);
error_desc_t desc = create_error_desc(ERROR_PACKET_DECODE, 0, 0);
zjs_defer_emit_event(con->conn, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(con->conn, "decoding packet");
return;
}

Expand All @@ -514,6 +523,8 @@ static void process_packet(ws_connection_t *con, u8_t *data, u32_t len)

u16_t plen = packet->payload_len;

// FIXME: deferred emit calls below can be changed to direct

switch (packet->opcode) {
case WS_PACKET_CONTINUATION:
// continuation frame
Expand Down Expand Up @@ -562,9 +573,10 @@ static ZJS_DECL_FUNC_ARGS(ws_send_data, ws_packet_type type)
}
if (con->server_h->max_payload &&
(buf->bufsize > con->server_h->max_payload)) {
DBG_PRINT("payload too large: %d > %d\n", buf->bufsize,
con->server_h->max_payload);
return zjs_error("payload too large");
char msg[60];
sprintf(msg, "payload too large: %d > %d", buf->bufsize,
con->server_h->max_payload);
return zjs_error(msg);
}
u8_t out[buf->bufsize + 10];
u32_t out_len = encode_packet(type, mask, buf->buffer, buf->bufsize, out);
Expand Down Expand Up @@ -661,9 +673,7 @@ static void receive_packet(const void *buffer, u32_t length)
u8_t *data = zjs_malloc(len);
if (!data) {
DBG_PRINT("not enough memory to allocate data\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(con->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(con->server, "out of memory");
net_pkt_unref(pkt);
return;
}
Expand All @@ -679,11 +689,8 @@ static void receive_packet(const void *buffer, u32_t length)
#endif
if (con->server_h->max_payload &&
(len > con->server_h->max_payload)) {
DBG_PRINT("payload too large: %d > %d\n", len,
emit_error(con->server, "payload too large: %d > %d", len,
con->server_h->max_payload);
error_desc_t desc = create_error_desc(ERROR_PAYLOAD, 0, 0);
zjs_defer_emit_event(con->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
net_pkt_unref(pkt);
zjs_free(data);
return;
Expand All @@ -693,9 +700,7 @@ static void receive_packet(const void *buffer, u32_t length)
con->accept_key = zjs_malloc(64);
if (!con->accept_key) {
DBG_PRINT("could not allocate accept key\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(con->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(con->server, "out of memory");
net_pkt_unref(pkt);
zjs_free(data);
return;
Expand Down Expand Up @@ -759,10 +764,7 @@ static void receive_packet(const void *buffer, u32_t length)
strlen(con->accept_key) + 6);
if (!send_data) {
DBG_PRINT("could not allocate accept message\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(con->server, "error", &desc,
sizeof(desc), handle_error_arg,
zjs_release_args);
emit_error(con->server, "out of memory");
net_pkt_unref(pkt);
zjs_free(data);
return;
Expand Down Expand Up @@ -832,6 +834,7 @@ static void tcp_received(struct net_context *context,

static void post_accept_handler(void *handle, jerry_value_t ret_val)
{
// requires: expects to be called from main thread; emits events directly
ws_connection_t *con = (ws_connection_t *)handle;
if (!jerry_value_is_string(ret_val)) {
DBG_PRINT("no protocol returned\n");
Expand All @@ -854,9 +857,7 @@ static void post_accept_handler(void *handle, jerry_value_t ret_val)
char *send_data = zjs_malloc(sdata_size);
if (!send_data) {
ERR_PRINT("could not allocate accept message\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(con->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(con->server, "out of memory");
return;
}
memset(send_data, 0, sdata_size);
Expand Down Expand Up @@ -899,19 +900,15 @@ static void accept_connection(const void *buffer, u32_t length)
ws_connection_t *con = zjs_malloc(sizeof(ws_connection_t));
if (!con) {
ERR_PRINT("could not allocate connection handle\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(server_h->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(server_h->server, "out of memory");
return;
}
memset(con, 0, sizeof(ws_connection_t));
con->tcp_sock = accept->context;
con->rbuf = zjs_malloc(DEFAULT_WS_BUFFER_SIZE);
if (!con->rbuf) {
ERR_PRINT("could not allocate read buffer\n");
error_desc_t desc = create_error_desc(ERROR_OOM, 0, 0);
zjs_defer_emit_event(server_h->server, "error", &desc, sizeof(desc),
handle_error_arg, zjs_release_args);
emit_error(server_h->server, "out of memory");
zjs_free(con);
return;
}
Expand Down

0 comments on commit 83ec422

Please sign in to comment.