/
transport.go
95 lines (79 loc) · 1.99 KB
/
transport.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package unigrpc
import (
"sync"
"github.com/centrifugal/centrifugo/v4/internal/unigrpc/unistream"
"github.com/centrifugal/centrifuge"
)
// grpcTransport wraps a stream.
type grpcTransport struct {
mu sync.RWMutex
stream unistream.CentrifugoUniStream_ConsumeServer
closed bool
closeCh chan struct{}
streamDataCh chan rawFrame
protoVersion centrifuge.ProtocolVersion
}
func newGRPCTransport(stream unistream.CentrifugoUniStream_ConsumeServer, streamDataCh chan rawFrame, protoVersion centrifuge.ProtocolVersion) *grpcTransport {
return &grpcTransport{
stream: stream,
streamDataCh: streamDataCh,
closeCh: make(chan struct{}),
protoVersion: protoVersion,
}
}
const transportName = "uni_grpc"
func (t *grpcTransport) Name() string {
return transportName
}
func (t *grpcTransport) Protocol() centrifuge.ProtocolType {
return centrifuge.ProtocolTypeProtobuf
}
// ProtocolVersion returns transport protocol version.
func (t *grpcTransport) ProtocolVersion() centrifuge.ProtocolVersion {
return t.protoVersion
}
// Unidirectional returns whether transport is unidirectional.
func (t *grpcTransport) Unidirectional() bool {
return true
}
// DisabledPushFlags ...
func (t *grpcTransport) DisabledPushFlags() uint64 {
return 0
}
// AppLevelPing ...
func (t *grpcTransport) AppLevelPing() centrifuge.AppLevelPing {
return centrifuge.AppLevelPing{
PingInterval: 0,
}
}
// Emulation ...
func (t *grpcTransport) Emulation() bool {
return false
}
func (t *grpcTransport) Write(message []byte) error {
return t.WriteMany(message)
}
func (t *grpcTransport) WriteMany(messages ...[]byte) error {
t.mu.RLock()
if t.closed {
t.mu.RUnlock()
return nil
}
t.mu.RUnlock()
for i := 0; i < len(messages); i++ {
err := t.stream.SendMsg(rawFrame(messages[i]))
if err != nil {
return err
}
}
return nil
}
func (t *grpcTransport) Close(_ centrifuge.Disconnect) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return nil
}
close(t.closeCh)
return nil
}