refine rto and wnd, temporarily disable congestion control

pull/215/head
v2ray 2016-06-23 22:37:38 +02:00
parent 576b16c2f7
commit 4beeba933c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
1 changed files with 105 additions and 105 deletions

View File

@ -283,13 +283,10 @@ func (kcp *KCP) update_ack(rtt int32) {
kcp.rx_srtt = 1 kcp.rx_srtt = 1
} }
} }
rto = kcp.rx_srtt + _imax_(1, 4*kcp.rx_rttval) rto = kcp.rx_srtt + _imax_(kcp.interval, 4*kcp.rx_rttval)
if rto > IKCP_RTO_MAX { if rto > IKCP_RTO_MAX {
rto = IKCP_RTO_MAX rto = IKCP_RTO_MAX
} }
if rto < kcp.rx_minrto {
rto = kcp.rx_minrto
}
kcp.rx_rto = rto kcp.rx_rto = rto
} }
@ -403,7 +400,7 @@ func (kcp *KCP) parse_data(newseg *Segment) {
// Input when you received a low level packet (eg. UDP packet), call it // Input when you received a low level packet (eg. UDP packet), call it
func (kcp *KCP) Input(data []byte) int { func (kcp *KCP) Input(data []byte) int {
una := kcp.snd_una //una := kcp.snd_una
if len(data) < IKCP_OVERHEAD { if len(data) < IKCP_OVERHEAD {
return -1 return -1
} }
@ -440,7 +437,10 @@ func (kcp *KCP) Input(data []byte) int {
return -3 return -3
} }
kcp.rmt_wnd = uint32(wnd) if kcp.rmt_wnd < uint32(wnd) {
kcp.rmt_wnd = uint32(wnd)
}
//kcp.rmt_wnd = uint32(wnd)
kcp.parse_una(una) kcp.parse_una(una)
kcp.shrink_buf() kcp.shrink_buf()
@ -489,44 +489,38 @@ func (kcp *KCP) Input(data []byte) int {
kcp.parse_fastack(maxack) kcp.parse_fastack(maxack)
} }
if _itimediff(kcp.snd_una, una) > 0 { /*
if kcp.cwnd < kcp.rmt_wnd { if _itimediff(kcp.snd_una, una) > 0 {
mss := kcp.mss if kcp.cwnd < kcp.rmt_wnd {
if kcp.cwnd < kcp.ssthresh { mss := kcp.mss
kcp.cwnd++ if kcp.cwnd < kcp.ssthresh {
kcp.incr += mss
} else {
if kcp.incr < mss {
kcp.incr = mss
}
kcp.incr += (mss*mss)/kcp.incr + (mss / 16)
if (kcp.cwnd+1)*mss <= kcp.incr {
kcp.cwnd++ kcp.cwnd++
kcp.incr += mss
} else {
if kcp.incr < mss {
kcp.incr = mss
}
kcp.incr += (mss*mss)/kcp.incr + (mss / 16)
if (kcp.cwnd+1)*mss <= kcp.incr {
kcp.cwnd++
}
}
if kcp.cwnd > kcp.rmt_wnd {
kcp.cwnd = kcp.rmt_wnd
kcp.incr = kcp.rmt_wnd * mss
} }
} }
if kcp.cwnd > kcp.rmt_wnd { }*/
kcp.cwnd = kcp.rmt_wnd
kcp.incr = kcp.rmt_wnd * mss
}
}
}
return 0 return 0
} }
func (kcp *KCP) wnd_unused() int32 {
if len(kcp.rcv_queue) < int(kcp.rcv_wnd) {
return int32(int(kcp.rcv_wnd) - len(kcp.rcv_queue))
}
return 0
}
// flush pending data // flush pending data
func (kcp *KCP) flush() { func (kcp *KCP) flush() {
current := kcp.current current := kcp.current
buffer := kcp.buffer buffer := kcp.buffer
change := 0 change := 0
lost := false //lost := false
if kcp.updated == 0 { if kcp.updated == 0 {
return return
@ -534,7 +528,7 @@ func (kcp *KCP) flush() {
var seg Segment var seg Segment
seg.conv = kcp.conv seg.conv = kcp.conv
seg.cmd = IKCP_CMD_ACK seg.cmd = IKCP_CMD_ACK
seg.wnd = uint32(kcp.wnd_unused()) seg.wnd = uint32(kcp.rcv_nxt + kcp.rcv_wnd)
seg.una = kcp.rcv_nxt seg.una = kcp.rcv_nxt
// flush acknowledges // flush acknowledges
@ -552,61 +546,65 @@ func (kcp *KCP) flush() {
kcp.acklist = nil kcp.acklist = nil
// probe window size (if remote window size equals zero) // probe window size (if remote window size equals zero)
if kcp.rmt_wnd == 0 { /*
if kcp.probe_wait == 0 { if kcp.rmt_wnd == 0 {
kcp.probe_wait = IKCP_PROBE_INIT if kcp.probe_wait == 0 {
kcp.ts_probe = kcp.current + kcp.probe_wait kcp.probe_wait = IKCP_PROBE_INIT
} else {
if _itimediff(kcp.current, kcp.ts_probe) >= 0 {
if kcp.probe_wait < IKCP_PROBE_INIT {
kcp.probe_wait = IKCP_PROBE_INIT
}
kcp.probe_wait += kcp.probe_wait / 2
if kcp.probe_wait > IKCP_PROBE_LIMIT {
kcp.probe_wait = IKCP_PROBE_LIMIT
}
kcp.ts_probe = kcp.current + kcp.probe_wait kcp.ts_probe = kcp.current + kcp.probe_wait
kcp.probe |= IKCP_ASK_SEND } else {
if _itimediff(kcp.current, kcp.ts_probe) >= 0 {
if kcp.probe_wait < IKCP_PROBE_INIT {
kcp.probe_wait = IKCP_PROBE_INIT
}
kcp.probe_wait += kcp.probe_wait / 2
if kcp.probe_wait > IKCP_PROBE_LIMIT {
kcp.probe_wait = IKCP_PROBE_LIMIT
}
kcp.ts_probe = kcp.current + kcp.probe_wait
kcp.probe |= IKCP_ASK_SEND
}
} }
} } else {
} else { kcp.ts_probe = 0
kcp.ts_probe = 0 kcp.probe_wait = 0
kcp.probe_wait = 0 }*/
}
// flush window probing commands // flush window probing commands
if (kcp.probe & IKCP_ASK_SEND) != 0 { /*
seg.cmd = IKCP_CMD_WASK if (kcp.probe & IKCP_ASK_SEND) != 0 {
size := len(buffer) - len(ptr) seg.cmd = IKCP_CMD_WASK
if size+IKCP_OVERHEAD > int(kcp.mtu) { size := len(buffer) - len(ptr)
kcp.output(buffer[:size]) if size+IKCP_OVERHEAD > int(kcp.mtu) {
ptr = buffer kcp.output(buffer[:size])
} ptr = buffer
ptr = seg.encode(ptr) }
} ptr = seg.encode(ptr)
}*/
// flush window probing commands // flush window probing commands
if (kcp.probe & IKCP_ASK_TELL) != 0 { /*
seg.cmd = IKCP_CMD_WINS if (kcp.probe & IKCP_ASK_TELL) != 0 {
size := len(buffer) - len(ptr) seg.cmd = IKCP_CMD_WINS
if size+IKCP_OVERHEAD > int(kcp.mtu) { size := len(buffer) - len(ptr)
kcp.output(buffer[:size]) if size+IKCP_OVERHEAD > int(kcp.mtu) {
ptr = buffer kcp.output(buffer[:size])
ptr = buffer
}
ptr = seg.encode(ptr)
} }
ptr = seg.encode(ptr)
}
kcp.probe = 0 kcp.probe = 0*/
// calculate window size // calculate window size
cwnd := _imin_(kcp.snd_wnd, kcp.rmt_wnd)
cwnd := _imin_(kcp.snd_nxt+kcp.snd_wnd, kcp.rmt_wnd)
if kcp.congestionControl { if kcp.congestionControl {
cwnd = _imin_(kcp.cwnd, cwnd) cwnd = _imin_(kcp.cwnd, cwnd)
} }
count = 0 count = 0
for k := range kcp.snd_queue { for k := range kcp.snd_queue {
if _itimediff(kcp.snd_nxt, kcp.snd_una+cwnd) >= 0 { if _itimediff(kcp.snd_nxt, cwnd) >= 0 {
break break
} }
newseg := kcp.snd_queue[k] newseg := kcp.snd_queue[k]
@ -631,10 +629,10 @@ func (kcp *KCP) flush() {
if kcp.fastresend <= 0 { if kcp.fastresend <= 0 {
resent = 0xffffffff resent = 0xffffffff
} }
rtomin := (kcp.rx_rto >> 3) //rtomin := (kcp.rx_rto >> 3)
if kcp.nodelay != 0 { //if kcp.nodelay != 0 {
rtomin = 0 // rtomin = 0
} //}
// flush data segments // flush data segments
for _, segment := range kcp.snd_buf { for _, segment := range kcp.snd_buf {
@ -643,23 +641,23 @@ func (kcp *KCP) flush() {
needsend = true needsend = true
segment.xmit++ segment.xmit++
segment.rto = kcp.rx_rto segment.rto = kcp.rx_rto
segment.resendts = current + segment.rto + rtomin segment.resendts = current + segment.rto + kcp.interval
} else if _itimediff(current, segment.resendts) >= 0 { } else if _itimediff(current, segment.resendts) >= 0 {
needsend = true needsend = true
segment.xmit++ segment.xmit++
kcp.xmit++ kcp.xmit++
if kcp.nodelay == 0 { //if kcp.nodelay == 0 {
segment.rto += kcp.rx_rto segment.rto += kcp.rx_rto
} else { //} else {
segment.rto += kcp.rx_rto / 2 // segment.rto += kcp.rx_rto / 2
} //}
segment.resendts = current + segment.rto segment.resendts = current + segment.rto + kcp.interval
lost = true //lost = true
} else if segment.fastack >= resent { } else if segment.fastack >= resent {
needsend = true needsend = true
segment.xmit++ segment.xmit++
segment.fastack = 0 segment.fastack = 0
segment.resendts = current + segment.rto segment.resendts = current + segment.rto + kcp.interval
change++ change++
} }
@ -694,30 +692,32 @@ func (kcp *KCP) flush() {
// update ssthresh // update ssthresh
// rate halving, https://tools.ietf.org/html/rfc6937 // rate halving, https://tools.ietf.org/html/rfc6937
if change != 0 { /*
inflight := kcp.snd_nxt - kcp.snd_una if change != 0 {
kcp.ssthresh = inflight / 2 inflight := kcp.snd_nxt - kcp.snd_una
if kcp.ssthresh < IKCP_THRESH_MIN { kcp.ssthresh = inflight / 2
kcp.ssthresh = IKCP_THRESH_MIN if kcp.ssthresh < IKCP_THRESH_MIN {
} kcp.ssthresh = IKCP_THRESH_MIN
kcp.cwnd = kcp.ssthresh + resent }
kcp.incr = kcp.cwnd * kcp.mss kcp.cwnd = kcp.ssthresh + resent
} kcp.incr = kcp.cwnd * kcp.mss
}*/
// congestion control, https://tools.ietf.org/html/rfc5681 // congestion control, https://tools.ietf.org/html/rfc5681
if lost { /*
kcp.ssthresh = cwnd / 2 if lost {
if kcp.ssthresh < IKCP_THRESH_MIN { kcp.ssthresh = cwnd / 2
kcp.ssthresh = IKCP_THRESH_MIN if kcp.ssthresh < IKCP_THRESH_MIN {
kcp.ssthresh = IKCP_THRESH_MIN
}
kcp.cwnd = 1
kcp.incr = kcp.mss
} }
kcp.cwnd = 1
kcp.incr = kcp.mss
}
if kcp.cwnd < 1 { if kcp.cwnd < 1 {
kcp.cwnd = 1 kcp.cwnd = 1
kcp.incr = kcp.mss kcp.incr = kcp.mss
} }*/
} }
// Update updates state (call it repeatedly, every 10ms-100ms), or you can ask // Update updates state (call it repeatedly, every 10ms-100ms), or you can ask