Beispiel #1
0
int
main(void)
{
	// [0,42,{key=>"value"},["1.2.3","4.5.6"]]
	char *buf1 = "\x94\x00\x2a\x81\xa3\x6b\x65\x79\xa5\x76\x61\x6c\x75\x65\x92\xa5\x31\x2e\x32\x2e\x33\xa5\x34\x2e\x35\x2e\x36";
	int buf1_len = 27;
	// ["ModeratelyLongStringLongerThan32Bytes",1,-2,{dict=>42}]
	char *buf2 = "\x94\xda\x00\x25\x4d\x6f\x64\x65\x72\x61\x74\x65\x6c\x79\x4c\x6f"
				 "\x6e\x67\x53\x74\x72\x69\x6e\x67\x4c\x6f\x6e\x67\x65\x72\x54\x68"
				 "\x61\x6e\x33\x32\x42\x79\x74\x65\x73\x01\xfe\x81\xa4\x64\x69\x63"
				 "\x74\x2a";
	int buf2_len = 50;
	msgpack_unpacker unpacker;

	msgpack_unpacker_init(&unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);

	while (buf1_len) {
		add_input_bytes(&unpacker, buf1, 3);
		buf1_len -= 3;
		buf1 += 3;
		printf("buf1 len is now %d\n", buf1_len);
	}
	while (buf2_len) {
		add_input_bytes(&unpacker, buf2, 2);
		buf2_len -= 2;
		buf2 += 2;
		printf("buf2 len is now %d\n", buf2_len);
	}

	return 0;
}
Beispiel #2
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);
}
Beispiel #3
0
/**
 * msgpack_conn_new - Set up read/write buffering.
 */
struct msgpack_conn *
msgpack_conn_new(GIOChannel *channel, msgpack_conn_recv_t recv,
    msgpack_conn_drop_t drop)
{
  struct msgpack_conn *conn;

  if (channel == NULL) {
    return NULL;
  }

  conn = g_malloc0(sizeof(*conn));
  conn->mc_channel = channel;
  conn->mc_recv = recv;
  conn->mc_drop = drop;

  // TODO: This needs to be done when the socket is first opened.
  // Put the conn's channel into nonblocking mode.
  g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);

  // Set the channel encoding to binary.
  g_io_channel_set_encoding(channel, NULL, NULL);

  // Set up the read listener.
  msgpack_unpacker_init(&conn->mc_unpacker, BUFSIZE);
  g_io_add_watch(channel, G_IO_IN, msgpack_conn_read, conn);

  // Set up the write buffer.
  conn->mc_write_buffer = g_string_sized_new(BUFSIZE);

  return conn;
}
Beispiel #4
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;
}
Beispiel #5
0
/**
 * paxos_peer_init - Set up peer read/write buffering.
 */
struct paxos_peer *
paxos_peer_init(GIOChannel *channel)
{
  struct paxos_peer *peer;

  if (channel == NULL) {
    return NULL;
  }

  peer = g_malloc0(sizeof(*peer));
  peer->pp_channel = channel;

  // TODO: this needs to be done when the socket is first opened
  // Put the peer's channel into nonblocking mode.
  g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);

  // Set the channel encoding to binary.
  g_io_channel_set_encoding(channel, NULL, NULL);

  // Set up the read listener.
  msgpack_unpacker_init(&peer->pp_unpacker, PIO_BUFSIZE);
  g_io_add_watch(channel, G_IO_IN, paxos_peer_read, peer);

  // Set up the write buffer.
  peer->pp_write_buffer = g_string_sized_new(PIO_BUFSIZE);

  return peer;
}
ANetworkController::ANetworkController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FClassFinder<AMockingVehicle> CarFinder(TEXT("/Game/Vehicle/MockingCar"));
	static ConstructorHelpers::FClassFinder<AMockingVehicle> SedanFinder(TEXT("/Game/Vehicle/MockingSedan"));
	static ConstructorHelpers::FClassFinder<AMockingVehicle> EmergencyFinder(TEXT("/Game/Vehicle/MockingEmergencyVehicle"));
	static ConstructorHelpers::FClassFinder<AMockingVehicle> BusFinder(TEXT("/Game/Vehicle/MockingBus"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Yellow(TEXT("Material'/Game/Sedan/Materials/M_Vehicle_Sedan_yelllo.M_Vehicle_Sedan_yelllo'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Blue(TEXT("Material'/Game/Sedan/Materials/M_Vehicle_Sedan.M_Vehicle_Sedan'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Green(TEXT("Material'/Game/Sedan/Materials/M_Vehicle_Sedan_green.M_Vehicle_Sedan_green'"));


	VehicleTypeClass.Add((uint8)EVehicleType::VT_Car, CarFinder.Class);
	VehicleTypeClass.Add((uint8)EVehicleType::VT_Sedan, SedanFinder.Class);
	VehicleTypeClass.Add((uint8)EVehicleType::VT_Bus, BusFinder.Class);
	VehicleTypeClass.Add((uint8)EVehicleType::VT_Emergency, EmergencyFinder.Class);

	TArray<UMaterial*> Materials;
	Materials.Add(Yellow.Object);
	Materials.Add(Blue.Object);
	Materials.Add(Green.Object);

	VehicleTypeMaterials.Add((uint8)EVehicleType::VT_Car, Materials);
	VehicleTypeMaterials.Add((uint8)EVehicleType::VT_Sedan, Materials);

	static ConstructorHelpers::FClassFinder<AActor> ArrowFinder(TEXT("/Game/Blueprints/Arrow"));

	AArrow = ArrowFinder.Class;

	msgpack_unpacker_init(&unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
}
Beispiel #7
0
void tmate_decoder_init(struct tmate_decoder *decoder, tmate_decoder_reader *reader,
			void *userdata)
{
	if (!msgpack_unpacker_init(&decoder->unpacker, UNPACKER_RESERVE_SIZE))
		tmate_fatal("Cannot initialize the unpacker");
	decoder->reader = reader;
	decoder->userdata = userdata;
}
static VALUE Unpacker_alloc(VALUE klass)
{
    msgpack_unpacker_t* uk = ALLOC_N(msgpack_unpacker_t, 1);
    msgpack_unpacker_init(uk);

    VALUE self = Data_Wrap_Struct(klass, msgpack_unpacker_mark, Unpacker_free, uk);

    uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);

    return self;
}
Beispiel #9
0
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size)
{
    msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker));
    if(mpac == NULL) {
        return NULL;
    }

    if(!msgpack_unpacker_init(mpac, initial_buffer_size)) {
        free(mpac);
        return NULL;
    }

    return mpac;
}
Beispiel #10
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;
}
Beispiel #11
0
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;
}
Beispiel #12
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);
}
Beispiel #13
0
MsgpackIODevice::MsgpackIODevice(QIODevice *dev, QObject *parent)
:QObject(parent), m_reqid(0), m_dev(dev), m_encoding(0), m_reqHandler(0), m_error(NoError)
{
	qRegisterMetaType<MsgpackError>("MsgpackError");
	msgpack_unpacker_init(&m_uk, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);

	if (m_dev) {
		// MSVC: wont build without the (mspack_packer_write) cast
		msgpack_packer_init(&m_pk, this, (msgpack_packer_write)MsgpackIODevice::msgpack_write_to_dev);

		m_dev->setParent(this);
		connect(m_dev, &QAbstractSocket::readyRead,
				this, &MsgpackIODevice::dataAvailable);

		if ( !m_dev->isSequential() ) {
			setError(InvalidDevice, tr("IO device needs to be sequential"));
			return;
		}
	}
}
Beispiel #14
0
void tmate_decoder_init(struct tmate_decoder *decoder)
{
	if (!msgpack_unpacker_init(&decoder->unpacker, 2*TMATE_MAX_MESSAGE_SIZE))
		tmate_fatal("cannot initialize the unpacker");
	decoder->ready = 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);
}