tendermint/rpc/grpc/client_server.go

45 lines
1.0 KiB
Go
Raw Normal View History

2016-06-21 10:19:49 -07:00
package core_grpc
import (
"fmt"
"net"
"strings"
"time"
"google.golang.org/grpc"
2017-10-04 13:40:45 -07:00
cmn "github.com/tendermint/tmlibs/common"
2016-06-21 10:19:49 -07:00
)
// Start the grpcServer in a go routine
func StartGRPCServer(protoAddr string) (net.Listener, error) {
parts := strings.SplitN(protoAddr, "://", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid listen address for grpc server (did you forget a tcp:// prefix?) : %s", protoAddr)
}
proto, addr := parts[0], parts[1]
ln, err := net.Listen(proto, addr)
if err != nil {
return nil, err
}
grpcServer := grpc.NewServer()
RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
2017-10-03 15:49:20 -07:00
go grpcServer.Serve(ln) // nolint: errcheck
2016-06-21 10:19:49 -07:00
return ln, nil
}
// Start the client by dialing the server
func StartGRPCClient(protoAddr string) BroadcastAPIClient {
conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil {
panic(err)
}
return NewBroadcastAPIClient(conn)
}
func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
2017-10-04 13:40:45 -07:00
return cmn.Connect(addr)
2016-06-21 10:19:49 -07:00
}