You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1019 B

  1. package core_grpc
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "time"
  7. "google.golang.org/grpc"
  8. . "github.com/tendermint/go-common"
  9. )
  10. // Start the grpcServer in a go routine
  11. func StartGRPCServer(protoAddr string) (net.Listener, error) {
  12. parts := strings.SplitN(protoAddr, "://", 2)
  13. if len(parts) != 2 {
  14. return nil, fmt.Errorf("Invalid listen address for grpc server (did you forget a tcp:// prefix?) : %s", protoAddr)
  15. }
  16. proto, addr := parts[0], parts[1]
  17. ln, err := net.Listen(proto, addr)
  18. if err != nil {
  19. return nil, err
  20. }
  21. grpcServer := grpc.NewServer()
  22. RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
  23. go grpcServer.Serve(ln)
  24. return ln, nil
  25. }
  26. // Start the client by dialing the server
  27. func StartGRPCClient(protoAddr string) BroadcastAPIClient {
  28. conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
  29. if err != nil {
  30. panic(err)
  31. }
  32. return NewBroadcastAPIClient(conn)
  33. }
  34. func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
  35. return Connect(addr)
  36. }