【优化】1.优化buffer大小 2.优化interval 3. 增加wait_rcv api

This commit is contained in:
yangchaojun 2020-09-10 11:40:28 +08:00
parent bf616efb43
commit 3eee3a5825
2 changed files with 13 additions and 3 deletions

13
ikcp.c
View File

@ -254,7 +254,7 @@ ikcpcb* ikcp_create(IUINT32 conv, void *user)
kcp->mss = kcp->mtu - IKCP_OVERHEAD;
kcp->stream = 0;
kcp->buffer = (char*)ikcp_malloc((kcp->mtu + IKCP_OVERHEAD) * 3);
kcp->buffer = (char*)ikcp_malloc(kcp->mtu);
if (kcp->buffer == NULL) {
ikcp_free(kcp);
return NULL;
@ -1213,7 +1213,9 @@ IUINT32 ikcp_check(const ikcpcb *kcp, IUINT32 current)
}
minimal = (IUINT32)(tm_packet < tm_flush ? tm_packet : tm_flush);
if (minimal >= kcp->interval) minimal = kcp->interval;
IUINT32 interval = (kcp->probe != 0 || kcp->nsnd_que > 0 || kcp->nrcv_que > 0 || kcp->nrcv_buf > 0)
? kcp->interval : 5000;
if (minimal >= interval) minimal = interval;
return current + minimal;
}
@ -1225,7 +1227,8 @@ int ikcp_setmtu(ikcpcb *kcp, int mtu)
char *buffer;
if (mtu < 50 || mtu < (int)IKCP_OVERHEAD)
return -1;
buffer = (char*)ikcp_malloc((mtu + IKCP_OVERHEAD) * 3);
if(mtu == kcp->mtu) return 0;
buffer = (char*)ikcp_malloc(mtu);
if (buffer == NULL)
return -2;
kcp->mtu = mtu;
@ -1287,6 +1290,10 @@ int ikcp_waitsnd(const ikcpcb *kcp)
return kcp->nsnd_buf + kcp->nsnd_que;
}
int ikcp_waitrcv(const ikcpcb *kcp)
{
return kcp->nrcv_buf + kcp->nrcv_que;
}
// read conv
IUINT32 ikcp_getconv(const void *ptr)

3
ikcp.h
View File

@ -390,6 +390,9 @@ int ikcp_wndsize(ikcpcb *kcp, int sndwnd, int rcvwnd);
// get how many packet is waiting to be sent
int ikcp_waitsnd(const ikcpcb *kcp);
// get how many packet is waiting to be receive
int ikcp_waitrcv(const ikcpcb *kcp);
// fastest: ikcp_nodelay(kcp, 1, 20, 2, 1)
// nodelay: 0:disable(default), 1:enable
// interval: internal update timer interval in millisec, default is 100ms