private void parse_ack(UInt32 sn) { if (KcpHelper._itimediff(sn, snd_una) < 0 || KcpHelper._itimediff(sn, snd_nxt) >= 0) { return; } var index = 0; foreach (var seg in snd_buf) { if (sn == seg.sn) { snd_buf = KcpHelper.append(KcpHelper.slice(snd_buf, 0, index), KcpHelper.slice(snd_buf, index + 1, snd_buf.Length)); break; } seg.fastack++; index++; } }
private void parse_una(UInt32 una) { var count = 0; foreach (var seg in snd_buf) { if (KcpHelper._itimediff(una, seg.sn) > 0) { count++; } else { break; } } if (0 < count) { snd_buf = KcpHelper.slice(snd_buf, count, snd_buf.Length); } }
// flush pending data private void flush() { var current_ = current; var buffer_ = buffer; var change = 0; var lost = 0; if (0 == updated) { return; } var seg = new Segment(0); seg.conv = conv; seg.cmd = IKCP_CMD_ACK; seg.wnd = (UInt32)wnd_unused(); seg.una = rcv_nxt; // flush acknowledges var count = acklist.Length / 2; var offset = 0; for (var i = 0; i < count; i++) { if (offset + IKCP_OVERHEAD > mtu) { output(buffer, offset); //Array.Clear(buffer, 0, offset); offset = 0; } ack_get(i, ref seg.sn, ref seg.ts); offset += seg.encode(buffer, offset); } acklist = new UInt32[0]; // probe window size (if remote window size equals zero) if (0 == rmt_wnd) { if (0 == probe_wait) { probe_wait = IKCP_PROBE_INIT; ts_probe = current + probe_wait; } else { if (KcpHelper._itimediff(current, ts_probe) >= 0) { if (probe_wait < IKCP_PROBE_INIT) { probe_wait = IKCP_PROBE_INIT; } probe_wait += probe_wait / 2; if (probe_wait > IKCP_PROBE_LIMIT) { probe_wait = IKCP_PROBE_LIMIT; } ts_probe = current + probe_wait; probe |= IKCP_ASK_SEND; } } } else { ts_probe = 0; probe_wait = 0; } // flush window probing commands if ((probe & IKCP_ASK_SEND) != 0) { seg.cmd = IKCP_CMD_WASK; if (offset + IKCP_OVERHEAD > (int)mtu) { output(buffer, offset); //Array.Clear(buffer, 0, offset); offset = 0; } offset += seg.encode(buffer, offset); } probe = 0; // calculate window size var cwnd_ = KcpHelper._imin_(snd_wnd, rmt_wnd); if (0 == nocwnd) { cwnd_ = KcpHelper._imin_(cwnd, cwnd_); } count = 0; for (var k = 0; k < snd_queue.Length; k++) { if (KcpHelper._itimediff(snd_nxt, snd_una + cwnd_) >= 0) { break; } var newseg = snd_queue[k]; newseg.conv = conv; newseg.cmd = IKCP_CMD_PUSH; newseg.wnd = seg.wnd; newseg.ts = current_; newseg.sn = snd_nxt; newseg.una = rcv_nxt; newseg.resendts = current_; newseg.rto = rx_rto; newseg.fastack = 0; newseg.xmit = 0; snd_buf = KcpHelper.append(snd_buf, newseg); snd_nxt++; count++; } if (0 < count) { this.snd_queue = KcpHelper.slice(this.snd_queue, count, this.snd_queue.Length); } // calculate resent var resent = (UInt32)fastresend; if (fastresend <= 0) { resent = 0xffffffff; } var rtomin = rx_rto >> 3; if (nodelay != 0) { rtomin = 0; } // flush data segments foreach (var segment in snd_buf) { var needsend = false; var debug = KcpHelper._itimediff(current_, segment.resendts); if (0 == segment.xmit) { needsend = true; segment.xmit++; segment.rto = rx_rto; segment.resendts = current_ + segment.rto + rtomin; } else if (KcpHelper._itimediff(current_, segment.resendts) >= 0) { needsend = true; segment.xmit++; xmit++; if (0 == nodelay) { segment.rto += rx_rto; } else { segment.rto += rx_rto / 2; } segment.resendts = current_ + segment.rto; lost = 1; } else if (segment.fastack >= resent) { needsend = true; segment.xmit++; segment.fastack = 0; segment.resendts = current_ + segment.rto; change++; } if (needsend) { segment.ts = current_; segment.wnd = seg.wnd; segment.una = rcv_nxt; var need = IKCP_OVERHEAD + segment.data.Length; if (offset + need > mtu) { output(buffer, offset); //Array.Clear(buffer, 0, offset); offset = 0; } offset += segment.encode(buffer, offset); if (segment.data.Length > 0) { Array.Copy(segment.data, 0, buffer, offset, segment.data.Length); offset += segment.data.Length; } if (segment.xmit >= dead_link) { this.state = 0; } } } // flash remain segments if (offset > 0) { output(buffer, offset); //Array.Clear(buffer, 0, offset); offset = 0; } // update ssthresh if (change != 0) { var inflight = snd_nxt - snd_una; ssthresh = inflight / 2; if (ssthresh < IKCP_THRESH_MIN) { ssthresh = IKCP_THRESH_MIN; } cwnd = ssthresh + resent; incr = cwnd * mss; } if (lost != 0) { ssthresh = cwnd / 2; if (ssthresh < IKCP_THRESH_MIN) { ssthresh = IKCP_THRESH_MIN; } cwnd = 1; incr = mss; } if (cwnd < 1) { cwnd = 1; incr = mss; } }
private void parse_data(Segment newseg) { var sn = newseg.sn; if (KcpHelper._itimediff(sn, rcv_nxt + rcv_wnd) >= 0 || KcpHelper._itimediff(sn, rcv_nxt) < 0) { return; } var n = rcv_buf.Length - 1; var after_idx = -1; var repeat = false; for (var i = n; i >= 0; i--) { var seg = rcv_buf[i]; if (seg.sn == sn) { repeat = true; break; } if (KcpHelper._itimediff(sn, seg.sn) > 0) { after_idx = i; break; } } if (!repeat) { if (after_idx == -1) { this.rcv_buf = KcpHelper.append(new Segment[1] { newseg }, this.rcv_buf); } else { this.rcv_buf = KcpHelper.append(KcpHelper.slice(this.rcv_buf, 0, after_idx + 1), KcpHelper.append(new Segment[1] { newseg }, KcpHelper.slice(this.rcv_buf, after_idx + 1, this.rcv_buf.Length))); } } // move available data from rcv_buf -> rcv_queue var count = 0; foreach (var seg in rcv_buf) { if (seg.sn == this.rcv_nxt && this.rcv_queue.Length < this.rcv_wnd) { this.rcv_queue = KcpHelper.append(this.rcv_queue, seg); this.rcv_nxt++; count++; } else { break; } } if (0 < count) { this.rcv_buf = KcpHelper.slice(this.rcv_buf, count, this.rcv_buf.Length); } }
// user/upper level recv: returns size, returns below zero for EAGAIN public int Recv(byte[] buffer) { if (0 == rcv_queue.Length) { return(-1); } var peekSize = PeekSize(); if (0 > peekSize) { return(-2); } if (peekSize > buffer.Length) { return(-3); } var fast_recover = false; if (rcv_queue.Length >= rcv_wnd) { fast_recover = true; } // merge fragment. var count = 0; var n = 0; foreach (var seg in rcv_queue) { Array.Copy(seg.data, 0, buffer, n, seg.data.Length); n += seg.data.Length; count++; if (0 == seg.frg) { break; } } if (0 < count) { this.rcv_queue = KcpHelper.slice(this.rcv_queue, count, this.rcv_queue.Length); } // move available data from rcv_buf -> rcv_queue count = 0; foreach (var seg in rcv_buf) { if (seg.sn == this.rcv_nxt && this.rcv_queue.Length < this.rcv_wnd) { this.rcv_queue = KcpHelper.append(this.rcv_queue, seg); this.rcv_nxt++; count++; } else { break; } } if (0 < count) { rcv_buf = KcpHelper.slice(rcv_buf, count, rcv_buf.Length); } // fast recover if (rcv_queue.Length < rcv_wnd && fast_recover) { this.probe |= IKCP_ASK_TELL; } return(n); }