mirror of
https://github.com/soheilhy/cmux.git
synced 2024-11-10 03:31:52 +08:00
Require two Read calls to get EOF
This is maybe slightly less efficient, but it is easier to reason about, and seems to be the style in which most of the standard library is written. For instance, bytes.Buffer behaves this way.
This commit is contained in:
parent
36dc9894b6
commit
69ab4c207f
14
cmux.go
14
cmux.go
@ -197,19 +197,11 @@ func newMuxConn(c net.Conn) *MuxConn {
|
||||
// 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(p []byte) (int, error) {
|
||||
n1, err := m.buf.Read(p)
|
||||
if err == nil && m.buf.Len() == 0 {
|
||||
err = io.EOF
|
||||
if n, err := m.buf.Read(p); err != io.EOF {
|
||||
return n, err
|
||||
}
|
||||
if n1 == len(p) || err != io.EOF {
|
||||
return n1, err
|
||||
}
|
||||
n2, err := m.Conn.Read(p[n1:])
|
||||
return n1 + n2, err
|
||||
return m.Conn.Read(p)
|
||||
}
|
||||
|
||||
func (m *MuxConn) getSniffer() io.Reader {
|
||||
|
10
cmux_test.go
10
cmux_test.go
@ -278,14 +278,22 @@ func TestHTTP2(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
{
|
||||
var b [len(http2.ClientPreface)]byte
|
||||
if _, err := muxedConn.Read(b[:]); err != io.EOF {
|
||||
if _, err := muxedConn.Read(b[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(b[:]) != http2.ClientPreface {
|
||||
t.Errorf("got unexpected read %s, expected %s", b, http2.ClientPreface)
|
||||
}
|
||||
}
|
||||
{
|
||||
var b [1]byte
|
||||
if _, err := muxedConn.Read(b[:]); err != io.EOF {
|
||||
t.Errorf("unexpected error %v, expected %v", err, io.EOF)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPGoRPC(t *testing.T) {
|
||||
defer leakCheck(t)()
|
||||
|
Loading…
Reference in New Issue
Block a user