This commit is contained in:
xtaci
2020-10-09 19:53:42 +08:00
parent ad7383d8a3
commit ce66a98f95
4 changed files with 19 additions and 21 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ install:
- go get github.com/xtaci/kcp-go
script:
- go test -coverprofile=coverage.txt -covermode=atomic -bench .
- go test -coverprofile=coverage.txt -covermode=atomic -bench . -timeout 10m
after_success:
- bash <(curl -s https://codecov.io/bash)
+1 -1
View File
@@ -35,7 +35,7 @@ const (
var refTime time.Time = time.Now()
// currentMs returns current elapsed monotonic milliseconds since program startup
func currentMs() uint32 { return uint32(time.Now().Sub(refTime) / time.Millisecond) }
func currentMs() uint32 { return uint32(time.Since(refTime) / time.Millisecond) }
// output_callback is a prototype which ought capture conn and call conn.Write
type output_callback func(buf []byte, size int)
+1 -1
View File
@@ -104,7 +104,7 @@ func testlink(t *testing.T, client *lossyconn.LossyConn, server *lossyconn.Lossy
start := time.Now()
s.Write(buf)
io.ReadFull(s, buf)
rtt += time.Now().Sub(start)
rtt += time.Since(start)
}
t.Log("client:", client)
+16 -18
View File
@@ -237,7 +237,7 @@ func (s *UDPSession) Read(b []byte) (n int, err error) {
return 0, errors.WithStack(errTimeout)
}
delay := s.rd.Sub(time.Now())
delay := time.Until(s.rd)
timeout = time.NewTimer(delay)
c = timeout.C
}
@@ -308,7 +308,7 @@ func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {
s.mu.Unlock()
return 0, errors.WithStack(errTimeout)
}
delay := s.wd.Sub(time.Now())
delay := time.Until(s.wd)
timeout = time.NewTimer(delay)
c = timeout.C
}
@@ -340,7 +340,6 @@ func (s *UDPSession) uncork() {
}
s.txqueue = s.txqueue[:0]
}
return
}
// Close closes the connection.
@@ -652,22 +651,22 @@ func (s *UDPSession) notifyWriteError(err error) {
// packet input stage
func (s *UDPSession) packetInput(data []byte) {
dataValid := false
decrypted := false
if s.block != nil && len(data) >= cryptHeaderSize {
s.block.Decrypt(data, data)
data = data[nonceSize:]
checksum := crc32.ChecksumIEEE(data[crcSize:])
if checksum == binary.LittleEndian.Uint32(data) {
data = data[crcSize:]
dataValid = true
decrypted = true
} else {
atomic.AddUint64(&DefaultSnmp.InCsumErrors, 1)
}
} else if s.block == nil {
dataValid = true
decrypted = true
}
if dataValid && len(data) >= IKCP_OVERHEAD {
if decrypted && len(data) >= IKCP_OVERHEAD {
s.kcpInput(data)
}
}
@@ -769,7 +768,6 @@ type (
block BlockCrypt // block encryption
dataShards int // FEC data shard
parityShards int // FEC parity shard
fecDecoder *fecDecoder // FEC mock initialization
conn net.PacketConn // the underlying packet connection
ownConn bool // true if we created conn internally, false if provided by caller
@@ -792,43 +790,43 @@ type (
// packet input stage
func (l *Listener) packetInput(data []byte, addr net.Addr) {
dataValid := false
decrypted := false
if l.block != nil && len(data) >= cryptHeaderSize {
l.block.Decrypt(data, data)
data = data[nonceSize:]
checksum := crc32.ChecksumIEEE(data[crcSize:])
if checksum == binary.LittleEndian.Uint32(data) {
data = data[crcSize:]
dataValid = true
decrypted = true
} else {
atomic.AddUint64(&DefaultSnmp.InCsumErrors, 1)
}
} else if l.block == nil {
dataValid = true
decrypted = true
}
if dataValid && len(data) >= IKCP_OVERHEAD {
if decrypted && len(data) >= IKCP_OVERHEAD {
l.sessionLock.Lock()
s, ok := l.sessions[addr.String()]
l.sessionLock.Unlock()
var conv, sn uint32
convValid := false
convRecovered := false
fecFlag := binary.LittleEndian.Uint16(data[4:])
if fecFlag == typeData || fecFlag == typeParity { // 16bit kcp cmd [81-84] and frg [0-255] will not overlap with FEC type 0x00f1 0x00f2
if fecFlag == typeData && len(data) >= fecHeaderSizePlus2+IKCP_OVERHEAD {
conv = binary.LittleEndian.Uint32(data[fecHeaderSizePlus2:])
sn = binary.LittleEndian.Uint32(data[fecHeaderSizePlus2+IKCP_SN_OFFSET:])
convValid = true
convRecovered = true
}
} else {
conv = binary.LittleEndian.Uint32(data)
sn = binary.LittleEndian.Uint32(data[IKCP_SN_OFFSET:])
convValid = true
convRecovered = true
}
if ok { // existing connection
if !convValid || conv == s.kcp.conv { // parity or valid data shard
if !convRecovered || conv == s.kcp.conv { // parity data or valid conversation
s.kcpInput(data)
} else if sn == 0 { // should replace current connection
s.Close()
@@ -836,7 +834,7 @@ func (l *Listener) packetInput(data []byte, addr net.Addr) {
}
}
if s == nil && convValid { // new session
if s == nil && convRecovered { // new session
if len(l.chAccepts) < cap(l.chAccepts) { // do not let the new sessions overwhelm accept queue
s := newUDPSession(conv, l.dataShards, l.parityShards, l, l.conn, false, addr, l.block)
s.kcpInput(data)
@@ -914,7 +912,7 @@ func (l *Listener) Accept() (net.Conn, error) {
func (l *Listener) AcceptKCP() (*UDPSession, error) {
var timeout <-chan time.Time
if tdeadline, ok := l.rd.Load().(time.Time); ok && !tdeadline.IsZero() {
timeout = time.After(tdeadline.Sub(time.Now()))
timeout = time.After(time.Until(tdeadline))
}
select {