static void msg_hgetall_reply(msgpack_packer* pk, const redisReply *r) { /* zip keys and values together in a msgpack object */ unsigned int i; if(r->elements % 2 != 0) { return; } msgpack_pack_map(pk, r->elements / 2); for(i = 0; i < r->elements; i += 2) { redisReply *k = r->element[i], *v = r->element[i+1]; /* keys and values need to be strings */ if(k->type != REDIS_REPLY_STRING || v->type != REDIS_REPLY_STRING) { return; } /* key */ msgpack_pack_raw(pk, k->len); msgpack_pack_raw_body(pk, k->str, k->len); /* value */ msgpack_pack_raw(pk, v->len); msgpack_pack_raw_body(pk, v->str, v->len); } }
int main(void) { /* creates buffer and serializer instance. */ msgpack_sbuffer* buffer = msgpack_sbuffer_new(); msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); /* serializes ["Hello", "MessagePack"]. */ msgpack_pack_array(pk, 2); msgpack_pack_raw(pk, 5); msgpack_pack_raw_body(pk, "Hello", 5); msgpack_pack_raw(pk, 11); msgpack_pack_raw_body(pk, "MessagePack", 11); /* deserializes it. */ msgpack_unpacked msg; msgpack_unpacked_init(&msg); bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL); /* prints the deserialized object. */ msgpack_object obj = msg.data; msgpack_object_print(stdout, obj); /*=> ["Hello", "MessagePack"] */ puts(""); // msgpack_pack_object/pk /* cleaning */ msgpack_sbuffer_free(buffer); msgpack_packer_free(pk); }
static apr_status_t log_fluentd_writer(request_rec *r, void *handle, const char **strs, int *strl, int nelts, apr_size_t len) { fluentd_log *log = (fluentd_log *)handle; apr_status_t result; if (log->write_local == 1) { if (log->normal_handle) { result = normal_log_writer(r, log->normal_handle, strs, strl, nelts, len); } } else { int i =0; char *str; msgpack_sbuffer sbuf; msgpack_packer pk; msgpack_sbuffer_init(&sbuf); msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 3); msgpack_pack_raw(&pk,log->tag_len); msgpack_pack_raw_body(&pk, log->tag, log->tag_len); msgpack_pack_int(&pk, 1329275765); msgpack_pack_array(&pk,nelts); for (i = 0; i < nelts; i++) { msgpack_pack_raw(&pk,strl[i]); msgpack_pack_raw_body(&pk, strs[i], strl[i]); } log_fluentd_post(log,&sbuf); msgpack_sbuffer_destroy(&sbuf); } return OK; }
int in_mem_collect(struct flb_config *config, void *in_context) { struct sysinfo info; (void) config; struct flb_in_mem_config *ctx = in_context; uint32_t totalram, freeram; sysinfo(&info); totalram = info.totalram / 1024; freeram = info.freeram / 1024; msgpack_pack_map(&ctx->pckr, 3); msgpack_pack_raw(&ctx->pckr, 4); msgpack_pack_raw_body(&ctx->pckr, "time", 4); msgpack_pack_uint64(&ctx->pckr, time(NULL)); msgpack_pack_raw(&ctx->pckr, 5); msgpack_pack_raw_body(&ctx->pckr, "total", 5); msgpack_pack_uint32(&ctx->pckr, totalram); msgpack_pack_raw(&ctx->pckr, 4); msgpack_pack_raw_body(&ctx->pckr, "free", 4); msgpack_pack_uint32(&ctx->pckr, freeram); flb_debug("[in_mem] memory total %d kb, free %d kb (buffer=%i)", info.totalram, info.freeram, ctx->idx); ++ctx->idx; return 0; }
void msgpack_pack_key_value(msgpack_packer *packer, char *key, int key_len, char *value, int value_len) { msgpack_pack_raw(packer, key_len); msgpack_pack_raw_body(packer, key, key_len); msgpack_pack_raw(packer, value_len); msgpack_pack_raw_body(packer, value, value_len); }
/** * Parse info message and return object. */ void msg_info_reply(msgpack_packer* pk, const char *s, size_t sz) { const char *p = s; unsigned int count = 0; /* TODO: handle new format */ /* count number of lines */ while(p < s + sz) { p = strchr(p, '\r'); if(!p) break; p++; count++; } /* create msgpack object */ msgpack_pack_map(pk, count); p = s; while(p < s + sz) { char *key, *val, *nl, *colon; size_t key_sz, val_sz; /* find key */ colon = strchr(p, ':'); if(!colon) { break; } key_sz = colon - p; key = calloc(key_sz + 1, 1); memcpy(key, p, key_sz); p = colon + 1; /* find value */ nl = strchr(p, '\r'); if(!nl) { free(key); break; } val_sz = nl - p; val = calloc(val_sz + 1, 1); memcpy(val, p, val_sz); p = nl + 1; if(*p == '\n') p++; /* add to object */ msgpack_pack_raw(pk, key_sz); msgpack_pack_raw_body(pk, key, key_sz); msgpack_pack_raw(pk, val_sz); msgpack_pack_raw_body(pk, val, val_sz); free(key); free(val); } }
std::string encodeRequest(const Request &r) { std::shared_ptr<msgpack_sbuffer> buffer{msgpack_sbuffer_new(), &msgpack_sbuffer_free}; std::shared_ptr<msgpack_packer> pk{msgpack_packer_new(buffer.get(), msgpack_sbuffer_write), &msgpack_packer_free}; msgpack_pack_map(pk.get(), 5); msgpack_pack_raw(pk.get(), 4); msgpack_pack_raw_body(pk.get(), "func", 4); msgpack_pack_raw(pk.get(), r.func.size()); msgpack_pack_raw_body(pk.get(), r.func.data(), r.func.size()); msgpack_pack_raw(pk.get(), 6); msgpack_pack_raw_body(pk.get(), "metric", 6); msgpack_pack_raw(pk.get(), r.metric.size()); msgpack_pack_raw_body(pk.get(), r.metric.data(), r.metric.size()); msgpack_pack_raw(pk.get(), 9); msgpack_pack_raw_body(pk.get(), "timestamp", 9); msgpack_pack_uint64(pk.get(), r.timestamp); msgpack_pack_raw(pk.get(), 5); msgpack_pack_raw_body(pk.get(), "value", 5); switch(r.value.type()) { case Value::Type::Nil: msgpack_pack_nil(pk.get()); break; case Value::Type::Boolean: if(r.value.as<bool>()) msgpack_pack_true(pk.get()); else msgpack_pack_false(pk.get()); break; case Value::Type::Numeric: msgpack_pack_double(pk.get(), r.value.as<double>()); break; case Value::Type::String: { std::string b = r.value.as<std::string>(); msgpack_pack_raw(pk.get(), b.size()); msgpack_pack_raw_body(pk.get(), b.data(), b.size()); } break; case Value::Type::Table: { // Hack std::string b = r.value.as<std::string>(); if(b.empty()) msgpack_pack_nil(pk.get()); else msgpack_sbuffer_write(reinterpret_cast<void *>(buffer.get()), b.data(), b.size()); } break; } msgpack_pack_raw(pk.get(), 5); msgpack_pack_raw_body(pk.get(), "state", 5); if(r.state.empty()) msgpack_pack_nil(pk.get()); else msgpack_sbuffer_write(reinterpret_cast<void *>(buffer.get()), r.state.data(), r.state.size()); return std::string(buffer->data, buffer->size); }
virtual void encode() { mImgs = boost::shared_ptr<boost::circular_buffer<CameraImage> >(new boost::circular_buffer<CameraImage>(60)); while(1) { if(mImgs->size() == 0) { usleep(3000); continue; } printf("encoding!\n"); CameraImage i = mImgs->back(); mImgs->pop_back(); encodeImage(i); msgpack_sbuffer buffer; msgpack_packer pk; msgpack_sbuffer_init(&buffer); msgpack_packer_init(&pk, &buffer, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_int(&pk, 1); msgpack_pack_int(&pk, i.camera_timestamp); msgpack_pack_raw(&pk, i.encoded.size); msgpack_pack_raw_body(&pk, (void*)i.encoded.buffer.get(), i.encoded.size); write(mFile, buffer.data, buffer.size); msgpack_sbuffer_destroy(&buffer); } }
/* * Document-method: String#to_msgpack * * call-seq: * string.to_msgpack(out = '') -> String * * Serializes the String into raw bytes. */ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_raw(out, RSTRING_LEN(self)); msgpack_pack_raw_body(out, RSTRING_PTR(self), RSTRING_LEN(self)); return out; }
void grn_output_geo_point(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, grn_geo_point *value) { put_delimiter(ctx, outbuf, output_type); switch (output_type) { case GRN_CONTENT_JSON: if (value) { GRN_TEXT_PUTC(ctx, outbuf, '"'); grn_text_itoa(ctx, outbuf, value->latitude); GRN_TEXT_PUTC(ctx, outbuf, 'x'); grn_text_itoa(ctx, outbuf, value->longitude); GRN_TEXT_PUTC(ctx, outbuf, '"'); } else { GRN_TEXT_PUTS(ctx, outbuf, "null"); } break; case GRN_CONTENT_TSV: if (value) { GRN_TEXT_PUTC(ctx, outbuf, '"'); grn_text_itoa(ctx, outbuf, value->latitude); GRN_TEXT_PUTC(ctx, outbuf, 'x'); grn_text_itoa(ctx, outbuf, value->longitude); GRN_TEXT_PUTC(ctx, outbuf, '"'); } else { GRN_TEXT_PUTS(ctx, outbuf, "\"\""); } break; case GRN_CONTENT_XML: GRN_TEXT_PUTS(ctx, outbuf, "<GEO_POINT>"); if (value) { grn_text_itoa(ctx, outbuf, value->latitude); GRN_TEXT_PUTC(ctx, outbuf, 'x'); grn_text_itoa(ctx, outbuf, value->longitude); } GRN_TEXT_PUTS(ctx, outbuf, "</GEO_POINT>"); break; case GRN_CONTENT_MSGPACK : #ifdef HAVE_MESSAGE_PACK if (value) { grn_obj buf; GRN_TEXT_INIT(&buf, 0); grn_text_itoa(ctx, &buf, value->latitude); GRN_TEXT_PUTC(ctx, &buf, 'x'); grn_text_itoa(ctx, &buf, value->longitude); msgpack_pack_raw(&ctx->impl->msgpacker, GRN_TEXT_LEN(&buf)); msgpack_pack_raw_body(&ctx->impl->msgpacker, GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf)); grn_obj_close(ctx, &buf); } else { msgpack_pack_nil(&ctx->impl->msgpacker); } #endif break; case GRN_CONTENT_NONE: break; } INCR_LENGTH; }
static FilterReplyResult filter_apply(FilterReplyResultBase * const rb, const struct sockaddr_storage * const sa_local, const socklen_t sa_local_len, const struct sockaddr_storage * const sa_remote, const socklen_t sa_remote_len, void *buf, size_t * const nbyte) { msgpack_packer * const msgpack_packer = rb->filter->msgpack_packer; filter_before_apply(rb, 1U, "read", sa_local, sa_local_len, sa_remote, sa_remote_len); if (rb->pre != false) { msgpack_pack_mstring(msgpack_packer, "nbyte"); msgpack_pack_unsigned_long(msgpack_packer, *nbyte); } else if (*rb->ret <= 0) { msgpack_pack_mstring(msgpack_packer, "data"); msgpack_pack_nil(msgpack_packer); } else { assert((size_t) *rb->ret <= *nbyte); msgpack_pack_mstring(msgpack_packer, "data"); msgpack_pack_raw(msgpack_packer, *rb->ret); msgpack_pack_raw_body(msgpack_packer, buf, *rb->ret); } if (filter_send_message(rb->filter) != 0) { return FILTER_REPLY_RESULT_ERROR; } return filter_parse_reply(rb, buf, nbyte); }
static inline void pack_string(msgpack_packer *pk, const char *str) { size_t len = strlen(str); msgpack_pack_raw(pk, len); msgpack_pack_raw_body(pk, str, len); }
/* Given a series of tasks, serialize into a series of array objects * using messagepack and return pointer to the buffer containing the * serialized data */ msgpack_sbuffer * oolong_serialize(msgpack_sbuffer *sbuf, oolong_task **tasks, int size) { int i; oolong_task *task; msgpack_packer *pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); for(i = 0; i < size; i++) { task = tasks[i]; /* Begin array packing */ msgpack_pack_array(pk, 3); /* Pack description */ msgpack_pack_raw(pk, sizeof(task->description) + 1); msgpack_pack_raw_body(pk, task->description, sizeof(task->description) + 1); /* Pack completed */ msgpack_pack_int(pk, task->completed); /* Pack due */ msgpack_pack_int(pk, task->due); } msgpack_packer_free(pk); return sbuf; }
void grn_output_str(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, const char *value, size_t value_len) { put_delimiter(ctx, outbuf, output_type); switch (output_type) { case GRN_CONTENT_JSON: grn_text_esc(ctx, outbuf, value, value_len); break; case GRN_CONTENT_TSV: grn_text_esc(ctx, outbuf, value, value_len); break; case GRN_CONTENT_XML: GRN_TEXT_PUTS(ctx, outbuf, "<TEXT>"); grn_text_escape_xml(ctx, outbuf, value, value_len); GRN_TEXT_PUTS(ctx, outbuf, "</TEXT>"); break; case GRN_CONTENT_MSGPACK : #ifdef HAVE_MESSAGE_PACK msgpack_pack_raw(&ctx->impl->msgpacker, value_len); msgpack_pack_raw_body(&ctx->impl->msgpacker, value, value_len); #endif break; case GRN_CONTENT_NONE: break; } INCR_LENGTH; }
void msgpack_pack_string(msgpack_packer *pk, char *s) { size_t len = strlen(s); msgpack_pack_raw(pk, len); msgpack_pack_raw_body(pk, s, len); }
int msgpack_pack_lstring(msgpack_packer * const msgpack_packer, const char * const str, const size_t str_len) { if (msgpack_pack_raw(msgpack_packer, str_len) != 0) { return -1; } return msgpack_pack_raw_body(msgpack_packer, str, str_len); }
void paxos_request_pack(struct yakyak *yy, struct paxos_request *req) { msgpack_pack_array(yy->pk, 2); paxos_value_pack(yy, &req->pr_val); msgpack_pack_raw(yy->pk, req->pr_size); msgpack_pack_raw_body(yy->pk, req->pr_data, req->pr_size); }
void mlockfile_pack(msgpack_packer * pk, struct mlockfile *lf) { size_t path_length = strlen(lf->path); msgpack_pack_array(pk, 2); msgpack_pack_raw(pk, path_length); msgpack_pack_raw_body(pk, lf->path, path_length); msgpack_pack_uint64(pk, lf->mmappedsize); }
void paxos_acceptor_pack(struct yakyak *yy, struct paxos_acceptor *acc) { msgpack_pack_array(yy->pk, 2); msgpack_pack_paxid(yy->pk, acc->pa_paxid); msgpack_pack_raw(yy->pk, acc->pa_size); msgpack_pack_raw_body(yy->pk, acc->pa_desc, acc->pa_size); }
int LogInLog_to_msgpack(msgpack_packer *pk, LogInLog *arg) { msgpack_pack_array(pk, 13); UserInfo_to_msgpack(pk, &arg->user); msgpack_pack_int(pk, arg->site); msgpack_pack_int(pk, arg->num); msgpack_pack_array(pk, arg->sites_size); for (int _i = 0; _i < arg->sites_size; ++_i) { msgpack_pack_int(pk, arg->sites[_i]); } msgpack_pack_map(pk, arg->sites2_size); for (int _i = 0; _i < arg->sites2_size; ++_i) { if (arg->sites2_keys[_i]) { size_t _l = strlen(arg->sites2_keys[_i]); msgpack_pack_raw(pk, _l); msgpack_pack_raw_body(pk, arg->sites2_keys[_i], _l); } else { msgpack_pack_nil(pk); } msgpack_pack_int(pk, arg->sites2_vals[_i]); } if (arg->s1) { size_t _l = strlen(arg->s1); msgpack_pack_raw(pk, _l); msgpack_pack_raw_body(pk, arg->s1, _l); } else { msgpack_pack_nil(pk); } arg->b1 ? msgpack_pack_true(pk) : msgpack_pack_false(pk); msgpack_pack_raw(pk, arg->r1_size); msgpack_pack_raw_body(pk, arg->r1, arg->r1_size); msgpack_pack_double(pk, arg->d1); msgpack_pack_int(pk, arg->i2); my_type_x_to_msgpack(pk, &arg->my1); msgpack_pack_array(pk, arg->user_list_size); for (int _i = 0; _i < arg->user_list_size; ++_i) { UserInfo_to_msgpack(pk, &arg->user_list[_i]); } msgpack_pack_map(pk, arg->user_map_size); for (int _i = 0; _i < arg->user_map_size; ++_i) { UserInfo_to_msgpack(pk, &arg->user_map_keys[_i]); UserInfo_to_msgpack(pk, &arg->user_map_vals[_i]); } return 0; }
/* * Document-method: Symbol#to_msgpack * * call-seq: * symbol.to_msgpack(out = '') -> String * * Serializes the Symbol into raw bytes. */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; }
int msgpack_pack_cstr(msgpack_packer* pk, const char* cstr) { size_t length; if (cstr) { length = strlen(cstr); msgpack_pack_raw(pk, length); msgpack_pack_raw_body(pk, cstr, length); } else { msgpack_pack_raw(pk, 0); msgpack_pack_raw_body(pk, NULL, 0); } return 0; }
void msgpack_rpc_error(char *msg, msgpack_packer *res) { size_t len = strlen(msg); // error message msgpack_pack_raw(res, len); msgpack_pack_raw_body(res, msg, len); // Nil result msgpack_pack_nil(res); }
void send_trigger(mdcli_t * client, char * target_worker, char * rule_id, int ival, char * user_id) { zclock_log("activating trigger\ntarget=%s\nvalue=%d\nuser=%s", target_worker, ival, user_id); struct timeval tval; gettimeofday(&tval, NULL); // make a messagepack hash msgpack_sbuffer * buffer = msgpack_sbuffer_new(); msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); // value chunk msgpack_pack_map(pk, 3); // key msgpack_pack_raw(pk, 5); msgpack_pack_raw_body(pk, "value", 5); // value msgpack_pack_int(pk, ival); //time chunk // key msgpack_pack_raw(pk, 5); msgpack_pack_raw_body(pk, "epoch", 5); // time msgpack_pack_int(pk, tval.tv_sec); msgpack_pack_raw(pk, 6); msgpack_pack_raw_body(pk, "micros", 6); // time msgpack_pack_int(pk, tval.tv_usec); zmsg_t * msg = zmsg_new(); // really, the user_id should be being added by a // gatekeeper, not the block itself, or it's a security // hole. will do for now FIX zmsg_pushstr(msg, user_id); // zmsg_pushmem(msg, &trigger.line_id, sizeof(int)); zmsg_pushmem(msg, buffer->data, buffer->size); zmsg_pushstr(msg, rule_id); zmsg_pushstr(msg, "DoAction"); mdcli_send(client, target_worker, &msg); }
/** * plume_req_init - Initialize a yakyak, then pack an opcode and our cert. */ void plume_req_init(struct yakyak *yy, struct plume_client *client, char *op, unsigned n) { yakyak_init(yy, 2); msgpack_pack_string(yy->pk, op); msgpack_pack_array(yy->pk, n + 1); msgpack_pack_raw(yy->pk, client->pc_cert_size); msgpack_pack_raw_body(yy->pk, client->pc_cert, client->pc_cert_size); }
int failed_packfn(msgpack_packer * pk, void *msg) { size_t msg_length = strlen((char *) msg); msgpack_pack_array(pk, 2); msgpack_pack_false(pk); msgpack_pack_raw(pk, msg_length); msgpack_pack_raw_body(pk, msg, msg_length); return (0); }
void report_add_pair(struct msg_report* rp, char * key, char * value){ if(!key) return; report_add_key(rp, key); if(value){ int len = strlen(value); msgpack_pack_raw(rp->pk, len); msgpack_pack_raw_body(rp->pk, value, len); }else{ msgpack_pack_nil(rp->pk); } }
static FilterReplyResult filter_apply(FilterReplyResultBase * const rb, const struct sockaddr_storage * const sa_local, const socklen_t sa_local_len, struct msghdr *msg, size_t * const nbyte, int * const flags) { msgpack_packer * const msgpack_packer = rb->filter->msgpack_packer; if (rb->pre != false) { filter_before_apply(rb, 2U, "recvmsg", sa_local, sa_local_len, NULL, (socklen_t) 0U); } else { filter_before_apply(rb, 2U, "recvmsg", sa_local, sa_local_len, msg->msg_name, msg->msg_namelen); } msgpack_pack_mstring(msgpack_packer, "flags"); msgpack_pack_int(msgpack_packer, *flags); if (rb->pre != false) { msgpack_pack_mstring(msgpack_packer, "nbyte"); msgpack_pack_unsigned_long(msgpack_packer, *nbyte); } else if (*rb->ret <= 0) { msgpack_pack_mstring(msgpack_packer, "data"); msgpack_pack_nil(msgpack_packer); } else { assert((size_t) *rb->ret <= *nbyte); msgpack_pack_mstring(msgpack_packer, "data"); msgpack_pack_raw(msgpack_packer, *nbyte); size_t data_remaining = *nbyte; size_t read_from_vec; struct iovec * const vecs = msg->msg_iov; size_t i_vecs = 0U; while (i_vecs < (size_t) msg->msg_iovlen && data_remaining > (size_t) 0U) { if (data_remaining < vecs[i_vecs].iov_len) { read_from_vec = data_remaining; } else { read_from_vec = vecs[i_vecs].iov_len; } assert(data_remaining >= read_from_vec); assert(vecs[i_vecs].iov_len >= read_from_vec); msgpack_pack_raw_body(msgpack_packer, vecs[i_vecs].iov_base, read_from_vec); data_remaining -= read_from_vec; i_vecs++; } } if (filter_send_message(rb->filter) != 0) { return FILTER_REPLY_RESULT_ERROR; } return filter_parse_reply(rb, msg, flags); }
/* * Document-method: Symbol#to_msgpack * * call-seq: * symbol.to_msgpack(out = '') -> String * * Serializes the Symbol into raw bytes. */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { #ifdef COMPAT_HAVE_ENCODING return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); #else ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; #endif }
static void pack_string(msgpack_packer *packer, const char *string) { if(string == NULL) { msgpack_pack_nil(packer); } else { int length = (int)strlen(string); #if defined(MSGPACK_VERSION_MAJOR) && MSGPACK_VERSION_MAJOR < 1 msgpack_pack_raw(packer, length); msgpack_pack_raw_body(packer, string, length); #else msgpack_pack_str(packer, length); msgpack_pack_str_body(packer, string, length); #endif } }