2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-14 11:31:28 +08:00
cmux/example_test.go

99 lines
2.0 KiB
Go
Raw Normal View History

2015-07-30 01:45:57 +08:00
package cmux_test
import (
"fmt"
"io"
2015-07-30 01:45:57 +08:00
"log"
"net"
"net/http"
"net/rpc"
"google.golang.org/grpc"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
2015-07-30 01:45:57 +08:00
"github.com/soheilhy/cmux"
grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
2015-07-30 01:45:57 +08:00
)
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{},
}
s.Serve(l)
}
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func serveWS(l net.Listener) {
s := &http.Server{
Handler: websocket.Handler(EchoServer),
}
s.Serve(l)
}
2015-07-30 01:45:57 +08:00
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()
s.Register(&ExampleRPCRcvr{})
s.Accept(l)
}
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{})
grpcs.Serve(l)
}
func Example() {
l, err := net.Listen("tcp", "127.0.0.1:50051")
if err != nil {
log.Fatal(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())
2015-07-30 01:45:57 +08:00
// 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)
2015-07-30 01:45:57 +08:00
go serveHTTP(httpl)
go serveRPC(rpcl)
m.Serve()
}