-
Notifications
You must be signed in to change notification settings - Fork 12
/
mux_conn.go
48 lines (40 loc) · 1.21 KB
/
mux_conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mux
import (
"io"
"net"
io_ "github.com/searKing/golang/go/io"
)
// sniffConn wraps a net.Conn and provides transparent sniffing of connection data.
type sniffConn struct {
net.Conn
sniffer io_.ReadSniffer
}
func newMuxConn(c net.Conn) *sniffConn {
return &sniffConn{
Conn: c,
sniffer: io_.SniffReader(c),
}
}
// 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.
func (m *sniffConn) Read(p []byte) (int, error) {
return m.sniffer.Read(p)
}
func (m *sniffConn) startSniffing() io.Reader {
m.sniffer.Sniff(true)
return m.sniffer
}
func (m *sniffConn) doneSniffing() {
m.sniffer.Sniff(false)
}