forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
50 lines (37 loc) · 932 Bytes
/
client.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
package bertyprotocol
import (
"context"
"berty.tech/berty/v2/go/internal/grpcutil"
"google.golang.org/grpc"
)
type Client interface {
ProtocolServiceClient
Close() error
}
type client struct {
ProtocolServiceClient
l *grpcutil.PipeListener
}
func (c *client) Close() error {
return c.l.Close()
}
func NewClient(svc Service, opts ...grpc.ServerOption) (Client, error) {
return NewClientFromServer(grpc.NewServer(opts...), svc)
}
func NewClientFromServer(s *grpc.Server, svc Service, opts ...grpc.DialOption) (Client, error) {
pl := grpcutil.NewPipeListener()
opts = append(opts, grpc.WithInsecure())
cc, err := pl.NewClientConn(opts...)
if err != nil {
return nil, err
}
RegisterProtocolServiceServer(s, svc)
go func() {
err := s.Serve(pl)
if err != nil && err != context.Canceled {
panic(err)
}
}()
c := client{ProtocolServiceClient: NewProtocolServiceClient(cc), l: pl}
return &c, nil
}