websocket_return_t websocket_register_cb(websocket_t *websocket, websocket_cb_t *cb) { if (websocket == NULL) { WEBSOCKET_DEBUG("NULL parameter\n"); return WEBSOCKET_ALLOCATION_ERROR; } wslay_event_config_set_callbacks(websocket->ctx, cb); return WEBSOCKET_SUCCESS; }
static int wslay_event_context_init (wslay_event_context_ptr *ctx, const struct wslay_event_callbacks *callbacks, void *user_data) { int i, r; struct wslay_frame_callbacks frame_callbacks = { wslay_event_frame_send_callback, wslay_event_frame_recv_callback, wslay_event_frame_genmask_callback }; *ctx = (wslay_event_context_ptr)malloc(sizeof(struct wslay_event_context)); if(!*ctx) { return WSLAY_ERR_NOMEM; } memset(*ctx, 0, sizeof(struct wslay_event_context)); wslay_event_config_set_callbacks(*ctx, callbacks); (*ctx)->user_data = user_data; (*ctx)->frame_user_data.ctx = *ctx; (*ctx)->frame_user_data.user_data = user_data; if((r = wslay_frame_context_init(&(*ctx)->frame_ctx, &frame_callbacks, &(*ctx)->frame_user_data)) != 0) { wslay_event_context_free(*ctx); return r; } (*ctx)->read_enabled = (*ctx)->write_enabled = 1; (*ctx)->send_queue = wslay_queue_new(); if(!(*ctx)->send_queue) { wslay_event_context_free(*ctx); return WSLAY_ERR_NOMEM; } (*ctx)->send_ctrl_queue = wslay_queue_new(); if(!(*ctx)->send_ctrl_queue) { wslay_event_context_free(*ctx); return WSLAY_ERR_NOMEM; } (*ctx)->queued_msg_count = 0; (*ctx)->queued_msg_length = 0; for(i = 0; i < 2; ++i) { wslay_event_imsg_reset(&(*ctx)->imsgs[i]); (*ctx)->imsgs[i].chunks = wslay_queue_new(); if(!(*ctx)->imsgs[i].chunks) { wslay_event_context_free(*ctx); return WSLAY_ERR_NOMEM; } } (*ctx)->imsg = &(*ctx)->imsgs[0]; (*ctx)->obufmark = (*ctx)->obuflimit = (*ctx)->obuf; (*ctx)->status_code_sent = WSLAY_CODE_ABNORMAL_CLOSURE; (*ctx)->status_code_recv = WSLAY_CODE_ABNORMAL_CLOSURE; (*ctx)->max_recv_msg_length = (1u << 31)-1; return 0; }
void set_callbacks(const struct wslay_event_callbacks *callbacks) { wslay_event_config_set_callbacks(ctx_, callbacks); }