2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 11:41:52 +08:00
cmux/cmux.go

218 lines
4.7 KiB
Go
Raw Normal View History

2015-07-30 01:45:57 +08:00
package cmux
import (
"fmt"
"io"
"net"
"sync"
2015-07-30 01:45:57 +08:00
)
// Matcher matches a connection based on its content.
type Matcher func(io.Reader) bool
2015-07-30 01:45:57 +08:00
// ErrorHandler handles an error and returns whether
// the mux should continue serving the listener.
type ErrorHandler func(error) bool
2015-07-30 01:45:57 +08:00
var _ net.Error = ErrNotMatched{}
2015-07-30 01:45:57 +08:00
// ErrNotMatched is returned whenever a connection is not matched by any of
// the matchers registered in the multiplexer.
type ErrNotMatched struct {
c net.Conn
}
func (e ErrNotMatched) Error() string {
return fmt.Sprintf("mux: connection %v not matched by an matcher",
e.c.RemoteAddr())
}
// Temporary implements the net.Error interface.
2015-07-30 01:45:57 +08:00
func (e ErrNotMatched) Temporary() bool { return true }
// Timeout implements the net.Error interface.
func (e ErrNotMatched) Timeout() bool { return false }
2015-07-30 01:45:57 +08:00
type errListenerClosed string
func (e errListenerClosed) Error() string { return string(e) }
func (e errListenerClosed) Temporary() bool { return false }
func (e errListenerClosed) Timeout() bool { return false }
// ErrListenerClosed is returned from muxListener.Accept when the underlying
// listener is closed.
var ErrListenerClosed = errListenerClosed("mux: listener closed")
2015-07-30 01:45:57 +08:00
// New instantiates a new connection multiplexer.
func New(l net.Listener) CMux {
return &cMux{
root: l,
bufLen: 1024,
errh: func(_ error) bool { return true },
donec: make(chan struct{}),
2015-07-30 01:45:57 +08:00
}
}
// CMux is a multiplexer for network connections.
type CMux interface {
// Match returns a net.Listener that sees (i.e., accepts) only
// the connections matched by at least one of the matcher.
//
// The order used to call Match determines the priority of matchers.
Match(...Matcher) net.Listener
2015-07-30 01:45:57 +08:00
// Serve starts multiplexing the listener. Serve blocks and perhaps
// should be invoked concurrently within a go routine.
Serve() error
// HandleError registers an error handler that handles listener errors.
HandleError(ErrorHandler)
2015-07-30 01:45:57 +08:00
}
type matchersListener struct {
ss []Matcher
l muxListener
}
type cMux struct {
root net.Listener
bufLen int
errh ErrorHandler
donec chan struct{}
2015-07-30 01:45:57 +08:00
sls []matchersListener
}
func (m *cMux) Match(matchers ...Matcher) net.Listener {
2015-07-30 01:45:57 +08:00
ml := muxListener{
Listener: m.root,
connc: make(chan net.Conn, m.bufLen),
2015-07-30 01:45:57 +08:00
}
m.sls = append(m.sls, matchersListener{ss: matchers, l: ml})
return ml
}
func (m *cMux) Serve() error {
var wg sync.WaitGroup
2015-07-30 01:45:57 +08:00
defer func() {
close(m.donec)
wg.Wait()
2015-07-30 01:45:57 +08:00
for _, sl := range m.sls {
close(sl.l.connc)
// Drain the connections enqueued for the listener.
for c := range sl.l.connc {
_ = c.Close()
}
2015-07-30 01:45:57 +08:00
}
}()
for {
c, err := m.root.Accept()
if err != nil {
if !m.handleErr(err) {
return err
}
continue
}
wg.Add(1)
go m.serve(c, m.donec, &wg)
}
}
func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
muc := newMuxConn(c)
for _, sl := range m.sls {
for _, s := range sl.ss {
matched := s(muc.sniffer())
muc.reset()
if matched {
select {
case sl.l.connc <- muc:
case <-donec:
_ = c.Close()
}
return
2015-07-30 01:45:57 +08:00
}
}
}
2015-07-30 01:45:57 +08:00
errcheck go/src/github.com/soheilhy/cmux/cmux.go:127:13 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:134:9 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:137:15 m.root.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:43:9 s.Serve(l) go/src/github.com/soheilhy/cmux/cmux_test.go:52:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:72:12 s.Register(TestRPCRcvr{}) go/src/github.com/soheilhy/cmux/cmux_test.go:103:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:109:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:116:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:125:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:133:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:141:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:147:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:27:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_recursive_test.go:56:12 s.Register(&RecursiveRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_recursive_test.go:88:15 go tlsm.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:89:12 tcpm.Serve() go/src/github.com/soheilhy/cmux/example_test.go:30:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:34:9 io.Copy(ws, ws) go/src/github.com/soheilhy/cmux/example_test.go:41:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:53:12 s.Register(&ExampleRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_test.go:68:13 grpcs.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:97:9 m.Serve() go/src/github.com/soheilhy/cmux/example_tls_test.go:24:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_tls_test.go:69:9 m.Serve() go/src/github.com/soheilhy/cmux/matchers.go:151:14 hdec.Write(f.HeaderBlockFragment())
2016-01-07 23:15:03 +08:00
_ = c.Close()
err := ErrNotMatched{c: c}
if !m.handleErr(err) {
errcheck go/src/github.com/soheilhy/cmux/cmux.go:127:13 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:134:9 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:137:15 m.root.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:43:9 s.Serve(l) go/src/github.com/soheilhy/cmux/cmux_test.go:52:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:72:12 s.Register(TestRPCRcvr{}) go/src/github.com/soheilhy/cmux/cmux_test.go:103:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:109:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:116:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:125:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:133:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:141:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:147:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:27:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_recursive_test.go:56:12 s.Register(&RecursiveRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_recursive_test.go:88:15 go tlsm.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:89:12 tcpm.Serve() go/src/github.com/soheilhy/cmux/example_test.go:30:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:34:9 io.Copy(ws, ws) go/src/github.com/soheilhy/cmux/example_test.go:41:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:53:12 s.Register(&ExampleRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_test.go:68:13 grpcs.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:97:9 m.Serve() go/src/github.com/soheilhy/cmux/example_tls_test.go:24:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_tls_test.go:69:9 m.Serve() go/src/github.com/soheilhy/cmux/matchers.go:151:14 hdec.Write(f.HeaderBlockFragment())
2016-01-07 23:15:03 +08:00
_ = m.root.Close()
2015-07-30 01:45:57 +08:00
}
}
func (m *cMux) HandleError(h ErrorHandler) {
m.errh = h
}
func (m *cMux) handleErr(err error) bool {
if !m.errh(err) {
return false
}
if ne, ok := err.(net.Error); ok {
return ne.Temporary()
}
return false
}
type muxListener struct {
net.Listener
connc chan net.Conn
2015-07-30 01:45:57 +08:00
}
func (l muxListener) Accept() (net.Conn, error) {
c, ok := <-l.connc
if !ok {
2015-07-30 01:45:57 +08:00
return nil, ErrListenerClosed
}
return c, nil
2015-07-30 01:45:57 +08:00
}
// MuxConn wraps a net.Conn and provides transparent sniffing of connection data.
2015-07-30 01:45:57 +08:00
type MuxConn struct {
net.Conn
buf buffer
2015-07-30 01:45:57 +08:00
}
func newMuxConn(c net.Conn) *MuxConn {
return &MuxConn{
Conn: 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.
//
// 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
2015-07-30 01:45:57 +08:00
}
n2, err := m.Conn.Read(b[n1:])
return n1 + n2, err
2015-07-30 01:45:57 +08:00
}
func (m *MuxConn) sniffer() io.Reader {
return io.MultiReader(&m.buf, io.TeeReader(m.Conn, &m.buf))
2015-07-30 01:45:57 +08:00
}
func (m *MuxConn) reset() {
m.buf.resetRead()
2015-07-30 01:45:57 +08:00
}