forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.go
76 lines (61 loc) · 1.69 KB
/
grpc_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package server
import (
"context"
"net"
"google.golang.org/grpc"
"github.com/cometbft/cometbft/abci/types"
cmtnet "github.com/cometbft/cometbft/internal/net"
"github.com/cometbft/cometbft/libs/service"
)
type GRPCServer struct {
service.BaseService
proto string
addr string
listener net.Listener
server *grpc.Server
app types.Application
}
// NewGRPCServer returns a new gRPC ABCI server.
func NewGRPCServer(protoAddr string, app types.Application) service.Service {
proto, addr := cmtnet.ProtocolAndAddress(protoAddr)
s := &GRPCServer{
proto: proto,
addr: addr,
listener: nil,
app: app,
}
s.BaseService = *service.NewBaseService(nil, "ABCIServer", s)
return s
}
// OnStart starts the gRPC service.
func (s *GRPCServer) OnStart() error {
ln, err := net.Listen(s.proto, s.addr)
if err != nil {
return err
}
s.listener = ln
s.server = grpc.NewServer()
types.RegisterABCIServer(s.server, &gRPCApplication{s.app})
s.Logger.Info("Listening", "proto", s.proto, "addr", s.addr)
go func() {
if err := s.server.Serve(s.listener); err != nil {
s.Logger.Error("Error serving gRPC server", "err", err)
}
}()
return nil
}
// OnStop stops the gRPC server.
func (s *GRPCServer) OnStop() {
s.server.Stop()
}
// -------------------------------------------------------
// gRPCApplication is a gRPC shim for Application.
type gRPCApplication struct {
types.Application
}
func (*gRPCApplication) Echo(_ context.Context, req *types.EchoRequest) (*types.EchoResponse, error) {
return &types.EchoResponse{Message: req.Message}, nil
}
func (*gRPCApplication) Flush(context.Context, *types.FlushRequest) (*types.FlushResponse, error) {
return &types.FlushResponse{}, nil
}