github.com/searKing/golang/go@v1.2.117/net/mux/mux_conn.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mux
     6  
     7  import (
     8  	"io"
     9  	"net"
    10  
    11  	io_ "github.com/searKing/golang/go/io"
    12  )
    13  
    14  // sniffConn wraps a net.Conn and provides transparent sniffing of connection data.
    15  type sniffConn struct {
    16  	net.Conn
    17  	sniffer io_.ReadSniffer
    18  }
    19  
    20  func newMuxConn(c net.Conn) *sniffConn {
    21  	return &sniffConn{
    22  		Conn:    c,
    23  		sniffer: io_.SniffReader(c),
    24  	}
    25  }
    26  
    27  // From the io.Reader documentation:
    28  //
    29  // When Read encounters an error or end-of-file condition after
    30  // successfully reading n > 0 bytes, it returns the number of
    31  // bytes read.  It may return the (non-nil) error from the same call
    32  // or return the error (and n == 0) from a subsequent call.
    33  // An instance of this general case is that a Reader returning
    34  // a non-zero number of bytes at the end of the input stream may
    35  // return either err == EOF or err == nil.  The next Read should
    36  // return 0, EOF.
    37  func (m *sniffConn) Read(p []byte) (int, error) {
    38  	return m.sniffer.Read(p)
    39  }
    40  
    41  func (m *sniffConn) startSniffing() io.Reader {
    42  	m.sniffer.Sniff(true)
    43  	return m.sniffer
    44  }
    45  
    46  func (m *sniffConn) doneSniffing() {
    47  	m.sniffer.Sniff(false)
    48  }