github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/rpc/grpc/client_server.go (about)

     1  package coregrpc
     2  
     3  import (
     4  	"net"
     5  
     6  	"golang.org/x/net/context"
     7  	"google.golang.org/grpc"
     8  	"google.golang.org/grpc/credentials/insecure"
     9  
    10  	cmtnet "github.com/badrootd/celestia-core/libs/net"
    11  )
    12  
    13  // Config is an gRPC server configuration.
    14  type Config struct {
    15  	MaxOpenConnections int
    16  }
    17  
    18  // StartGRPCServer starts a new gRPC BroadcastAPIServer using the given
    19  // net.Listener.
    20  // NOTE: This function blocks - you may want to call it in a go-routine.
    21  func StartGRPCServer(ln net.Listener) error {
    22  	grpcServer := grpc.NewServer()
    23  	RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
    24  	return grpcServer.Serve(ln)
    25  }
    26  
    27  // StartGRPCClient dials the gRPC server using protoAddr and returns a new
    28  // BroadcastAPIClient.
    29  func StartGRPCClient(protoAddr string) BroadcastAPIClient {
    30  	conn, err := grpc.Dial(protoAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialerFunc))
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	return NewBroadcastAPIClient(conn)
    35  }
    36  
    37  func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
    38  	return cmtnet.Connect(addr)
    39  }