Ejemplo n.º 1
0
/**
 * msgpack_conn_destroy - Free up a conn.
 */
void
msgpack_conn_destroy(struct msgpack_conn *conn)
{
  GIOStatus status;
  GError *error = NULL;

  if (conn == NULL) {
    return;
  }

  // Clean up the msgpack unpacker/read buffer.
  msgpack_unpacker_destroy(&conn->mc_unpacker);

  // Get rid of our event listeners.
  while (g_source_remove_by_user_data(conn));

  // Flush and destroy the GIOChannel.
  status = g_io_channel_shutdown(conn->mc_channel, TRUE, &error);
  if (status != G_IO_STATUS_NORMAL) {
    g_warning("msgpack_conn_destroy: Trouble destroying conn.");
  }

  // Free the conn structure itself.
  g_free(conn);
}
Ejemplo n.º 2
0
/**
 * paxos_peer_destroy - Free up a peer.
 */
void
paxos_peer_destroy(struct paxos_peer *peer)
{
  GIOStatus status;
  GError *error = NULL;

  if (peer == NULL) {
    return;
  }

  // Clean up the msgpack read buffer / unpacker.
  msgpack_unpacker_destroy(&peer->pp_unpacker);

  // Get rid of our event listeners.
  while (g_source_remove_by_user_data(peer));

  // Flush and destroy the GIOChannel.
  status = g_io_channel_shutdown(peer->pp_channel, TRUE, &error);
  if (status != G_IO_STATUS_NORMAL) {
    g_warning("paxos_peer_destroy: Trouble destroying peer.");
  }

  // Free the peer structure itself.
  g_free(peer);
}
Ejemplo n.º 3
0
msgpack_unpacked *filter_receive_message(Filter * const filter)
{
    msgpack_unpacker * const msgpack_unpacker = &filter->msgpack_unpacker;    
    msgpack_unpacker_destroy(msgpack_unpacker);
    msgpack_unpacker_init(msgpack_unpacker, FILTER_UNPACK_BUFFER_SIZE);
    msgpack_unpacker_reserve_buffer(msgpack_unpacker,
                                    FILTER_READ_BUFFER_SIZE);
    msgpack_unpacked * const message = &filter->message;
    
    ssize_t readnb;    
    while (msgpack_unpacker_next(msgpack_unpacker, message) == false) {
        assert(msgpack_unpacker_buffer_capacity(msgpack_unpacker) > 0U);
        readnb = safe_read_partial
            (filter->upipe_stdout.fd_read,
                msgpack_unpacker_buffer(msgpack_unpacker),
                msgpack_unpacker_buffer_capacity(msgpack_unpacker));
        if (readnb <= (ssize_t) 0) {
            assert(0);
            return NULL;
        }
        msgpack_unpacker_buffer_consumed(msgpack_unpacker, readnb);
    }
    assert(message->data.type == MSGPACK_OBJECT_MAP);
    
    return message;
}
Ejemplo n.º 4
0
static void Unpacker_free(msgpack_unpacker_t* uk)
{
    if(uk == NULL) {
        return;
    }
    msgpack_unpacker_destroy(uk);
    free(uk);
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
static void
client_gone(struct socket_info *si)
{
	struct client_connection *c = si->udata;

	PS.active_client_connections--;

	si->udata = NULL;
	free_all_client_request_info_for_fd(si->fd);
	delete_socket_info(si);
	if (c) {
		msgpack_unpacked_destroy(&c->input);
		msgpack_unpacker_destroy(&c->unpacker);
		free(c);
	}
	if (!opt_quiet)
		fprintf(stderr, "client disconnect\n");
}
Ejemplo n.º 7
0
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);
}
Ejemplo n.º 8
0
void msgpack_unpacker_free(msgpack_unpacker* mpac)
{
    msgpack_unpacker_destroy(mpac);
    free(mpac);
}
Ejemplo n.º 9
0
MsgpackIODevice::~MsgpackIODevice()
{
	//msgpack_packer_destroy(&m_pk);
	msgpack_unpacker_destroy(&m_uk);
}
Ejemplo n.º 10
0
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);
}
Ejemplo n.º 11
0
void tmate_decoder_destroy(struct tmate_decoder *decoder)
{
	msgpack_unpacker_destroy(&decoder->unpacker);
	memset(decoder, 0, sizeof(*decoder));
}