-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
45 lines (37 loc) · 1003 Bytes
/
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
package libp2pgrpc
import (
"context"
gostream "github.com/libp2p/go-libp2p-gostream"
"github.com/libp2p/go-libp2p/core/host"
"google.golang.org/grpc"
)
var _ grpc.ServiceRegistrar = &Server{}
// only for unit test
var _gostream_Listen = gostream.Listen
type Server struct {
host host.Host
grpc *grpc.Server
ctx context.Context
}
// NewGrpcServer creates a Server object with the given LibP2P host
// and protocol.
func NewGrpcServer(ctx context.Context, h host.Host, opts ...grpc.ServerOption) (*Server, error) {
grpcServer := grpc.NewServer(opts...)
srv := &Server{
host: h,
ctx: ctx,
grpc: grpcServer,
}
return srv, nil
}
// Serve start gRPC serve after NewGrpcServer() and services register
func (s *Server) Serve() error {
listener, err := _gostream_Listen(s.host, ProtocolID)
if err != nil {
return err
}
return s.grpc.Serve(listener)
}
func (s *Server) RegisterService(serviceDesc *grpc.ServiceDesc, srv interface{}) {
s.grpc.RegisterService(serviceDesc, srv)
}