2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 18:45:48 +08:00
cmux/example_test.go
Tamir Duberstein 92a63c4fce 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-02-21 12:02:37 -05:00

121 lines
2.5 KiB
Go

package cmux_test
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/rpc"
"strings"
"google.golang.org/grpc"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
"github.com/soheilhy/cmux"
grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
)
type exampleHTTPHandler struct{}
func (h *exampleHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "example http response")
}
func serveHTTP(l net.Listener) {
s := &http.Server{
Handler: &exampleHTTPHandler{},
}
if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
}
func EchoServer(ws *websocket.Conn) {
if _, err := io.Copy(ws, ws); err != nil {
panic(err)
}
}
func serveWS(l net.Listener) {
s := &http.Server{
Handler: websocket.Handler(EchoServer),
}
if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
}
type ExampleRPCRcvr struct{}
func (r *ExampleRPCRcvr) Cube(i int, j *int) error {
*j = i * i
return nil
}
func serveRPC(l net.Listener) {
s := rpc.NewServer()
if err := s.Register(&ExampleRPCRcvr{}); err != nil {
panic(err)
}
for {
conn, err := l.Accept()
if err != nil {
if err != cmux.ErrListenerClosed {
panic(err)
}
return
}
go s.ServeConn(conn)
}
}
type grpcServer struct{}
func (s *grpcServer) SayHello(ctx context.Context, in *grpchello.HelloRequest) (
*grpchello.HelloReply, error) {
return &grpchello.HelloReply{Message: "Hello " + in.Name + " from cmux"}, nil
}
func serveGRPC(l net.Listener) {
grpcs := grpc.NewServer()
grpchello.RegisterGreeterServer(grpcs, &grpcServer{})
if err := grpcs.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
}
func Example() {
l, err := net.Listen("tcp", "127.0.0.1:50051")
if err != nil {
log.Panic(err)
}
m := cmux.New(l)
// We first match the connection against HTTP2 fields. If matched, the
// connection will be sent through the "grpcl" listener.
grpcl := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
//Otherwise, we match it againts a websocket upgrade request.
wsl := m.Match(cmux.HTTP1HeaderField("Upgrade", "websocket"))
// Otherwise, we match it againts HTTP1 methods. If matched,
// it is sent through the "httpl" listener.
httpl := m.Match(cmux.HTTP1Fast())
// If not matched by HTTP, we assume it is an RPC connection.
rpcl := m.Match(cmux.Any())
// Then we used the muxed listeners.
go serveGRPC(grpcl)
go serveWS(wsl)
go serveHTTP(httpl)
go serveRPC(rpcl)
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
}