bool msgpack_unpack_next(msgpack_unpacked* result, const char* data, size_t len, size_t* off) { msgpack_unpacked_destroy(result); size_t noff = 0; if(off != NULL) { noff = *off; } if(len <= noff) { return false; } msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); template_context ctx; template_init(&ctx); ctx.user.z = z; ctx.user.referenced = false; int e = template_execute(&ctx, data, len, &noff); if(e <= 0) { msgpack_zone_free(z); return false; } if(off != NULL) { *off = noff; } result->zone = z; result->data = template_data(&ctx); return true; }
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) { if(!msgpack_unpacker_flush_zone(mpac)) { return NULL; } msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); if(r == NULL) { return NULL; } msgpack_zone* old = mpac->z; mpac->z = r; return old; }
msgpack_unpack_return msgpack_unpack_next(msgpack_unpacked* result, const char* data, size_t len, size_t* off) { size_t noff = 0; msgpack_unpacked_destroy(result); if(off != NULL) { noff = *off; } if(len <= noff) { return MSGPACK_UNPACK_CONTINUE; } if (!result->zone) { result->zone = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); } if (!result->zone) { return MSGPACK_UNPACK_NOMEM_ERROR; } else { int e; template_context ctx; template_init(&ctx); ctx.user.z = result->zone; ctx.user.referenced = false; e = template_execute(&ctx, data, len, &noff); if(e < 0) { msgpack_zone_free(result->zone); result->zone = NULL; return MSGPACK_UNPACK_PARSE_ERROR; } if(e == 0) { return MSGPACK_UNPACK_CONTINUE; } if(off != NULL) { *off = noff; } result->data = template_data(&ctx); return MSGPACK_UNPACK_SUCCESS; } }
static void * recv_from_learner(void *arg) { void *zmq_recv_sock; recv_thd_data *thd = arg; if ((zmq_recv_sock = zmq_socket(thd->zmq_ctx, ZMQ_SUB))) { if (!zmq_connect(zmq_recv_sock, thd->recv_endpoint)) { grn_ctx ctx; if (!grn_ctx_init(&ctx, 0)) { if ((!grn_ctx_use(&ctx, db))) { msgpack_zone *mempool; if ((mempool = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE))) { grn_obj cmd_buf; zmq_pollitem_t items[] = { { zmq_recv_sock, 0, ZMQ_POLLIN, 0} }; GRN_TEXT_INIT(&cmd_buf, 0); zmq_setsockopt(zmq_recv_sock, ZMQ_SUBSCRIBE, "", 0); while (loop) { zmq_poll(items, 1, 10000); if (items[0].revents & ZMQ_POLLIN) { recv_handler(&ctx, zmq_recv_sock, mempool, &cmd_buf); } } grn_obj_unlink(&ctx, &cmd_buf); msgpack_zone_free(mempool); } else { print_error("cannot create msgpack zone."); } /* db_close */ } else { print_error("error in grn_db_open() on recv thread."); } grn_ctx_fin(&ctx); } else { print_error("error in grn_ctx_init() on recv thread."); } } else { print_error("cannot create recv zmq_socket."); } } else { print_error("cannot connect zmq_socket."); } return NULL; }
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size) { char* buffer; void* ctx; msgpack_zone* z; if(initial_buffer_size < COUNTER_SIZE) { initial_buffer_size = COUNTER_SIZE; } buffer = (char*)malloc(initial_buffer_size); if(buffer == NULL) { return false; } ctx = malloc(sizeof(template_context)); if(ctx == NULL) { free(buffer); return false; } z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); if(z == NULL) { free(ctx); free(buffer); return false; } mpac->buffer = buffer; mpac->used = COUNTER_SIZE; mpac->free = initial_buffer_size - mpac->used; mpac->off = COUNTER_SIZE; mpac->parsed = 0; mpac->last_parsed = 0; mpac->initial_buffer_size = initial_buffer_size; mpac->z = z; mpac->ctx = ctx; init_count(mpac->buffer); template_init(CTX_CAST(mpac->ctx)); CTX_CAST(mpac->ctx)->user.z = mpac->z; CTX_CAST(mpac->ctx)->user.referenced = false; return true; }
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) { msgpack_zone* r; msgpack_zone* old; if(!msgpack_unpacker_flush_zone(mpac)) { return NULL; } r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); if(r == NULL) { return NULL; } old = mpac->z; mpac->z = r; CTX_CAST(mpac->ctx)->user.z = mpac->z; return old; }