From ce66a98f9547a97f1f86c6d2232e30434a1f7e41 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 9 Oct 2020 19:53:42 +0800 Subject: [PATCH] lint --- .travis.yml | 2 +- kcp.go | 2 +- kcp_test.go | 2 +- sess.go | 34 ++++++++++++++++------------------ 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4003ab2..0e7c4ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) diff --git a/kcp.go b/kcp.go index 4c34e8f..0c6c304 100644 --- a/kcp.go +++ b/kcp.go @@ -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) diff --git a/kcp_test.go b/kcp_test.go index 435d6cc..49d55d5 100644 --- a/kcp_test.go +++ b/kcp_test.go @@ -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) diff --git a/sess.go b/sess.go index a7da98e..7af2a0e 100644 --- a/sess.go +++ b/sess.go @@ -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 {