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; }
wslay_event_context * wslay_event_context_new ( void * ctx, const struct wslay_event_callbacks * callbacks, void * user_data ) { wslay_event_context * context = talloc_zero ( ctx, sizeof ( wslay_event_context ) ); if ( context == NULL ) { return NULL; } struct wslay_frame_callbacks frame_callbacks = { wslay_event_frame_send_callback, wslay_event_frame_recv_callback, wslay_event_frame_genmask_callback }; context->callbacks = * callbacks; context->user_data = user_data; context->frame_user_data.ctx = context; context->frame_user_data.user_data = user_data; wslay_frame_context * frame_ctx = wslay_frame_context_new ( context, &frame_callbacks, &context->frame_user_data ); if ( frame_ctx == NULL ) { talloc_free ( context ); return NULL; } context->frame_ctx = frame_ctx; context->read_enabled = context->write_enabled = 1; context->send_queue = wslay_queue_new ( context ); if ( context->send_queue == NULL ) { talloc_free ( context ); return NULL; } context->send_ctrl_queue = wslay_queue_new ( context ); if ( context->send_ctrl_queue == NULL ) { talloc_free ( context ); return NULL; } context->queued_msg_count = 0; context->queued_msg_length = 0; uint8_t i; for ( i = 0; i < 2; ++i ) { wslay_event_imsg_reset ( & context->imsgs[i] ); context->imsgs[i].chunks = wslay_queue_new ( context ); if ( context->imsgs[i].chunks == NULL ) { talloc_free ( context ); return NULL; } } context->imsg = & context->imsgs[0]; context->obufmark = context->obuflimit = context->obuf; context->status_code_sent = WSLAY_CODE_ABNORMAL_CLOSURE; context->status_code_recv = WSLAY_CODE_ABNORMAL_CLOSURE; context->max_recv_msg_length = UINT64_MAX; return context; }