예제 #1
0
파일: sess.cpp 프로젝트: ruleless/libkcp
void
UDPSession::Update(uint32_t current) noexcept {
    for (;;) {
        ssize_t n = recv(m_sockfd, m_buf, UDPSession::mtuLimit, 0);
        if (n > 0) {
            ikcp_input(m_kcp, static_cast<const char *>(m_buf), n);
        } else {
            break;
        }
    }
    ikcp_update(m_kcp, current);
}
예제 #2
0
파일: lua-kcp.c 프로젝트: korialuo/skynet
static int
lupdate(lua_State *l) {
    struct kcpctx *ctx = (struct kcpctx *)luaL_checkudata(l, 1, LKCP_MT);
    if (!ctx)
        return luaL_argerror(l, 1, "parameter self invalid.");
    assert(ctx->kcp);

    uint32_t tick = (uint32_t)luaL_checkinteger(l, 2);
    if (tick >= ctx->timeout) {
        ikcp_update(ctx->kcp, tick);
        ctx->timeout = ikcp_check(ctx->kcp, tick);
    }

    return 0;
}
예제 #3
0
파일: conn.cpp 프로젝트: JackWangCUMT/kcpuv
int Conn::run(uint64_t tick) {
	ikcp_update(_kcp, (uint32_t)tick);

	char* buf;
	uint32_t size;
	int r = recv_kcp(buf, size);
	if (r < 0)
		return r;

	kcpuv_msg_t msg;
	msg.conv = _conv;
	msg.data = (uint8_t*)buf;
	msg.size = size;
	_network->push_msg(msg);
	return 0;
}
예제 #4
0
파일: conn.c 프로젝트: susemm/ckcpuv
/** TODO: change msg to malloc */
int conn_run(conn_t * thiz, uint64_t tick)
{
	ikcp_update(thiz->_kcp, (uint32_t)tick);

	char* buf;
	uint32_t size;
	int r = conn_recv_kcp(thiz, &buf, &size);
	if (r < 0)
		return r;

	kcpuv_msg_t * msg = malloc(sizeof(kcpuv_msg_t));
	msg->conv = thiz->_conv;
	msg->data = (uint8_t*)buf;
	msg->size = size;
	network_push_msg(thiz->_network, msg);
	return 0;
}
예제 #5
0
int main()
{
	client_sock = make_sock("127.0.0.1");
	ikcpcb *kcp = ikcp_create(0x11223344, (void*)0);
	kcp->output = udp_output;
	ikcp_wndsize(kcp, 128, 128);
	ikcp_nodelay(kcp, 0, 10, 0, 0);

	char buf[ECHO_LEN];
	char *buf_in;
	char *msg = "hello";
	int i=0;
	int current;
	int ret;
	for(;;)
	{
		++i;
		isleep(1);
		current = iclock();
		ikcp_update(kcp, current);

		if (i % 1000 == 0)
		{
			sprintf(buf, "%s:%d, %u", msg, i, current);
			ret = ikcp_send(kcp, buf, strlen(buf));
			check(ret >= 0, "send");
			printf("send [%s]\n", buf);
		}

		ret = recv(client_sock, buf, ECHO_LEN-1, 0);
		check_silently(ret > 0);

		ikcp_input(kcp, buf, ret);
		printf("\nrecv from server raw: %s\n", buf);

		ret = ikcp_recv(kcp, buf, ECHO_LEN-1);
		check_silently(ret > 0);

		buf[ret] = '\0';
		printf("\nrecv from server: %s\n", buf);

error:
		continue;
	}
}
예제 #6
0
파일: etp.c 프로젝트: canmor-lam/libsg
/* for libuv */
static void on_uv_timer_cb(uv_timer_t * handle)
{
    sg_etp_session_t * session = handle->data;
    IUINT32 now = 0;

    /*LOG_D("update %d", client->conv);*/

    /* update ikcp */
    now = (IUINT32)uv_now(session->loop);
    sg_etp_update_speed((sg_etp_t *)session, now);
    if (now >= session->kcp_update_time)
    {
        ikcp_update(session->kcp, now);
        session->kcp_update_time = ikcp_check(session->kcp, now);

        LOG_D("update %lu @ %lu, timeout: %lu", session->conv, session->kcp_update_time, session->recv_data_time);

        /* check received data and add to work queue */
        //recv_data_check(session);
        if (ikcp_peeksize(session->kcp) > 0)
        {
            uv_idle_start(&(session->idle), on_uv_idle_cb);
        }
    }

    /* check if session is timeout */
    if (session->recv_data_time < now)
    {
        session->to_close = true; /* mark to close this session. */
        ikcp_flush(session->kcp);
        LOG_I("session %lu timeout, will be closed", session->conv);
    }

    /* check if should close this session */
    if (session->to_close)
    {
        sg_etp_session_close(session);
    }
}
예제 #7
0
void connection::update_kcp(uint32_t clock)
{
    ikcp_update(p_kcp_, clock);
}
예제 #8
0
파일: test.cpp 프로젝트: Mephostopilis/lua
// 测试用例
void test(int mode)
{
	// 创建模拟网络:丢包率10%,Rtt 60ms~125ms
	vnet = new LatencySimulator(10, 60, 125);

	// 创建两个端点的 kcp对象,第一个参数 conv是会话编号,同一个会话需要相同
	// 最后一个是 user参数,用来传递标识
	ikcpcb *kcp1 = ikcp_create(0x11223344, (void*)0);
	ikcpcb *kcp2 = ikcp_create(0x11223344, (void*)1);

	// 设置kcp的下层输出,这里为 udp_output,模拟udp网络输出函数
	kcp1->output = udp_output;
	kcp2->output = udp_output;

	IUINT32 current = iclock();
	IUINT32 slap = current + 20;
	IUINT32 index = 0;
	IUINT32 next = 0;
	IINT64 sumrtt = 0;
	int count = 0;
	int maxrtt = 0;

	// 配置窗口大小:平均延迟200ms,每20ms发送一个包,
	// 而考虑到丢包重发,设置最大收发窗口为128
	ikcp_wndsize(kcp1, 128, 128);
	ikcp_wndsize(kcp2, 128, 128);

	// 判断测试用例的模式
	if (mode == 0) {
		// 默认模式
		ikcp_nodelay(kcp1, 0, 10, 0, 0);
		ikcp_nodelay(kcp2, 0, 10, 0, 0);
	}
	else if (mode == 1) {
		// 普通模式,关闭流控等
		ikcp_nodelay(kcp1, 0, 10, 0, 1);
		ikcp_nodelay(kcp2, 0, 10, 0, 1);
	}	else {
		// 启动快速模式
		// 第二个参数 nodelay-启用以后若干常规加速将启动
		// 第三个参数 interval为内部处理时钟,默认设置为 10ms
		// 第四个参数 resend为快速重传指标,设置为2
		// 第五个参数 为是否禁用常规流控,这里禁止
		ikcp_nodelay(kcp1, 1, 10, 2, 1);
		ikcp_nodelay(kcp2, 1, 10, 2, 1);
		kcp1->rx_minrto = 10;
		kcp1->fastresend = 1;
	}


	char buffer[2000];
	int hr;

	IUINT32 ts1 = iclock();

	while (1) {
		isleep(1);
		current = iclock();
		ikcp_update(kcp1, iclock());
		ikcp_update(kcp2, iclock());

		// 每隔 20ms,kcp1发送数据
		for (; current >= slap; slap += 20) {
			((IUINT32*)buffer)[0] = index++;
			((IUINT32*)buffer)[1] = current;

			// 发送上层协议包
			ikcp_send(kcp1, buffer, 8);
		}

		// 处理虚拟网络:检测是否有udp包从p1->p2
		while (1) {
			hr = vnet->recv(1, buffer, 2000);
			if (hr < 0) break;
			// 如果 p2收到udp,则作为下层协议输入到kcp2
			ikcp_input(kcp2, buffer, hr);
		}

		// 处理虚拟网络:检测是否有udp包从p2->p1
		while (1) {
			hr = vnet->recv(0, buffer, 2000);
			if (hr < 0) break;
			// 如果 p1收到udp,则作为下层协议输入到kcp1
			ikcp_input(kcp1, buffer, hr);
		}

		// kcp2接收到任何包都返回回去
		while (1) {
			hr = ikcp_recv(kcp2, buffer, 10);
			// 没有收到包就退出
			if (hr < 0) break;
			// 如果收到包就回射
			ikcp_send(kcp2, buffer, hr);
		}

		// kcp1收到kcp2的回射数据
		while (1) {
			hr = ikcp_recv(kcp1, buffer, 10);
			// 没有收到包就退出
			if (hr < 0) break;
			IUINT32 sn = *(IUINT32*)(buffer + 0);
			IUINT32 ts = *(IUINT32*)(buffer + 4);
			IUINT32 rtt = current - ts;
			
			if (sn != next) {
				// 如果收到的包不连续
				printf("ERROR sn %d<->%d\n", (int)count, (int)next);
				return;
			}

			next++;
			sumrtt += rtt;
			count++;
			if (rtt > (IUINT32)maxrtt) maxrtt = rtt;

			printf("[RECV] mode=%d sn=%d rtt=%d\n", mode, (int)sn, (int)rtt);
		}
		if (next > 1000) break;
	}

	ts1 = iclock() - ts1;

	ikcp_release(kcp1);
	ikcp_release(kcp2);

	const char *names[3] = { "default", "normal", "fast" };
	printf("%s mode result (%dms):\n", names[mode], (int)ts1);
	printf("avgrtt=%d maxrtt=%d tx=%d\n", (int)(sumrtt / count), (int)maxrtt, (int)vnet->tx1);
	printf("press enter to next ...\n");
	char ch; scanf("%c", &ch);
}