Beispiel #1
0
struct buffer *wait_response(int cfd) {
    int rbytes = 0, rc, r, remain, resp_size;
    struct buffer *response;

    rc = wait_socket_data(cfd, 3000, CR_READ);
    if (rc <= 0) { // timeout or error
       logger(DEBUG, "wait response error, as %s!", strerror(errno));
        return NULL;
    }

    response = alloc_buffer(128);
    remain = get_buffer_cap(response);
    TIME_START();
    while (rbytes < 4) {
        r = read(cfd, get_buffer_data(response) + rbytes, remain);
        if (r <= 0) {
            if (r == -1 &&
                   (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) continue;
            if (r == 0) close(cfd);
            logger(DEBUG, "wait response error, as %s!", strerror(errno));
            goto err_cleanup;
        }
        rbytes += r;
        remain -= r;
        incr_buffer_used(response, r);
    }
    resp_size = read_int32_buffer(response) + 4; 
    need_expand(response, resp_size);
    while (rbytes < resp_size) {
        r = read(cfd, get_buffer_data(response) + rbytes, resp_size - rbytes);
        if (r <= 0) {
            if (r == -1 &&
                   (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) continue;
            if (r == 0) close(cfd);
            logger(DEBUG, "wait response error, as %s!", strerror(errno));
            goto err_cleanup;
        }
        rbytes += r;
        incr_buffer_used(response, r);
    }
    TIME_END();
    logger(DEBUG, "Total time cost %lldus in wait response", TIME_COST());
    return response;

err_cleanup:
    dealloc_buffer(response);
    return NULL;
}
Beispiel #2
0
int main(int argc, char* argv[])
{
	KDTree<Value, 2> kdtree = KDTree<Value, 2>::CreateKDTree(INSERT_NUM);

	std::vector<KDTree<Value, 2>::DataType> vData;
	for(uint32_t i=0; i<INSERT_NUM; ++i)
	{
		KDTree<Value, 2>::DataType item;
		item.Vector[0] = random() % 99;
		item.Vector[1] = random() % 99;
		item.Value.Uin = i;

		vData.push_back(item);
		//printf("new vector (%u, %u)\n", item.Vector[0], item.Vector[1]);
	}

	timeval t1, t2;
	gettimeofday(&t1, NULL);
	kdtree.Build(vData);
	gettimeofday(&t2, NULL);
	printf("const: %.03fs\n", TIME_COST(t1, t2));

	printf("Tree Dump:\n");
	kdtree.DumpTree();

	printf("\n");

	KDTree<Value, 2>::VectorType key;
	key[0] = (random() * time(NULL)) % 99;
	key[1] = (random() * time(NULL)) % 99;

	kdtree.Draw2DMap("map.jpg", key[0], key[1]);

#define SEARCH_COUNT	5

	uint32_t range = 0;

	KDTree<Value, 2>::DataType buffer[SEARCH_COUNT];
	int foundCount = kdtree.Nearest(key, buffer, SEARCH_COUNT);
	printf("nearest (%u, %u):\n", key[0], key[1]);
	for(int i=0; i<foundCount; ++i)
	{
		uint32_t distance = EuclideanDistance<uint32_t, KDVector<uint32_t, 2>, 2>::Distance(key, buffer[i].Vector);
		if(i == foundCount / 2)
			range = distance;
		printf("  %d:(%u, %u) distance:%u\n", i, buffer[i].Vector[0], buffer[i].Vector[1], distance);
	}

	printf("\n");

	foundCount = kdtree.Range(key, range, buffer, SEARCH_COUNT);
	printf("nearest (%u, %u) with range %u:\n", key[0], key[1], range);
	for(int i=0; i<foundCount; ++i)
	{
		uint32_t distance = EuclideanDistance<uint32_t, KDVector<uint32_t, 2>, 2>::Distance(key, buffer[i].Vector);
		printf("  %d:(%u, %u) distance:%u\n", i, buffer[i].Vector[0], buffer[i].Vector[1], distance);
	}

	printf("\n");

	kdtree.Delete();
	return 0;
}