void grn_output_time(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, int64_t value) { double dv = value; dv /= 1000000.0; put_delimiter(ctx, outbuf, output_type); switch (output_type) { case GRN_CONTENT_JSON: grn_text_ftoa(ctx, outbuf, dv); break; case GRN_CONTENT_TSV: grn_text_ftoa(ctx, outbuf, dv); break; case GRN_CONTENT_XML: GRN_TEXT_PUTS(ctx, outbuf, "<DATE>"); grn_text_ftoa(ctx, outbuf, dv); GRN_TEXT_PUTS(ctx, outbuf, "</DATE>"); break; case GRN_CONTENT_MSGPACK : #ifdef HAVE_MESSAGE_PACK msgpack_pack_double(&ctx->impl->msgpacker, dv); #endif break; case GRN_CONTENT_NONE: break; } INCR_LENGTH; }
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); }
void grn_output_float(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, double value) { put_delimiter(ctx, outbuf, output_type); switch (output_type) { case GRN_CONTENT_JSON: grn_text_ftoa(ctx, outbuf, value); break; case GRN_CONTENT_TSV: grn_text_ftoa(ctx, outbuf, value); break; case GRN_CONTENT_XML: GRN_TEXT_PUTS(ctx, outbuf, "<FLOAT>"); grn_text_ftoa(ctx, outbuf, value); GRN_TEXT_PUTS(ctx, outbuf, "</FLOAT>"); break; case GRN_CONTENT_MSGPACK : #ifdef HAVE_MESSAGE_PACK msgpack_pack_double(&ctx->impl->msgpacker, value); #endif break; case GRN_CONTENT_NONE: break; } INCR_LENGTH; }
static inline void pack_real(msgpack_packer *pk, const char *real_token) { char *end; double value; errno = 0; value = strtod(real_token, &end); /* TODO: error handling */ /* TODO */ msgpack_pack_double(pk, value); }
/* Callback to gather CPU usage between now and previous snapshot */ int in_cpu_collect(struct flb_config *config, void *in_context) { int i; int maps; int len; double usage; double total; (void) config; struct flb_in_cpu_config *ctx = in_context; struct cpu_stats *cstats = &ctx->cstats; struct cpu_snapshot *s; /* Get the current CPU usage */ total = proc_cpu_load(ctx->n_processors, cstats); cstats->load_now = total; /* Calculate the difference between the two samples */ usage = fabs(cstats->load_now - cstats->load_pre) / ctx->cpu_ticks; total = (usage * 100) / ctx->n_processors; /* Put current load back */ cstats->load_pre = cstats->load_now; /* * Store the new data into the MessagePack buffer, */ msgpack_pack_array(&ctx->mp_pck, 2); msgpack_pack_uint64(&ctx->mp_pck, time(NULL)); msgpack_pack_map(&ctx->mp_pck, (ctx->n_processors * 7 ) + 1); msgpack_pack_bin(&ctx->mp_pck, 3); msgpack_pack_bin_body(&ctx->mp_pck, "cpu", 3); msgpack_pack_double(&ctx->mp_pck, total); for (i = 1; i < ctx->n_processors + 1; i++) { s = &cstats->info[i]; CPU_PACK_SNAP(s, user); CPU_PACK_SNAP(s, nice); CPU_PACK_SNAP(s, system); CPU_PACK_SNAP(s, idle); CPU_PACK_SNAP(s, iowait); CPU_PACK_SNAP(s, irq); CPU_PACK_SNAP(s, softirq); } flb_debug("[in_cpu] CPU %0.2f%%", total); flb_stats_update(in_cpu_plugin.stats_fd, 0, 1); return 0; }
static void pack_value_object(msgpack_packer *packer, VALUE value) { VALUE rubyString; char *keyString; switch (TYPE(value)) { case T_FIXNUM: msgpack_pack_long(packer, FIX2LONG(value)); break; case T_FLOAT: msgpack_pack_double(packer, rb_num2dbl(value)); break; default: rubyString = rb_funcall(value, rb_intern("to_s"), 0, 0); keyString = StringValueCStr(rubyString); pack_string(packer, keyString); break; } }
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; }
void init_unpack_array(msgpack_object_type type) { size_t off = 0; msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); msgpack_packer pk; msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 3); if (type == MSGPACK_OBJECT_NIL) { msgpack_pack_nil(&pk); } else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) { msgpack_pack_int(&pk, 1); } else if (type == MSGPACK_OBJECT_BOOLEAN) { msgpack_pack_true(&pk); } else if (type == MSGPACK_OBJECT_MAP) { msgpack_pack_map(&pk, 1); } else if (type == MSGPACK_OBJECT_ARRAY) { msgpack_pack_array(&pk, 3); msgpack_pack_int(&pk, 1); msgpack_pack_int(&pk, 1); msgpack_pack_int(&pk, 1); } else if (type == MSGPACK_OBJECT_FLOAT) { msgpack_pack_double(&pk, 1.2); } msgpack_pack_true(&pk); msgpack_pack_str(&pk, 7); msgpack_pack_str_body(&pk, "example", 7); msgpack_unpacked result; msgpack_unpacked_init(&result); msgpack_unpack_next(&result, sbuf.data, sbuf.size, &off); msgpack_sbuffer_destroy(&sbuf); deserialized = result.data; }
void rpc_service_handle(rpc_service_t *service, zmq_msg_t *request) { /* Parse the msgpack */ zmq_msg_t response; int rc; msgpack_unpacked request_msg; msgpack_unpacked_init(&request_msg); rc = msgpack_unpack_next(&request_msg, zmq_msg_data(request), zmq_msg_size(request), NULL); insist_return(rc, (void)(0), "Failed to unpack message '%.*s'", (int)zmq_msg_size(request), (char *)zmq_msg_data(request)); msgpack_object request_obj = request_msg.data; /* Find the method name */ char *method = NULL; size_t method_len = -1; rc = obj_get(&request_obj, "method", MSGPACK_OBJECT_RAW, &method, &method_len); msgpack_sbuffer *response_buffer = msgpack_sbuffer_new(); msgpack_sbuffer *result_buffer = msgpack_sbuffer_new(); msgpack_sbuffer *error_buffer = msgpack_sbuffer_new(); msgpack_packer *response_msg = msgpack_packer_new(response_buffer, msgpack_sbuffer_write); msgpack_packer *result = msgpack_packer_new(result_buffer, msgpack_sbuffer_write); msgpack_packer *error = msgpack_packer_new(error_buffer, msgpack_sbuffer_write); //printf("Method: %.*s\n", method_len, method); void *clock = zmq_stopwatch_start(); double duration; if (rc != 0) { /* method not found */ msgpack_pack_nil(result); /* result is nil on error */ msgpack_pack_map(error, 2); msgpack_pack_string(error, "error", -1); msgpack_pack_string(error, "Message had no 'method' field", -1); msgpack_pack_string(error, "request", -1); msgpack_pack_object(error, request_obj); } else { /* valid method, keep going */ //printf("The method is: '%.*s'\n", (int)method_len, method); rpc_name name; name.name = method; name.len = method_len; rpc_method *rpcmethod = g_tree_lookup(service->methods, &name); /* if we found a valid rpc method and the args check passed ... */ if (rpcmethod != NULL) { /* the callback is responsible for filling in the 'result' and 'error' * objects. */ rpcmethod->callback(NULL, &request_obj, result, error, rpcmethod->data); } else { msgpack_pack_nil(result); /* result is nil on error */ /* TODO(sissel): allow methods to register themselves */ //fprintf(stderr, "Invalid request '%.*s' (unknown method): ", //method_len, method); //msgpack_object_print(stderr, request_obj); //fprintf(stderr, "\n"); msgpack_pack_map(error, 2); msgpack_pack_string(error, "error", -1); msgpack_pack_string(error, "No such method requested", -1); msgpack_pack_string(error, "request", -1); msgpack_pack_object(error, request_obj); } } /* valid/invalid method handling */ duration = zmq_stopwatch_stop(clock) / 1000000.; //printf("method '%.*s' took %lf seconds\n", (int)method_len, method); msgpack_unpacked result_unpacked; msgpack_unpacked error_unpacked; msgpack_unpacked response_unpacked; msgpack_unpacked_init(&result_unpacked); msgpack_unpacked_init(&error_unpacked); msgpack_unpacked_init(&response_unpacked); /* TODO(sissel): If this unpack test fails, we should return an error to the calling * client indicating that some internal error has occurred */ //fprintf(stderr, "Result payload: '%.*s'\n", result_buffer->size, //result_buffer->data); rc = msgpack_unpack_next(&result_unpacked, result_buffer->data, result_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on 'result' buffer" " of request '%.*s'", (int)method_len, method); rc = msgpack_unpack_next(&error_unpacked, error_buffer->data, error_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on 'error' buffer" " of request '%.*s'", (int)method_len, method); msgpack_pack_map(response_msg, 3); /* result, error, duration */ msgpack_pack_string(response_msg, "result", 6); msgpack_pack_object(response_msg, result_unpacked.data); msgpack_pack_string(response_msg, "error", 5); msgpack_pack_object(response_msg, error_unpacked.data); msgpack_pack_string(response_msg, "duration", 8); msgpack_pack_double(response_msg, duration); rc = msgpack_unpack_next(&response_unpacked, response_buffer->data, response_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on full response buffer" " of request '%.*s'", (int)method_len, method); //printf("request: "); //msgpack_object_print(stdout, request_obj); //printf("\n"); //printf("response: "); //msgpack_object_print(stdout, response_unpacked.data); //printf("\n"); zmq_msg_init_data(&response, response_buffer->data, response_buffer->size, free_msgpack_buffer, response_buffer); zmq_send(service->socket, &response, 0); zmq_msg_close(&response); msgpack_packer_free(error); msgpack_packer_free(result); msgpack_sbuffer_free(error_buffer); msgpack_sbuffer_free(result_buffer); msgpack_packer_free(response_msg); msgpack_unpacked_destroy(&request_msg); } /* rpc_service_handle */
static void pack_timestamp(msgpack_packer *packer) { msgpack_pack_double(packer, get_wall_time_in_msec()); }
void msgpack_rpc_from_double(double result, msgpack_packer *res) { msgpack_pack_double(res, result); }
static void msgpack_float(CTX ctx, void *pkp, kfloat_t f) { kpackAPI_t *pk = (kpackAPI_t *)pkp; msgpack_pack_double(pk->pk, f); }
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d) { switch(d.type) { case MSGPACK_OBJECT_NIL: return msgpack_pack_nil(pk); case MSGPACK_OBJECT_BOOLEAN: if(d.via.boolean) { return msgpack_pack_true(pk); } else { return msgpack_pack_false(pk); } case MSGPACK_OBJECT_POSITIVE_INTEGER: return msgpack_pack_uint64(pk, d.via.u64); case MSGPACK_OBJECT_NEGATIVE_INTEGER: return msgpack_pack_int64(pk, d.via.i64); case MSGPACK_OBJECT_DOUBLE: return msgpack_pack_double(pk, d.via.dec); case MSGPACK_OBJECT_RAW: { int ret = msgpack_pack_raw(pk, d.via.raw.size); if(ret < 0) { return ret; } return msgpack_pack_raw_body(pk, d.via.raw.ptr, d.via.raw.size); } case MSGPACK_OBJECT_ARRAY: { int ret = msgpack_pack_array(pk, d.via.array.size); if(ret < 0) { return ret; } msgpack_object* o = d.via.array.ptr; msgpack_object* const oend = d.via.array.ptr + d.via.array.size; for(; o != oend; ++o) { ret = msgpack_pack_object(pk, *o); if(ret < 0) { return ret; } } return 0; } case MSGPACK_OBJECT_MAP: { int ret = msgpack_pack_map(pk, d.via.map.size); if(ret < 0) { return ret; } msgpack_object_kv* kv = d.via.map.ptr; msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size; for(; kv != kvend; ++kv) { ret = msgpack_pack_object(pk, kv->key); if(ret < 0) { return ret; } ret = msgpack_pack_object(pk, kv->val); if(ret < 0) { return ret; } } return 0; } default: return -1; } }
void write_raw_msgpack(FILE *out, conjugrad_float_t *x, int ncol, void *meta) { int nsingle = ncol * (N_ALPHA - 1); int nsingle_padded = nsingle + N_ALPHA_PAD - (nsingle % N_ALPHA_PAD); conjugrad_float_t *x1 = x; conjugrad_float_t *x2 = &x[nsingle_padded]; (void)x2; msgpack_sbuffer* buffer = msgpack_sbuffer_new(); msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); #ifdef JANSSON if(meta != NULL) { msgpack_pack_map(pk, 5); meta_write_msgpack(pk, (json_t *)meta); } else { msgpack_pack_map(pk, 4); } #else msgpack_pack_map(pk, 4); #endif msgpack_pack_str(pk, 6); msgpack_pack_str_body(pk, "format", 6); msgpack_pack_str(pk, 5); msgpack_pack_str_body(pk, "ccm-1", 5); msgpack_pack_str(pk, 4); msgpack_pack_str_body(pk, "ncol", 4); msgpack_pack_int32(pk, ncol); msgpack_pack_str(pk, 8); msgpack_pack_str_body(pk, "x_single", 8); msgpack_pack_array(pk, ncol * (N_ALPHA - 1)); for(int i = 0; i < ncol; i++) { for(int a = 0; a < N_ALPHA - 1; a++) { #if CONJUGRAD_FLOAT == 32 msgpack_pack_float(pk, V(i, a)); #elif CONJUGRAD_FLOAT == 64 msgpack_pack_double(pk, V(i, a)); #endif } } msgpack_pack_str(pk, 6); msgpack_pack_str_body(pk, "x_pair", 6); int nedge = ncol * (ncol - 1) / 2; msgpack_pack_map(pk, nedge); char sbuf[8192]; for(int i = 0; i < ncol; i++) { for(int j = i + 1; j < ncol; j++) { int nchar = snprintf(sbuf, 8192, "%d/%d", i, j); msgpack_pack_str(pk, nchar); msgpack_pack_str_body(pk, sbuf, nchar); msgpack_pack_map(pk, 3); msgpack_pack_str(pk, 1); msgpack_pack_str_body(pk, "i", 1); msgpack_pack_int32(pk, i); msgpack_pack_str(pk, 1); msgpack_pack_str_body(pk, "j", 1); msgpack_pack_int32(pk, j); msgpack_pack_str(pk, 1); msgpack_pack_str_body(pk, "x", 1); msgpack_pack_array(pk, N_ALPHA * N_ALPHA); for(int a = 0; a < N_ALPHA; a++) { for(int b = 0; b < N_ALPHA; b++) { #if CONJUGRAD_FLOAT == 32 msgpack_pack_float(pk, W(b, j, a, i)); #elif CONJUGRAD_FLOAT == 64 msgpack_pack_double(pk, W(b, j, a, i)); #endif } } } } fwrite(buffer->data, buffer->size, 1, out); msgpack_sbuffer_free(buffer); msgpack_packer_free(pk); }
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d) { switch(d.type) { case MSGPACK_OBJECT_NIL: return msgpack_pack_nil(pk); case MSGPACK_OBJECT_BOOLEAN: if(d.via.boolean) { return msgpack_pack_true(pk); } else { return msgpack_pack_false(pk); } case MSGPACK_OBJECT_POSITIVE_INTEGER: return msgpack_pack_uint64(pk, d.via.u64); case MSGPACK_OBJECT_NEGATIVE_INTEGER: return msgpack_pack_int64(pk, d.via.i64); case MSGPACK_OBJECT_FLOAT32: return msgpack_pack_float(pk, (float)d.via.f64); case MSGPACK_OBJECT_FLOAT64: return msgpack_pack_double(pk, d.via.f64); case MSGPACK_OBJECT_STR: { int ret = msgpack_pack_str(pk, d.via.str.size); if(ret < 0) { return ret; } return msgpack_pack_str_body(pk, d.via.str.ptr, d.via.str.size); } case MSGPACK_OBJECT_BIN: { int ret = msgpack_pack_bin(pk, d.via.bin.size); if(ret < 0) { return ret; } return msgpack_pack_bin_body(pk, d.via.bin.ptr, d.via.bin.size); } case MSGPACK_OBJECT_EXT: { int ret = msgpack_pack_ext(pk, d.via.ext.size, d.via.ext.type); if(ret < 0) { return ret; } return msgpack_pack_ext_body(pk, d.via.ext.ptr, d.via.ext.size); } case MSGPACK_OBJECT_ARRAY: { int ret = msgpack_pack_array(pk, d.via.array.size); if(ret < 0) { return ret; } else { msgpack_object* o = d.via.array.ptr; msgpack_object* const oend = d.via.array.ptr + d.via.array.size; for(; o != oend; ++o) { ret = msgpack_pack_object(pk, *o); if(ret < 0) { return ret; } } return 0; } } case MSGPACK_OBJECT_MAP: { int ret = msgpack_pack_map(pk, d.via.map.size); if(ret < 0) { return ret; } else { msgpack_object_kv* kv = d.via.map.ptr; msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size; for(; kv != kvend; ++kv) { ret = msgpack_pack_object(pk, kv->key); if(ret < 0) { return ret; } ret = msgpack_pack_object(pk, kv->val); if(ret < 0) { return ret; } } return 0; } } default: return -1; } }
int msgpack_pack_double_wrap(msgpack_packer* pk, double d) { return msgpack_pack_double(pk, d); }
TEST(streaming, basic) { msgpack_sbuffer* buffer = msgpack_sbuffer_new(); msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); // 1, 2, 3, "str", ["str_data"], "bin", ["bin_data"], {0.3: 0.4} EXPECT_EQ(0, msgpack_pack_int(pk, 1)); EXPECT_EQ(0, msgpack_pack_int(pk, 2)); EXPECT_EQ(0, msgpack_pack_int(pk, 3)); EXPECT_EQ(0, msgpack_pack_str(pk, 3)); EXPECT_EQ(0, msgpack_pack_str_body(pk, "str", 3)); EXPECT_EQ(0, msgpack_pack_array(pk, 1)); EXPECT_EQ(0, msgpack_pack_str(pk, 8)); EXPECT_EQ(0, msgpack_pack_str_body(pk, "str_data", 8)); EXPECT_EQ(0, msgpack_pack_bin(pk, 3)); EXPECT_EQ(0, msgpack_pack_bin_body(pk, "bin", 3)); EXPECT_EQ(0, msgpack_pack_array(pk, 1)); EXPECT_EQ(0, msgpack_pack_bin(pk, 8)); EXPECT_EQ(0, msgpack_pack_bin_body(pk, "bin_data", 8)); EXPECT_EQ(0, msgpack_pack_map(pk, 1)); EXPECT_EQ(0, msgpack_pack_float(pk, 0.4f)); EXPECT_EQ(0, msgpack_pack_double(pk, 0.8)); int max_count = 6; msgpack_packer_free(pk); const char* input = buffer->data; const char* const eof = input + buffer->size; msgpack_unpacker pac; msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE); msgpack_unpacked result; msgpack_unpacked_init(&result); int count = 0; while(count < max_count) { bool unpacked = false; msgpack_unpacker_reserve_buffer(&pac, 32*1024); while(!unpacked) { /* read buffer into msgpack_unapcker_buffer(&pac) upto * msgpack_unpacker_buffer_capacity(&pac) bytes. */ memcpy(msgpack_unpacker_buffer(&pac), input, 1); input += 1; EXPECT_TRUE(input <= eof); msgpack_unpacker_buffer_consumed(&pac, 1); while(msgpack_unpacker_next(&pac, &result) == MSGPACK_UNPACK_SUCCESS) { unpacked = 1; msgpack_object obj = result.data; msgpack_object e; switch(count++) { case 0: EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); EXPECT_EQ(1, obj.via.u64); break; case 1: EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); EXPECT_EQ(2, obj.via.u64); break; case 2: EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); EXPECT_EQ(3, obj.via.u64); break; case 3: EXPECT_EQ(MSGPACK_OBJECT_STR, obj.type); EXPECT_EQ(std::string("str",3), std::string(obj.via.str.ptr, obj.via.str.size)); break; case 4: EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type); EXPECT_EQ(1, obj.via.array.size); e = obj.via.array.ptr[0]; EXPECT_EQ(MSGPACK_OBJECT_STR, e.type); EXPECT_EQ(std::string("str_data",8), std::string(e.via.str.ptr, e.via.str.size)); break; case 5: EXPECT_EQ(MSGPACK_OBJECT_BIN, obj.type); EXPECT_EQ(std::string("bin",3), std::string(obj.via.bin.ptr, obj.via.bin.size)); break; case 6: EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type); EXPECT_EQ(1, obj.via.array.size); e = obj.via.array.ptr[0]; EXPECT_EQ(MSGPACK_OBJECT_BIN, e.type); EXPECT_EQ(std::string("bin_data",8), std::string(e.via.bin.ptr, e.via.bin.size)); break; case 7: EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type); EXPECT_EQ(1, obj.via.map.size); e = obj.via.map.ptr[0].key; EXPECT_EQ(MSGPACK_OBJECT_FLOAT, e.type); ASSERT_FLOAT_EQ(0.4f, static_cast<float>(e.via.f64)); #if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT) EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type); ASSERT_FLOAT_EQ(0.4f, static_cast<float>(e.via.dec)); #endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT e = obj.via.map.ptr[0].val; EXPECT_EQ(MSGPACK_OBJECT_FLOAT, e.type); ASSERT_DOUBLE_EQ(0.8, e.via.f64); #if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT) EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type); ASSERT_DOUBLE_EQ(0.8, e.via.dec); #endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT break; } } } } msgpack_unpacker_destroy(&pac); msgpack_unpacked_destroy(&result); msgpack_sbuffer_free(buffer); }
/* * Document-method: Float#to_msgpack * * call-seq: * float.to_msgpack(out = '') -> String * * Serializes the Float into raw bytes. */ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_double(out, rb_num2dbl(self)); return out; }
void ANetworkController::ReceivedGetBehaviors() { msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); /* serialize values into the buffer using msgpack_sbuffer_write callback function. */ msgpack_packer pk; msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 2); msgpack_pack_uint16(&pk, EPacketType::PT_GetBehaviors); TArray<ABehaviours*> BehaviorsList; for (TActorIterator<ABehaviours> ObjIt(GetWorld()); ObjIt; ++ObjIt) { ABehaviours* Behaviors = *ObjIt; BehaviorsList.Add(Behaviors); } int32 BehaviorsListNum = BehaviorsList.Num(); msgpack_pack_array(&pk, BehaviorsListNum); for (int i = 0; i < BehaviorsListNum; i++) { ABehaviours* Behaviors = BehaviorsList[i]; msgpack_pack_map(&pk, 4); msgpack_pack_str(&pk, 13); msgpack_pack_str_body(&pk, "FlockingState", 13); msgpack_pack_uint8(&pk, (uint8)Behaviors->FlockingState); msgpack_pack_str(&pk, 13); msgpack_pack_str_body(&pk, "ThrottleGains", 13); Pack(&pk, Behaviors->ThrottleGains); msgpack_pack_str(&pk, 13); msgpack_pack_str_body(&pk, "SteeringGains", 13); Pack(&pk, Behaviors->SteeringGains); int32 BehaviorsNum = Behaviors->Behaviours.Num(); msgpack_pack_str(&pk, 9); msgpack_pack_str_body(&pk, "Behaviors", 9); msgpack_pack_array(&pk, BehaviorsNum); for (int j = 0; j < BehaviorsNum; j++) { UBehaviour* Behavior = Behaviors->Behaviours[j]; TArray<UProperty*> Properties; for (TFieldIterator<UProperty> PropIt(Behavior->GetClass()); PropIt; ++PropIt) { UProperty* Property = *PropIt; if (Cast<UNumericProperty>(Property) || Cast<UBoolProperty>(Property)) { Properties.Add(Property); } } msgpack_pack_map(&pk, Properties.Num() + 1); msgpack_pack_str(&pk, 4); msgpack_pack_str_body(&pk, "Name", 4); FString Name = Behavior->GetClass()->GetName(); msgpack_pack_str(&pk, Name.Len()); msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len()); for (auto PropIt(Properties.CreateIterator()); PropIt; ++PropIt) { UProperty* Property = *PropIt; const void* Value = Property->ContainerPtrToValuePtr<uint8>(Behavior); FString Name = Property->GetName(); msgpack_pack_str(&pk, Name.Len()); msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len()); if (UNumericProperty *NumericProperty = Cast<UNumericProperty>(Property)) { if (NumericProperty->IsFloatingPoint()) { msgpack_pack_double(&pk, NumericProperty->GetFloatingPointPropertyValue(Value)); } else if (NumericProperty->IsInteger()) { msgpack_pack_int(&pk, NumericProperty->GetSignedIntPropertyValue(Value)); } } else if (UBoolProperty *BoolProperty = Cast<UBoolProperty>(Property)) { if (BoolProperty->GetPropertyValue(Value)) { msgpack_pack_true(&pk); } else { msgpack_pack_false(&pk); } } } } } int32 BytesSent; TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent); msgpack_sbuffer_destroy(&sbuf); }
int yar_pack_push_double(yar_packager *packager, double num) /* {{{ */ { msgpack_packer *pk = packager->pk; return msgpack_pack_double(pk, num) < 0? 0 : 1; }
void msgpack_rpc_from_float(Float result, msgpack_packer *res) { msgpack_pack_double(res, result); }
/* Receive a tokenized JSON message and convert it to MsgPack */ static char *tokens_to_msgpack(char *js, jsmntok_t *tokens, int arr_size, int *out_size) { int i; int flen; char *p; char *buf; jsmntok_t *t; msgpack_packer pck; msgpack_sbuffer sbuf; /* initialize buffers */ msgpack_sbuffer_init(&sbuf); msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write); for (i = 0; i < arr_size ; i++) { t = &tokens[i]; if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)) { break; } flen = (t->end - t->start); switch (t->type) { case JSMN_OBJECT: msgpack_pack_map(&pck, t->size); break; case JSMN_ARRAY: msgpack_pack_array(&pck, t->size); break; case JSMN_STRING: msgpack_pack_bin(&pck, flen); msgpack_pack_bin_body(&pck, js + t->start, flen); break; case JSMN_PRIMITIVE: p = js + t->start; if (*p == 'f') { msgpack_pack_false(&pck); } else if (*p == 't') { msgpack_pack_true(&pck); } else if (*p == 'n') { msgpack_pack_nil(&pck); } else { if (is_float(p, flen)) { msgpack_pack_double(&pck, atof(p)); } else { msgpack_pack_int64(&pck, atol(p)); } } break; case JSMN_UNDEFINED: msgpack_sbuffer_destroy(&sbuf); return NULL; } } /* dump data back to a new buffer */ *out_size = sbuf.size; buf = malloc(sbuf.size); memcpy(buf, sbuf.data, sbuf.size); msgpack_sbuffer_destroy(&sbuf); return buf; }