Пример #1
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;
}
Пример #2
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);
}
Пример #3
0
int read_more(msgpack_unpacker *pac) {
    msgpack_unpacker_reserve_buffer(pac, 16384);
    int res = read(0, msgpack_unpacker_buffer(pac), 16384);
    if(res > 0) {
        msgpack_unpacker_buffer_consumed(pac, res);
        return 1;
    }
    return 0;
}
Пример #4
0
void tmate_decoder_get_buffer(struct tmate_decoder *decoder,
			      char **buf, size_t *len)
{
	if (!msgpack_unpacker_reserve_buffer(&decoder->unpacker, UNPACKER_RESERVE_SIZE))
		tmate_fatal("cannot expand decoder buffer");

	*buf = msgpack_unpacker_buffer(&decoder->unpacker);
	*len = msgpack_unpacker_buffer_capacity(&decoder->unpacker);
}
Пример #5
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;
}
Пример #6
0
size_t receiver_to_unpacker(receiver* r, size_t request_size,
        msgpack_unpacker *unpacker)
{
    size_t recv_len;
    // make sure there's enough room, or expand the unpacker accordingly
    if (msgpack_unpacker_buffer_capacity(unpacker) < request_size) {
        msgpack_unpacker_reserve_buffer(unpacker, request_size);
        assert(msgpack_unpacker_buffer_capacity(unpacker) >= request_size);
    }
    recv_len = receiver_recv(r, msgpack_unpacker_buffer(unpacker),
                             request_size);
    msgpack_unpacker_buffer_consumed(unpacker, recv_len);
    return recv_len;
}
Пример #7
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;
}
Пример #8
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);
}
Пример #9
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);
			}
		}
	}
}
Пример #10
0
static void
client_input(struct socket_info *si)
{
	struct client_connection *c = si->udata;
	char buf[1500];
	int n;
	int got = 0;
	int ok;

	if (!c)
		croak(1, "client_input: no client_connection information");
	if ( (n = read(si->fd, buf, 1500)) == -1) {
		switch (errno) {
		case EPIPE:
			client_gone(si);
			return;
		case ECONNRESET:
			client_gone(si);
			return;
		}
		croak(1, "client_input: read error");
	}
	if (n == 0) {
		client_gone(si);
		return;
	}

	msgpack_unpacker_reserve_buffer(&c->unpacker, n);
	memcpy(msgpack_unpacker_buffer(&c->unpacker), buf, n);
	msgpack_unpacker_buffer_consumed(&c->unpacker, n);

	while (msgpack_unpacker_next(&c->unpacker, &c->input)) {
		msgpack_object *o;
		uint32_t cid;
		uint32_t type;

		got = 1;
		ok = -1;
		PS.client_requests++;
		si->PS.client_requests++;

		//if (!opt_quiet) {
		//	printf("got client input: ");
		//	msgpack_object_print(stdout, c->input.data);
		//	printf("\n");
		//}
		o = &c->input.data;
		if (o->type != MSGPACK_OBJECT_ARRAY) {
			error_reply(si, RT_ERROR, 0, "Request is not an array");
			goto end;
		}
		if (o->via.array.size < 1) {
			error_reply(si, RT_ERROR, 0, "Request is an empty array");
			goto end;
		}
		if (o->via.array.size < 2) {
			error_reply(si, RT_ERROR, 0, "Request without an id");
			goto end;
		}
		if (o->via.array.ptr[RI_CID].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
			error_reply(si, RT_ERROR, 0, "Request id is not a positive integer");
			goto end;
		}
		cid = o->via.array.ptr[RI_CID].via.u64;
		if (o->via.array.ptr[RI_TYPE].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
			error_reply(si, RT_ERROR, cid, "Request type is not a positive integer");
			goto end;
		}
		type = o->via.array.ptr[RI_TYPE].via.u64;
		switch (type) {
		case RT_SETOPT:
			ok = handle_setopt_request(si, cid, o);
			break;
		case RT_GETOPT:
			ok = handle_getopt_request(si, cid, o);
			break;
		case RT_INFO:
			ok = handle_info_request(si, cid, o);
			break;
		case RT_DEST_INFO:
			ok = handle_dest_info_request(si, cid, o);
			break;
		case RT_GET:
			ok = handle_get_request(si, cid, o);
			break;
		case RT_GETTABLE:
			ok = handle_gettable_request(si, cid, o);
			break;
		default:
			error_reply(si, type|RT_ERROR, cid, "Unknown request type");
		}
end:
		if (ok < 0) {
			PS.invalid_requests++;
			si->PS.invalid_requests++;
		}
	}
	if (got) {
		msgpack_unpacker_expand_buffer(&c->unpacker, 0);
	}
}
Пример #11
0
/**
 * plume_recv_dispatch - Dispatch routine for Plume server messages.
 */
int
plume_recv_dispatch(struct plume_client *client, int status, void *data)
{
  ssize_t len;
  msgpack_unpacked result;
  msgpack_object *o, *p;
  int opcode;

  (void)status;
  (void)data;

  msgpack_unpacked_init(&result);

  // Reserve enough space in the msgpack buffer for a read.
  msgpack_unpacker_reserve_buffer(&client->pc_unpac, BUFSIZE);

  // Read up to BUFSIZE bytes into the stream.
  len = plume_recv(client, msgpack_unpacker_buffer(&client->pc_unpac), BUFSIZE);
  msgpack_unpacker_buffer_consumed(&client->pc_unpac, len);

  // Dispatch on as many msgpack objects as possible.
  while (msgpack_unpacker_next(&client->pc_unpac, &result)) {
    o = &result.data;

    // Discard malformed responses.
    if (o->type != MSGPACK_OBJECT_ARRAY ||
        o->via.array.size < 2 || o->via.array.size > 3) {
      log_error("Discarding malformed Plume server payload");
      continue;
    }
    p = o->via.array.ptr;

    // Pull out the opcode.
    if (p->type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
      log_error("Discarding malformed Plume server payload");
      continue;
    }
    opcode = (p++)->via.u64;

    // Dispatch.
    switch (opcode) {
      case PLUME_OP_IDENTIFY:
        plume_recv_identify(client, p->via.raw.ptr, p->via.raw.size, p + 1);
        break;
      case PLUME_OP_CERT:
        plume_recv_cert(client, p->via.raw.ptr, p->via.raw.size, p + 1);
        break;
      case PLUME_OP_CONNECT:
        plume_recv_connect(client, p->via.raw.ptr, p->via.raw.size, p + 1);
        break;
      case PLUME_OP_UDP_ACK:
        plume_recv_udp_ack(client, p);
      case PLUME_OP_UDP_ECHO:
        plume_recv_udp_echo(client, p);
        break;
      default:
        log_error("Discarding malformed Plume server payload");
        break;
    }
  }

  msgpack_unpacked_destroy(&result);
  return 0;
}
Пример #12
0
bool msgpack_unpacker_reserve_buffer_wrap(msgpack_unpacker *mpac, size_t size)
{
  return msgpack_unpacker_reserve_buffer(mpac, size);
}
void ANetworkController::TryReceiveData() {
	if (!TcpSocket) {
		return;
	}

	uint32 Size;
	if (TcpSocket->HasPendingData(Size)) {
		uint8 buffer[4096];
		int32 read;

		TcpSocket->Recv(buffer, 4096, read);

		msgpack_unpacker_reserve_buffer(&unpacker, read);
		memcpy(msgpack_unpacker_buffer(&unpacker), buffer, read);
		msgpack_unpacker_buffer_consumed(&unpacker, read);

		msgpack_unpacked result;
		msgpack_unpacked_init(&result);

		while (msgpack_unpacker_next(&unpacker, &result)) {
			msgpack_object* packet = &result.data;
			TMap<FName, msgpack_object*> Received = Unpack<TMap<FName, msgpack_object*>>(packet);

			uint8 PacketTypeId = Received[TYPE]->via.i64;
			EPacketType PacketType = (EPacketType)PacketTypeId;

			msgpack_object* Data = Received[DATA];

			switch (PacketType) {
			case EPacketType::PT_SetPaused: {
				bool Paused = Received[DATA]->via.boolean;
				ReceivedSetPaused(Paused);
				break;
			}
			case EPacketType::PT_GetBehaviors: {
				ReceivedGetBehaviors();
				break;
			}
			case EPacketType::PT_ConfigureSpawner: {
				TMap<FName, msgpack_object*> DataMap = Unpack<TMap<FName, msgpack_object*>>(Data);
				ReceivedConfigureSpawner(DataMap);
				break;
			}
			case EPacketType::PT_DisableAllSpawners: {
				ReceivedDisableAllSpawners();
				break;
			}
			case EPacketType::PT_ExecuteCommand: {
				ReceivedExecuteCommand(Data);
				break;
			}
			case EPacketType::PT_RemoveAllVehicles: {
				ReceivedRemoveAllVehicles();
				break;
			}
			case EPacketType::PT_ResetStats: {
				ReceivedResetStats();
				break;
			}
			case EPacketType::PT_RecreateLogEntry: {
				ReceivedRecreateLogEntry(Data);
				break;
			}
			case EPacketType::PT_AddArrow: {
				ReceivedAddArrow(Data);
				break;
			}
			case EPacketType::PT_StartRecording: {
				ReceivedStartRecording(Data);
				break;
			}
			case EPacketType::PT_StopRecording: {
				ReceivedStopRecording(Data);
				break;
			}
			case EPacketType::PT_StartPlayback: {
				ReceivedStartPlayback(Data);
				break;
			}
			case EPacketType::PT_StopPlayback: {
				ReceivedStopPlayback(Data);
				break;
			}
			case EPacketType::PT_GetGitHash: {
				ReceivedGetGitHash();
				break;
			}
			case EPacketType::PT_StartMatinee: {
				ReceivedStartMatinee(Data);
				break;
			}
			case EPacketType::PT_SetArrowsVisible:
				ReceivedSetArrowsVisible(Data);
				break;
			case EPacketType::PT_SeekAndPauseReplay:
				ReceivedSeekAndPauseReplay(Data);
				break;
			case EPacketType::PT_SetReplaySpeed:
				ReceivedSetReplaySpeed(Data);
				break;
			case EPacketType::PT_SetReplayPaused:
				ReceivedSetReplayPaused(Data);
				break;
			case EPacketType::PT_SelectCamera:
				ReceivedSelectCamera(Data);
				break;
			case EPacketType::PT_CameraLookAtVehicle:
				ReceivedCameraLookAtVehicle(Data);
				break;
			case EPacketType::PT_SelectVehicleCamera:
				ReceivedSelectVehicleCamera(Data);
				break;
			case EPacketType::PT_FollowCam:
				ReceivedFollowCam(Data);
				break;
			}
		}

		msgpack_unpacked_destroy(&result);
	}
}
Пример #14
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);
}