2012-05-07 00:48:58 +08:00
|
|
|
|
//=====================================================================
|
|
|
|
|
//
|
|
|
|
|
// test.cpp - kcp <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//
|
|
|
|
|
// ˵<><CBB5><EFBFBD><EFBFBD>
|
|
|
|
|
// gcc test.cpp -o test -lstdc++
|
|
|
|
|
//
|
|
|
|
|
//=====================================================================
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
#include "ikcp.c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
LatencySimulator *vnet;
|
|
|
|
|
|
|
|
|
|
// ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD>磺ģ<E7A3BA>ⷢ<EFBFBD><E2B7A2>һ<EFBFBD><D2BB> udp<64><70>
|
|
|
|
|
int udp_output(const char *buf, int len, ikcpcb *kcp, void *user)
|
|
|
|
|
{
|
|
|
|
|
int id = (int)user;
|
|
|
|
|
vnet->send(id, buf, len);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void test(int mode)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>磺<EFBFBD><E7A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10%<25><>Rtt 60ms~125ms
|
|
|
|
|
vnet = new LatencySimulator(10, 60, 125);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD> kcp<63><70><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> conv<6E>ǻỰ<C7BB><E1BBB0><EFBFBD>ţ<EFBFBD>ͬһ<CDAC><D2BB><EFBFBD>Ự<EFBFBD><E1BBB0>Ҫ<EFBFBD><D2AA>ͬ
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> user<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD>ʶ
|
|
|
|
|
ikcpcb *kcp1 = ikcp_create(0x11223344, (void*)0);
|
|
|
|
|
ikcpcb *kcp2 = ikcp_create(0x11223344, (void*)1);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>kcp<63><70><EFBFBD>²<EFBFBD><C2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ udp_output<75><74>ģ<EFBFBD><C4A3>udp<64><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڴ<EFBFBD>С<EFBFBD><D0A1>ƽ<EFBFBD><C6BD><EFBFBD>ӳ<EFBFBD>200ms<6D><73>ÿ20ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD>Ϊ128
|
|
|
|
|
ikcp_wndsize(kcp1, 128, 128);
|
|
|
|
|
ikcp_wndsize(kcp2, 128, 128);
|
|
|
|
|
|
|
|
|
|
// <20>жϲ<D0B6><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
if (mode == 0) {
|
|
|
|
|
// Ĭ<><C4AC>ģʽ
|
|
|
|
|
ikcp_nodelay(kcp1, 0, 10, 0, 0);
|
|
|
|
|
ikcp_nodelay(kcp2, 0, 10, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
else if (mode == 1) {
|
|
|
|
|
// <20><>ͨģʽ<C4A3><CABD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ص<EFBFBD>
|
|
|
|
|
ikcp_nodelay(kcp1, 0, 10, 0, 1);
|
|
|
|
|
ikcp_nodelay(kcp2, 0, 10, 0, 1);
|
|
|
|
|
} else {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
// <20>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nodelay-<2D><><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD>ɳ<EFBFBD><C9B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> intervalΪ<6C>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӣ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 10ms
|
|
|
|
|
// <20><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD> resendΪ<64><CEAA><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD>ָ<EFBFBD>꣬<EFBFBD><EAA3AC><EFBFBD><EFBFBD>Ϊ2
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Ϊ<>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ
|
|
|
|
|
ikcp_nodelay(kcp1, 1, 10, 2, 1);
|
|
|
|
|
ikcp_nodelay(kcp2, 1, 10, 2, 1);
|
2016-06-03 20:28:21 +08:00
|
|
|
|
kcp1->rx_minrto = 10;
|
|
|
|
|
kcp1->fastresend = 1;
|
2012-05-07 00:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char buffer[2000];
|
|
|
|
|
int hr;
|
|
|
|
|
|
|
|
|
|
IUINT32 ts1 = iclock();
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
isleep(1);
|
|
|
|
|
current = iclock();
|
|
|
|
|
ikcp_update(kcp1, iclock());
|
|
|
|
|
ikcp_update(kcp2, iclock());
|
|
|
|
|
|
|
|
|
|
// ÿ<><C3BF> 20ms<6D><73>kcp1<70><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (; current >= slap; slap += 20) {
|
2016-06-03 20:28:21 +08:00
|
|
|
|
((IUINT32*)buffer)[0] = index++;
|
|
|
|
|
((IUINT32*)buffer)[1] = current;
|
2012-05-07 00:48:58 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
|
|
|
|
ikcp_send(kcp1, buffer, 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>磺<EFBFBD><E7A3BA><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>udp<64><70><EFBFBD><EFBFBD>p1->p2
|
|
|
|
|
while (1) {
|
|
|
|
|
hr = vnet->recv(1, buffer, 2000);
|
|
|
|
|
if (hr < 0) break;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> p2<70>յ<EFBFBD>udp<64><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>²<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>뵽kcp2
|
|
|
|
|
ikcp_input(kcp2, buffer, hr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>磺<EFBFBD><E7A3BA><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>udp<64><70><EFBFBD><EFBFBD>p2->p1
|
|
|
|
|
while (1) {
|
|
|
|
|
hr = vnet->recv(0, buffer, 2000);
|
|
|
|
|
if (hr < 0) break;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> p1<70>յ<EFBFBD>udp<64><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>²<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>뵽kcp1
|
|
|
|
|
ikcp_input(kcp1, buffer, hr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// kcp2<70><32><EFBFBD>յ<EFBFBD><D5B5>κΰ<CEBA><CEB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ػ<EFBFBD>ȥ
|
|
|
|
|
while (1) {
|
|
|
|
|
hr = ikcp_recv(kcp2, buffer, 10);
|
|
|
|
|
// û<><C3BB><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
|
|
if (hr < 0) break;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
|
|
|
|
|
ikcp_send(kcp2, buffer, hr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// kcp1<70>յ<EFBFBD>kcp2<70>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while (1) {
|
|
|
|
|
hr = ikcp_recv(kcp1, buffer, 10);
|
|
|
|
|
// û<><C3BB><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|
|
|
|
if (hr < 0) break;
|
|
|
|
|
IUINT32 sn = *(IUINT32*)(buffer + 0);
|
|
|
|
|
IUINT32 ts = *(IUINT32*)(buffer + 4);
|
|
|
|
|
IUINT32 rtt = current - ts;
|
|
|
|
|
|
|
|
|
|
if (sn != next) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
printf("ERROR sn %d<->%d\n", (int)count, (int)next);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next++;
|
|
|
|
|
sumrtt += rtt;
|
|
|
|
|
count++;
|
2014-12-29 00:43:25 +08:00
|
|
|
|
if (rtt > (IUINT32)maxrtt) maxrtt = rtt;
|
2012-05-07 00:48:58 +08:00
|
|
|
|
|
|
|
|
|
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" };
|
2014-12-29 00:43:25 +08:00
|
|
|
|
printf("%s mode result (%dms):\n", names[mode], (int)ts1);
|
2016-06-03 20:28:21 +08:00
|
|
|
|
printf("avgrtt=%d maxrtt=%d tx=%d\n", (int)(sumrtt / count), (int)maxrtt, (int)vnet->tx1);
|
2012-05-07 00:48:58 +08:00
|
|
|
|
printf("press enter to next ...\n");
|
|
|
|
|
char ch; scanf("%c", &ch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2016-06-03 20:33:41 +08:00
|
|
|
|
test(0); // Ĭ<><C4AC>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><DEBF><EFBFBD><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
test(1); // <20><>ͨģʽ<C4A3><CABD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ص<EFBFBD>
|
2012-05-07 00:48:58 +08:00
|
|
|
|
test(2); // <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ض<EFBFBD><D8B6><EFBFBD><F2BFAAA3>ҹر<D2B9><D8B1><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
default mode result (20917ms):
|
|
|
|
|
avgrtt=740 maxrtt=1507
|
|
|
|
|
|
|
|
|
|
normal mode result (20131ms):
|
|
|
|
|
avgrtt=156 maxrtt=571
|
|
|
|
|
|
|
|
|
|
fast mode result (20207ms):
|
|
|
|
|
avgrtt=138 maxrtt=392
|
|
|
|
|
*/
|
|
|
|
|
|