/** * Process some data just received on the socket. */ enum ws_read_action ws_add_data(struct http_client *c) { const char *frame_start, *frame_end; char *tmp; while(1) { /* look for frame start */ if(!c->sz || c->buffer[0] != '\x00') { /* can't find frame start */ return WS_READ_FAIL; } /* look for frame end */ int ret; size_t frame_len; frame_start = c->buffer; frame_end = memchr(frame_start, '\xff', c->sz); if(frame_end == NULL) { /* continue reading */ return WS_READ_MORE; } /* parse and execute frame. */ frame_len = frame_end - frame_start - 1; ret = ws_execute(c, frame_start + 1, frame_len); if(ret != 0) { /* can't process frame. */ return WS_READ_FAIL; } /* remove frame from buffer */ c->sz -= (2 + frame_len); tmp = malloc(c->sz); memcpy(tmp, c->buffer + 2 + frame_len, c->sz); free(c->buffer); c->buffer = tmp; } }
/** * Process some data just received on the socket. */ enum ws_state ws_add_data(struct http_client *c) { enum ws_state state; state = ws_parse_data(c->buffer, c->sz, &c->frame); if(state == WS_MSG_COMPLETE) { int ret = ws_execute(c, c->frame->payload, c->frame->payload_sz); /* remove frame from client buffer */ http_client_remove_data(c, c->frame->total_sz); /* free frame and set back to NULL */ ws_msg_free(&c->frame); if(ret != 0) { /* can't process frame. */ return WS_ERROR; } } return state; }