2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-20 02:55:46 +08:00
cmux/example_recursive_test.go

110 lines
2.1 KiB
Go
Raw Permalink Normal View History

2015-08-02 00:07:30 +08:00
package cmux_test
import (
"crypto/rand"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/rpc"
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
"strings"
2015-08-02 00:07:30 +08:00
"github.com/soheilhy/cmux"
)
type recursiveHTTPHandler struct{}
func (h *recursiveHTTPHandler) ServeHTTP(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintf(w, "example http response")
}
func recursiveServeHTTP(l net.Listener) {
s := &http.Server{
Handler: &recursiveHTTPHandler{},
}
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
if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
2015-08-02 00:07:30 +08:00
}
func tlsListener(l net.Listener) net.Listener {
// Load certificates.
certificate, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
2016-02-21 23:46:12 +08:00
log.Panic(err)
2015-08-02 00:07:30 +08:00
}
config := &tls.Config{
Certificates: []tls.Certificate{certificate},
Rand: rand.Reader,
}
// Create TLS listener.
tlsl := tls.NewListener(l, config)
return tlsl
}
type RecursiveRPCRcvr struct{}
func (r *RecursiveRPCRcvr) Cube(i int, j *int) error {
*j = i * i
return nil
}
func recursiveServeRPC(l net.Listener) {
s := rpc.NewServer()
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
if err := s.Register(&RecursiveRPCRcvr{}); err != nil {
panic(err)
}
for {
conn, err := l.Accept()
if err != nil {
if err != cmux.ErrListenerClosed {
panic(err)
}
return
}
go s.ServeConn(conn)
}
2015-08-02 00:07:30 +08:00
}
// This is an example for serving HTTP, HTTPS, and GoRPC/TLS on the same port.
func Example_recursiveCmux() {
// Create the TCP listener.
l, err := net.Listen("tcp", "127.0.0.1:50051")
if err != nil {
2016-02-21 23:46:12 +08:00
log.Panic(err)
2015-08-02 00:07:30 +08:00
}
// Create a mux.
tcpm := cmux.New(l)
// We first match on HTTP 1.1 methods.
httpl := tcpm.Match(cmux.HTTP1Fast())
// If not matched, we assume that its TLS.
tlsl := tcpm.Match(cmux.Any())
tlsl = tlsListener(tlsl)
// Now, we build another mux recursively to match HTTPS and GoRPC.
// You can use the same trick for SSH.
tlsm := cmux.New(tlsl)
httpsl := tlsm.Match(cmux.HTTP1Fast())
gorpcl := tlsm.Match(cmux.Any())
go recursiveServeHTTP(httpl)
go recursiveServeHTTP(httpsl)
go recursiveServeRPC(gorpcl)
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
go func() {
if err := tlsm.Serve(); err != cmux.ErrListenerClosed {
panic(err)
}
}()
if err := tcpm.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
2015-08-02 00:07:30 +08:00
}