mirror of
https://github.com/soheilhy/cmux.git
synced 2025-09-17 12:10:08 +08:00
Reduce the number of calls needed to (*MuxConn).Read
Also affects (*buffer).Read.
This commit is contained in:
25
cmux.go
25
cmux.go
@@ -174,13 +174,26 @@ func newMuxConn(c net.Conn) *MuxConn {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MuxConn) Read(b []byte) (n int, err error) {
|
||||
if n, err = m.buf.Read(b); err == nil {
|
||||
return
|
||||
// From the io.Reader documentation:
|
||||
//
|
||||
// When Read encounters an error or end-of-file condition after
|
||||
// successfully reading n > 0 bytes, it returns the number of
|
||||
// bytes read. It may return the (non-nil) error from the same call
|
||||
// or return the error (and n == 0) from a subsequent call.
|
||||
// An instance of this general case is that a Reader returning
|
||||
// a non-zero number of bytes at the end of the input stream may
|
||||
// return either err == EOF or err == nil. The next Read should
|
||||
// return 0, EOF.
|
||||
//
|
||||
// This function implements the latter behaviour, returning the
|
||||
// (non-nil) error from the same call.
|
||||
func (m *MuxConn) Read(b []byte) (int, error) {
|
||||
n1, err := m.buf.Read(b)
|
||||
if n1 == len(b) || err != io.EOF {
|
||||
return n1, err
|
||||
}
|
||||
|
||||
n, err = m.Conn.Read(b)
|
||||
return
|
||||
n2, err := m.Conn.Read(b[n1:])
|
||||
return n1 + n2, err
|
||||
}
|
||||
|
||||
func (m *MuxConn) sniffer() io.Reader {
|
||||
|
Reference in New Issue
Block a user