예제 #1
0
파일: broker.c 프로젝트: ybalt/ModBusServer
static int event(zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
	if (interrupt)
		return -1;
	zmsg_t *msg = zmsg_recv(dealer);
	zframe_t *payload = zmsg_pop(msg);
	zmsg_destroy(&msg);
	msgpack_unpacked object;
	msgpack_unpacked_init(&object);
	
	if (msgpack_unpack_next(&object, (char*)zframe_data(payload), zframe_size(payload) , NULL))
	{
		//zclock_log("message");
		//msgpack_object_print(stdout, object.data);
		char *command = (char*)m_lookup(object.data, "command");
		if (command) {
			//zclock_log("command: %s", command);
			if (streq(command, "exception")) {
			    failed++;
			}
			if (streq(command, "result")) {
				success++;
			}
			free(command);
		}
	}
	msgpack_unpacked_destroy(&object);
	zframe_destroy(&payload);
	return 0;
}
예제 #2
0
파일: main.c 프로젝트: faggion/sample-codes
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);
}
예제 #3
0
static inline int process_pack(struct tcp_conn *conn,
                               char *pack, size_t size)
{
    size_t off = 0;
    msgpack_unpacked result;
    msgpack_object entry;
    struct flb_in_tcp_config *ctx;

    ctx = conn->ctx;

    /* First pack the results, iterate concatenated messages */
    msgpack_unpacked_init(&result);
    while (msgpack_unpack_next(&result, pack, size, &off)) {
        entry = result.data;

        msgpack_pack_array(&ctx->mp_pck, 2);
        msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

        msgpack_pack_map(&ctx->mp_pck, 1);
        msgpack_pack_bin(&ctx->mp_pck, 3);
        msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
        msgpack_pack_object(&ctx->mp_pck, entry);

        ctx->buffer_id++;
    }

    msgpack_unpacked_destroy(&result);

    return 0;
}
예제 #4
0
void test()
{
	uint64_t test_u64 = 0xFFF0000000000001LL;
	size_t size = 10000000;
	msgpack_sbuffer buf;
	msgpack_sbuffer_init(&buf);

	msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);

	msgpack_pack_array(pk, size);
	{
		int idx = 0;
		for (; idx < size; ++idx)
			msgpack_pack_uint64(pk, test_u64);
	}
	msgpack_packer_free(pk);


	size_t upk_pos = 0;
	msgpack_unpacked msg;
	msgpack_unpacked_init(&msg);

	while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {
	}

	msgpack_sbuffer_destroy(&buf);
}
예제 #5
0
void
add_input_bytes(msgpack_unpacker *unpacker, char *buf, int sz)
{
	msgpack_unpacked result;
	int got = 0;

	printf("reading %d bytes\n", sz);
	//getchar();
	printf("ENT u(%u) f(%u) o(%u) p(%u)\n", (unsigned)unpacker->used, (unsigned)unpacker->free, (unsigned)unpacker->off, (unsigned)unpacker->parsed);
	msgpack_unpacker_reserve_buffer(unpacker, sz);
	printf("EXP u(%u) f(%u) o(%u) p(%u)\n", (unsigned)unpacker->used, (unsigned)unpacker->free, (unsigned)unpacker->off, (unsigned)unpacker->parsed);
	memcpy(msgpack_unpacker_buffer(unpacker), buf, sz);
	msgpack_unpacker_buffer_consumed(unpacker, sz);
	printf("CON u(%u) f(%u) o(%u) p(%u)\n", (unsigned)unpacker->used, (unsigned)unpacker->free, (unsigned)unpacker->off, (unsigned)unpacker->parsed);

	msgpack_unpacked_init(&result);
	while (msgpack_unpacker_next(unpacker, &result)) {
		got = 1;
		msgpack_object_print(stdout, result.data);
		printf("\n");
	}
	if (got) {
		msgpack_unpacker_expand_buffer(unpacker, 0);
		printf("XXX u(%u) f(%u) o(%u) p(%u)\n", (unsigned)unpacker->used, (unsigned)unpacker->free, (unsigned)unpacker->off, (unsigned)unpacker->parsed);
	}
	msgpack_unpacked_destroy(&result);
}
예제 #6
0
void new_client_connection(int fd)
{
	struct socket_info *si;
	struct client_connection *c;
	int flags;

	if ( (flags = fcntl(fd, F_GETFL, 0)) < 0)
		croak(1, "new_client_connection: fcntl(F_GETFL)");
	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
		croak(1, "new_client_connection: fcntl(F_SETFL)");
	#if defined(SO_NOSIGPIPE)
	{
		int no_sigpipe = 1;
		if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof (no_sigpipe)) < 0)
			croak(1, "new_client_connection: setsockopt of SO_NOSIGPIPE error");
	}
	#endif
	si = new_socket_info(fd);
	c = malloc(sizeof(*c));
	if (!c)
		croak(1, "new_client_connection: malloc(client_connection)");
	bzero(c, sizeof(*c));
	si->udata = c;

	PS.active_client_connections++;
	PS.total_client_connections++;

	msgpack_unpacker_init(&c->unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
	msgpack_unpacked_init(&c->input);
	on_eof(si, client_gone);
	on_read(si, client_input);
}
예제 #7
0
struct mail_body* unpack(char * buf, size_t len) {
	
	msgpack_unpack_return ret;
	msgpack_unpacked result;
	int i = 0;
	size_t off = 0;
	struct mail_body *mb;
	
	msgpack_unpacked_init(&result);
	ret = msgpack_unpack_next(&result, buf, len, &off);

	// add judge the result.type whether it is the required.
	// That is to say to identify the data type.
	judge(result.data.via.u64);

	mb->type = (unsigned long)result.data.via.u64;
	
	ret = msgpack_unpack_next(&result, buf, len, &off);	
	strncpy(mb->username, result.data.via.str.ptr, result.data.via.str.size);

	ret = msgpack_unpack_next(&result, buf, len, &off);	
	strncpy(mb->password, result.data.via.str.ptr, result.data.via.str.size);

	return mb;
}
void unpack(char const* buf, size_t len) {
    /* buf is allocated by client. */
    msgpack_unpacked result;
    size_t off = 0;
    msgpack_unpack_return ret;
    int i = 0;
    msgpack_unpacked_init(&result);
    ret = msgpack_unpack_next(&result, buf, len, &off);
    while (ret == MSGPACK_UNPACK_SUCCESS) {
        msgpack_object obj = result.data;

        /* Use obj. */
        printf("Object no %d:\n", ++i);
        msgpack_object_print(stdout, obj);
        printf("\n");
        /* If you want to allocate something on the zone, you can use zone. */
        /* msgpack_zone* zone = result.zone; */
        /* The lifetime of the obj and the zone,  */

        ret = msgpack_unpack_next(&result, buf, len, &off);
    }
    msgpack_unpacked_destroy(&result);

    if (ret == MSGPACK_UNPACK_CONTINUE) {
        printf("All msgpack_object in the buffer is consumed.\n");
    }
    else if (ret == MSGPACK_UNPACK_PARSE_ERROR) {
        printf("The data in the buf is invalid format.\n");
    }
}
예제 #9
0
yar_unpackager * yar_unpack_init(char *data, uint len) /* {{{ */ {
	yar_unpackager *unpk = malloc(sizeof(yar_unpackager));
	msgpack_unpacked_init(&unpk->msg);
	if (!msgpack_unpack_next(&unpk->msg, data, len, NULL)) {
		yar_unpack_free(unpk);
		return NULL;
	}
	return unpk;
}
예제 #10
0
파일: broker.c 프로젝트: ybalt/ModBusServer
void start_echo(char *arg)
{
	struct timeval tv;
	gettimeofday(&tv,NULL);
	start = (uint64_t)tv.tv_sec;
	//while(!interrupt) 
	//{
		char *actionid = getActionid();
		zmsg_t *msg = create_call(actionid, "worker", "sleep", arg);
		if (msg) 
		{
			zmsg_send(&msg, dealer);
			counter ++;
			zmsg_destroy(&msg);
			zmsg_t *msg = zmsg_recv(dealer);
			zframe_t *payload = zmsg_pop(msg);
			zmsg_destroy(&msg);
			msgpack_unpacked object;
			msgpack_unpacked_init(&object);
		
			if (msgpack_unpack_next(&object, (char*)zframe_data(payload), zframe_size(payload) , NULL))
			{
				//zclock_log("message");
				//msgpack_object_print(stdout, object.data);
				char *command = (char*)m_lookup(object.data, "command");
				if (command) {
					//zclock_log("command: %s", command);
					if (streq(command, "exception")) {
						failed++;
						zclock_log("exception");
					}
					if (streq(command, "result")) {
						success++;
						zclock_log("result ok");
					}
					free(command);
				}
			}
			msgpack_unpacked_destroy(&object);
			zframe_destroy(&payload);
		}
		
		gettimeofday(&tv,NULL);
		uint64_t end = (uint64_t)tv.tv_sec;
		if ((end - start) == 1) 
		{
			float speed = counter/(end-start);
			zclock_log("speed %f m/s, failed %ld, success %ld", speed, failed, success);
			counter = 0;
			failed = 0;
			success = 0;
			start = end;
		} 
	//} //end while
}
예제 #11
0
/**
 * msgpack_conn_read - Buffer data from a socket read and deserialize.
 */
int
msgpack_conn_read(GIOChannel *channel, GIOCondition condition, void *data)
{
  struct msgpack_conn *conn;
  msgpack_unpacked result;
  size_t bytes_read;
  int r = TRUE;

  GIOStatus status;
  GError *error = NULL;

  conn = (struct msgpack_conn *)data;

  msgpack_unpacked_init(&result);

  // Keep reading until we've flushed the channel's read buffer completely.
  do {
    // Reserve enough space in the msgpack_unpacker buffer for a read.
    msgpack_unpacker_reserve_buffer(&conn->mc_unpacker, BUFSIZE);

    // Read up to BUFSIZE bytes into the stream.
    status = g_io_channel_read_chars(channel,
        msgpack_unpacker_buffer(&conn->mc_unpacker), BUFSIZE, &bytes_read,
        &error);

    if (status == G_IO_STATUS_ERROR) {
      g_warning("msgpack_conn_read: Read from socket failed.");
    }

    // Inform the msgpack_unpacker how much of the buffer we actually consumed.
    msgpack_unpacker_buffer_consumed(&conn->mc_unpacker, bytes_read);

    // Pop as many msgpack objects as we can get our hands on.
    while (msgpack_unpacker_next(&conn->mc_unpacker, &result)) {
      if (conn->mc_recv(conn, &result.data)) {
        g_warning("paxos_read_conn: Dispatch failed.");
        r = FALSE;
        break;
      }
    }

    // Drop the connection if we're at the end.
    if (status == G_IO_STATUS_EOF) {
      conn->mc_drop(conn);
      r = FALSE;
      break;
    }

  } while (g_io_channel_get_buffer_condition(channel) & G_IO_IN);

  msgpack_unpacked_destroy(&result);
  return r;
}
예제 #12
0
파일: client.c 프로젝트: nferch/pcma
int handle_rep(zmq_msg_t * msg)
{
    msgpack_object obj;
    msgpack_unpacked pack;
    char *errmsg;

    msgpack_unpacked_init(&pack);

    if (!msgpack_unpack_next
            (&pack, zmq_msg_data(msg), zmq_msg_size(msg), NULL)) {
        LOG_ERROR("handle_rep: msgpack_unpack_next failed\n");
        return (-1);
    }

    obj = pack.data;

    if (obj.type != MSGPACK_OBJECT_ARRAY) {
        LOG_ERROR("handle_rep: not an array\n");
        return (-2);
    }

    if (obj.via.array.size < 1) {
        LOG_ERROR("handle_rep: empty array\n");
        return (-3);
    }

    if (obj.via.array.ptr[0].type != MSGPACK_OBJECT_BOOLEAN) {
        LOG_ERROR("handle_rep: first entry is nota  boolean\n");
        return (-4);
    }

    if (obj.via.array.ptr[0].via.boolean == false) {
        if (obj.via.array.size > 1
                && obj.via.array.ptr[1].type == MSGPACK_OBJECT_RAW) {
            errmsg = raw_to_string(&(obj.via.array.ptr[1].via.raw));
            if (errmsg) {
                LOG_SERV("%s\n", errmsg);
                free(errmsg);
            }
        }
        msgpack_unpacked_destroy(&pack);
        return (1);
    }

    if (obj.via.array.size > 1) {
        /* Technically speaking unspecified, but I feel lazy */
        msgpack_object_print(stdout, obj.via.array.ptr[1]);
        printf("\n");
    }
    return (0);
}
예제 #13
0
void flb_pack_print(char *data, size_t bytes)
{
    msgpack_unpacked result;
    size_t off = 0, cnt = 0;

    msgpack_unpacked_init(&result);
    while (msgpack_unpack_next(&result, data, bytes, &off)) {
        /* FIXME: lazy output */
        printf("[%zd] ", cnt++);
        msgpack_object_print(stdout, result.data);
        printf("\n");
    }
    msgpack_unpacked_destroy(&result);
}
예제 #14
0
void tmate_decoder_commit(struct tmate_decoder *decoder, size_t len)
{
	struct tmate_unpacker _uk, *uk = &_uk;
	msgpack_unpacked result;

	msgpack_unpacker_buffer_consumed(&decoder->unpacker, len);

	msgpack_unpacked_init(&result);
	while (msgpack_unpacker_next(&decoder->unpacker, &result)) {
		init_unpacker(uk, result.data);
		decoder->reader(decoder->userdata, uk);
	}
	msgpack_unpacked_destroy(&result);
}
예제 #15
0
/**
 * Process incoming data from the given fd.
 *
 * \see fromStdinOut() and QSocketNotifier()
 */
void MsgpackIODevice::dataAvailableFd(int fd)
{
	qint64 bytes = read(fd, msgpack_unpacker_buffer(&m_uk),
			msgpack_unpacker_buffer_capacity(&m_uk));
	if (bytes > 0) {
		msgpack_unpacker_buffer_consumed(&m_uk, bytes);
		msgpack_unpacked result;
		msgpack_unpacked_init(&result);
		while(msgpack_unpacker_next(&m_uk, &result)) {
			dispatch(result.data);
		}
	} else if (bytes == -1) {
		setError(InvalidDevice, tr("Error when reading from device"));
	}
}
예제 #16
0
/**
 * Process incoming data.
 *
 * \see fromStdinOut() and StdinReader()
 */
void MsgpackIODevice::dataAvailableStdin(const QByteArray& data)
{
	if ( (quint64)data.length() > msgpack_unpacker_buffer_capacity(&m_uk)) {
		setError(InvalidDevice, tr("Error when reading from stdin, BUG(buffered data exceeds capaciy)"));
		return;
	} else if ( data.length() > 0 ) {
		memcpy(msgpack_unpacker_buffer(&m_uk), data.constData(), data.length());
		msgpack_unpacker_buffer_consumed(&m_uk, data.length());
		msgpack_unpacked result;
		msgpack_unpacked_init(&result);
		while(msgpack_unpacker_next(&m_uk, &result)) {
			dispatch(result.data);
		}
	}
}
예제 #17
0
파일: rpc.c 프로젝트: 40a/experiments
void rpc_call_handle_response(rpc_call_t *rpc, zmq_msg_t *response) {
  int rc;
  msgpack_unpacked response_msg;
  msgpack_unpacked_init(&response_msg);
  rc = msgpack_unpack_next(&response_msg, zmq_msg_data(response),
                           zmq_msg_size(response), NULL);
  insist_return(rc, (void)(0), "Failed to unpack message '%.*s'",
                (int)zmq_msg_size(response), (char *)zmq_msg_data(response));

  msgpack_object response_obj = response_msg.data;
  /* TODO(sissel): Call rpc->callback */
  printf("rpc call response: ");
  msgpack_object_print(stdout, response_obj);
  printf("\n");
} /* rpc_service_handle */
예제 #18
0
static ktype_t msgpack_unpackTo(CTX ctx, const char *buf, size_t size, ksfp_t *sfp)
{
	ktype_t type;
	msgpack_unpacker upk;
	msgpack_unpacked result;
	msgpack_unpacker_init(&upk, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
	msgpack_unpacker_reserve_buffer(&upk, size);
	memcpy(msgpack_unpacker_buffer(&upk), buf, size);
	msgpack_unpacker_buffer_consumed(&upk, size);
	msgpack_unpacked_init(&result);
	msgpack_unpacker_next(&upk, &result);
	type = msgpack_read(ctx, result.data, sfp);
	msgpack_unpacker_destroy(&upk);
	msgpack_unpacked_destroy(&result);
	return type;
}
예제 #19
0
파일: msgpack.c 프로젝트: tailhook/objpath
int main(int argc, char **argv) {
    void *pattern = objpath_compile(argv[1]);
    msgpack_unpacker pac;
    msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);

    msgpack_unpacked result;
    msgpack_unpacked_init(&result);
    while(1) {
        int rc = read_more(&pac);
        if(!rc) break;
        while(msgpack_unpacker_next(&pac, &result)) {
            match(&result.data, pattern);
        }
    }
    return 0;
}
예제 #20
0
void tmate_decoder_commit(struct tmate_decoder *decoder, size_t len)
{
	msgpack_unpacked result;

	msgpack_unpacker_buffer_consumed(&decoder->unpacker, len);

	msgpack_unpacked_init(&result);
	while (msgpack_unpacker_next(&decoder->unpacker, &result)) {
		handle_message(decoder, result.data);
	}
	msgpack_unpacked_destroy(&result);

	if (msgpack_unpacker_message_size(&decoder->unpacker) >
						TMATE_MAX_MESSAGE_SIZE) {
		tmate_fatal("Message too big");
	}
}
static gboolean
receive_message(Session *session)
{
  session->message_size =
    read(session->socket_fd, session->message, MESSAGE_SIZE);
  if (session->message_size == -1) {
    perror("failed to read()");
    return FALSE;
  }

  if (session->message_size == 0) {
    return stop_session(session);
  }

  if (parse_data) {
    if (session->message[0] == '{') {
      JsonParser *parser;
      parser = json_parser_new();
      json_parser_load_from_data(parser,
                                 session->message, session->message_size,
                                 NULL);
      g_object_unref(parser);
    } else {
      msgpack_unpacked unpacked;
      size_t offset;
      msgpack_unpacked_init(&unpacked);
      msgpack_unpack_next(&unpacked,
                          session->message, session->message_size,
                          &offset);
      msgpack_unpacked_destroy(&unpacked);
    }
  }

  {
    int epoll_fd = session->context->epoll_fd;
    struct epoll_event event;
    event.events = EPOLLOUT;
    event.data.ptr = session;
    if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, session->socket_fd, &event) == -1) {
      perror("failed to epoll_ctl(EPOLL_CTL_MOD, client_socket_fd)");
      return FALSE;
    }
  }

  return TRUE;
}
예제 #22
0
파일: unpack.c 프로젝트: deltheil/unpack.c
int main(int argc, char **argv) {
  g_cmd = argv[0];
  if (argc > 2) usage();
  if (argc == 2) g_path = argv[1];

  char buf[CHK_SIZE];
  int fd;
  ssize_t rsize;
  msgpack_unpacker u;
  msgpack_unpacked obj;

  if (!strcmp(g_path, "-"))
    fd = 0;
  else if (strlen(g_path) > 0)
    fd = open(g_path, O_RDONLY);
  else
    fd = 0;

  if (fd < 0) goto readerr;

  msgpack_unpacker_init(&u, BUF_SIZE);
  msgpack_unpacked_init(&obj);

  while ((rsize = read(fd, buf, CHK_SIZE)) > 0) {
    msgpack_unpacker_reserve_buffer(&u, rsize);
    memcpy(msgpack_unpacker_buffer(&u), buf, rsize);
    msgpack_unpacker_buffer_consumed(&u, rsize);
    while (msgpack_unpacker_next(&u, &obj)) {
      msgpack_object_print(stdout, obj.data);
      putchar('\n');
    }
  }

  msgpack_unpacker_destroy(&u);
  msgpack_unpacked_destroy(&obj);
  if (fd > 0) close(fd);

  return 0;

readerr:
  fprintf(stderr, "file read error\n");
  exit(1);
}
예제 #23
0
TEST(pack, insufficient)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    EXPECT_EQ(0, msgpack_pack_int(pk, 255)); // uint8 (2bytes)

    msgpack_unpack_return success;
    size_t offset = 0;

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    success = msgpack_unpack_next(&msg, sbuf->data, 1, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);
    EXPECT_EQ(0u, offset);

    msgpack_unpacked_destroy(&msg);

    msgpack_sbuffer_free(sbuf);
    msgpack_packer_free(pk);
}
예제 #24
0
TEST(unpack, sequence)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    EXPECT_EQ(0, msgpack_pack_int(pk, 1));
    EXPECT_EQ(0, msgpack_pack_int(pk, 2));
    EXPECT_EQ(0, msgpack_pack_int(pk, 3));

    msgpack_packer_free(pk);

    msgpack_unpack_return success;
    size_t offset = 0;

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(1u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(2u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(3u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);

    msgpack_sbuffer_free(sbuf);
    msgpack_unpacked_destroy(&msg);
}
예제 #25
0
파일: first.c 프로젝트: rryqszq4/sweet-clib
int main(void)
{
	msgpack_sbuffer* buffer = msgpack_sbuffer_new();
	msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

	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);

	msgpack_unpacked msg;
	msgpack_unpacked_init(&msg);
	bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);

	msgpack_object obj = msg.data;
	msgpack_object_print(stdout, obj);
	printf("\n");

	msgpack_sbuffer_free(buffer);
	msgpack_packer_free(pk);

}
예제 #26
0
/**
 * Process incoming data from the underlying device
 *
 * \see QIODevice()
 */
void MsgpackIODevice::dataAvailable()
{
	qint64 read = 1;
	while (read > 0) {
		if ( msgpack_unpacker_buffer_capacity(&m_uk) == 0 ) {
			if ( !msgpack_unpacker_reserve_buffer(&m_uk, 8192 ) ) {
				// FIXME: error out
				qWarning() << "Could not allocate memory in unpack buffer";
				return;
			}
		}

		read = m_dev->read(msgpack_unpacker_buffer(&m_uk), msgpack_unpacker_buffer_capacity(&m_uk));
		if ( read > 0 ) {
			msgpack_unpacker_buffer_consumed(&m_uk, read);
			msgpack_unpacked result;
			msgpack_unpacked_init(&result);
			while(msgpack_unpacker_next(&m_uk, &result)) {
				dispatch(result.data);
			}
		}
	}
}
예제 #27
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;
}
예제 #28
0
void unpack(receiver* r) {
    /* buf is allocated by unpacker. */
    msgpack_unpacker* unp = msgpack_unpacker_new(100);
    msgpack_unpacked result;
    msgpack_unpack_return ret;
    size_t recv_len;
    int recv_count = 0;
    int i = 0;

    msgpack_unpacked_init(&result);
    while (true) {
        recv_len = receiver_to_unpacker(r, EACH_RECV_SIZE, unp);
        if (recv_len == 0) break; // (reached end of input)
        printf("receive count: %d %zd bytes received.\n", recv_count++, recv_len);
        ret = msgpack_unpacker_next(unp, &result);
        while (ret == MSGPACK_UNPACK_SUCCESS) {
            msgpack_object obj = result.data;

            /* Use obj. */
            printf("Object no %d:\n", ++i);
            msgpack_object_print(stdout, obj);
            printf("\n");
            /* If you want to allocate something on the zone, you can use zone. */
            /* msgpack_zone* zone = result.zone; */
            /* The lifetime of the obj and the zone,  */

            ret = msgpack_unpacker_next(unp, &result);
        }
        if (ret == MSGPACK_UNPACK_PARSE_ERROR) {
            printf("The data in the buf is invalid format.\n");
            msgpack_unpacked_destroy(&result);
            return;
        }
    }
    msgpack_unpacked_destroy(&result);
    msgpack_unpacker_free(unp);
}
예제 #29
0
static void scl_read_power(float *_voltage, float *_current)
{
   char buffer[128];
   int ret = scl_recv_static(scl_socket, buffer, sizeof(buffer));
   if (ret > 0)
   {
      msgpack_unpacked msg;
      msgpack_unpacked_init(&msg);
      if (msgpack_unpack_next(&msg, buffer, ret, NULL))
      {
         msgpack_object root = msg.data;
         assert (root.type == MSGPACK_OBJECT_ARRAY);
         assert(root.via.array.size == 2);
         *_voltage = root.via.array.ptr[0].via.dec;
         *_current = root.via.array.ptr[1].via.dec;
      }
      msgpack_unpacked_destroy(&msg);
   }
   else
   {
      sleep(1);
      LOG(LL_ERROR, "could not read voltage");
   }
}
예제 #30
0
파일: server.c 프로젝트: harrylu/crabdb
static void *client_thread(void *arg) {

    ssize_t nwritten;

    pthread_detach(pthread_self());

    int client_fd = (int) (long) arg;

    int yes = 1, tcp_keepalive_probes = 3, tcp_keepalive_time = 5, tcp_keepalive_intvl = 2;
    int nodelay = 1;

    if (setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPCNT, &tcp_keepalive_probes, sizeof(tcp_keepalive_probes)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepalive_time, sizeof(tcp_keepalive_time)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepalive_intvl, sizeof(tcp_keepalive_intvl)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    size_t request_size = 8192;
    struct request *request_back = NULL;
    struct request *request = (struct request *) malloc(request_size);
    if (!request) {
        log_error("malloc failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    bool enable_gzip = false;

    while (1) {

        struct response_header response_header;

        ssize_t recvsize = readn(client_fd, request, sizeof(struct request));

        if (!recvsize) {
            log_warn("peer closed connection");
            break;
        }

        if (recvsize < (ssize_t) sizeof(struct request)) {
            log_warn("error while receiving header, received %zd", recvsize);
            break;
        }

        if (request->payload_size > 256 * 1024 * 1024) {
            log_warn("payload size %"PRIu32" too large", request->payload_size);
            break;
        }

        if (sizeof(struct request) + request->payload_size > request_size) {
            request_size = sizeof(struct request) + request->payload_size;
            request_back = request;
            request = realloc(request, request_size);
            if (!request) {
                log_error("realloc failed: %s", strerror(errno));
                free(request_back);
                break;
            }
        }

        recvsize = readn(client_fd, request->payload, request->payload_size);

        if (!recvsize) {
            log_warn("peer closed connection");
            break;
        }

        if (recvsize < request->payload_size) {
            log_warn("error while receiving payload, received %zd (should be %"PRIu32")", recvsize, request->payload_size);
            break;
        }

        if (request->flags & REQUEST_FLAG_GZIP) {
            enable_gzip = true;
            uLongf destLen = 16;
            Bytef *dest = malloc(destLen);
            if (!dest) {
                log_error("malloc failed: %s", strerror(errno));
                break;
            }

            int ret;
keep_malloc:
            ret = uncompress(dest, &destLen, (Bytef *) request->payload, request->payload_size);
            if (ret == Z_BUF_ERROR) {
                destLen = destLen * 2;
                free(dest);
                dest = malloc(destLen);
                if (!dest) {
                    log_error("malloc failed: %s", strerror(errno));
                    break;
                }
                goto keep_malloc;
            }

            if (ret != Z_OK) {
                free(dest);
                break;
            }

            request->flags &= ~REQUEST_FLAG_GZIP;
            if (sizeof(struct request) + destLen > request_size) {
                request_size = sizeof(struct request) + destLen;
                request_back = request;
                request = realloc(request, request_size);
                if (!request) {
                    log_error("realloc failed: %s", strerror(errno));
                    free(request_back);
                    free(dest);
                    break;
                }
            }
            memcpy(request->payload, dest, destLen);
            request->payload_size = destLen;

            free(dest);
        }

        bool success = msgpack_unpack_next(&msg, request->payload, request->payload_size, NULL);
        if (!success) {
            log_warn("error while parsing payload");
            break;
        }

        msgpack_sbuffer* buffer = msgpack_sbuffer_new();
        msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

        response_header.seq = request->seq;

        __sync_add_and_fetch(&pending_commands, 1);
        if (server->terminated) {
            __sync_add_and_fetch(&pending_commands, -1);
            break;
        }

        if (server->redirection) {
            response_header.status = RESPONSE_STATUS_REDIRECTION;
            msgpack_pack_map(pk, 2);
            mp_pack_string(pk, "host");
            mp_pack_string(pk, server->redirection->host);
            mp_pack_string(pk, "port");
            msgpack_pack_uint16(pk, server->redirection->port);
        }
        else {
            response_header.status = execute_command(request, request->command, msg.data, pk);
        }
        __sync_add_and_fetch(&pending_commands, -1);

        msgpack_packer_free(pk);

        if (!(request->flags & REQUEST_FLAG_NO_REPLY)) {
            if (!(buffer->size > 0)) {
                response_header.payload_size = 0;
                nwritten = writen(client_fd, &response_header, sizeof(response_header));
                if (!nwritten) {
                    log_error("writen failed: %s", strerror(errno));
                    msgpack_sbuffer_free(buffer);
                    break;
                }
            }
            else {
                if (!enable_gzip) {
                    response_header.payload_size = buffer->size;
                    nwritten = writen(client_fd, &response_header, sizeof(response_header));
                    if (!nwritten) {
                        log_error("writen failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }

                    nwritten = writen(client_fd, buffer->data, buffer->size);
                    if (!nwritten) {
                        log_error("writen failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }
                }
                else {
                    uLongf destLen = buffer->size * 1.00101 + 13;
                    Bytef *dest = malloc(destLen);
                    if (!dest) {
                        log_error("malloc failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }
                    int ret = compress(dest, &destLen, (Bytef *) buffer->data, buffer->size);

                    while (ret == Z_BUF_ERROR) {
                        destLen = destLen * 2 + 16;
                        free(dest);
                        dest = malloc(destLen);
                        if (!dest) {
                            log_error("malloc failed: %s", strerror(errno));
                            msgpack_sbuffer_free(buffer);
                            break;
                        }
                        ret = compress(dest, &destLen, (Bytef *) buffer->data, buffer->size);
                    }

                    if (ret != Z_OK) {
                        log_error("error while compressing response: %d", ret);
                        if (dest)
                            free(dest);
                        break;
                    }

                    response_header.payload_size = destLen;
                    nwritten = writen(client_fd, &response_header, sizeof(response_header));
                    if (!nwritten) {
                        log_warn("peer closed connection");
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < 0) {
                        log_error("send response header failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < (ssize_t)sizeof(response_header)) {
                        log_warn("error while sending response header, sent %zd (should be %"PRIu64")", nwritten, sizeof(response_header));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    nwritten = writen(client_fd, dest, destLen);
                    if (!nwritten) {
                        log_warn("peer closed connection");
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < 0) {
                        log_error("send response failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < (ssize_t)destLen) {
                        log_warn("error while sending response, sent %zd (should be %"PRIu64")", nwritten, destLen);
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    free(dest);
                }
            }
        }

        msgpack_sbuffer_free(buffer);

        if (request_size > 65536) {
            request_size = 8192;
            request_back = request;
            request = realloc(request, request_size);
            if (!request) {
                log_error("realloc failed: %s", strerror(errno));
                free(request_back);
                break;
            }
        }
    }

    close(client_fd);

    free(request);
    msgpack_unpacked_destroy(&msg);

    return NULL;
}