Пример #1
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);
}
Пример #2
0
void tmate_decoder_get_buffer(struct tmate_decoder *decoder,
			      char **buf, size_t *len)
{
	/* rewind the buffer if possible */
	if (msgpack_unpacker_buffer_capacity(&decoder->unpacker) <
						TMATE_MAX_MESSAGE_SIZE) {
		msgpack_unpacker_expand_buffer(&decoder->unpacker, 0);
	}

	*buf = msgpack_unpacker_buffer(&decoder->unpacker);
	*len = msgpack_unpacker_buffer_capacity(&decoder->unpacker);
}
Пример #3
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);
	}
}