-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubstream.go
195 lines (165 loc) · 4.02 KB
/
substream.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package tentacle
import (
"io"
"net"
"sync/atomic"
)
// ProtocolID define the protocol id
type ProtocolID uint
// StreamID define the substream id
type streamID uint
const (
subStreamOpen uint = iota
subStreamClose
subStreamMessage
subStreamSelectError
subStreamOtherError
subStreamTimeOutCheck
stateChange
)
// As a firm believer in the type system, this is the last stubborn stand against the Go type!
type protocolEvent struct {
tag uint
event interface{}
}
type subStreamOpenInner struct {
name string
version string
conn net.Conn
}
type subStreamMessageInner struct {
sID streamID
pID ProtocolID
data []byte
}
type subStreamCloseInner struct {
sID streamID
pID ProtocolID
}
type subStreamOtherErrorInner struct {
err error
pid ProtocolID
}
type subStream struct {
// Common
socket Codec
pID ProtocolID
sID streamID
context *SessionContext
dead atomic.Value
version string
// Output protocol event to session
eventSender chan<- protocolEvent
// Read protocol event and then send to socket
eventReceiver <-chan protocolEvent
// Read socket message and then send to handle
beforeReceive BeforeReceive
serviceProtoSender chan<- serviceProtocolEvent
sessionProtoSender chan<- sessionProtocolEvent
}
func (s *subStream) protoOpen() {
if s.serviceProtoSender != nil {
s.serviceProtoSender <- serviceProtocolEvent{tag: serviceProtocolConnected, event: serviceProtocolConnectedInner{context: s.context, version: s.version}}
}
if s.sessionProtoSender != nil {
s.sessionProtoSender <- sessionProtocolEvent{tag: sessionProtocolOpened, event: s.version}
}
}
func (s *subStream) runWrite() {
for event := range s.eventReceiver {
if s.dead.Load().(bool) || s.context.closed.Load().(bool) {
break
}
switch event.tag {
case subStreamMessage:
msg := event.event.(subStreamMessageInner)
err := s.socket.WriteMsg(msg.data)
if err != nil {
s.dead.Store(true)
s.errorClose(err)
break
}
case subStreamClose:
s.dead.Store(true)
s.closeStream()
break
}
}
}
func (s *subStream) runRead() {
for {
if s.context.closed.Load().(bool) {
s.closeStream()
return
}
readMsg, err := s.socket.ReadMsg()
if err != nil {
s.dead.Store(true)
switch err {
case io.EOF:
s.closeStream()
default:
s.errorClose(err)
}
break
}
s.sendToHandle(s.beforeReceive(readMsg))
}
}
func (s *subStream) NextMsg() (msg []byte, err error) {
if s.context.closed.Load().(bool) {
s.closeStream()
return nil, io.EOF
}
readMsg, err := s.socket.ReadMsg()
if err != nil {
s.dead.Store(true)
switch err {
case io.EOF:
s.closeStream()
default:
s.errorClose(err)
}
return nil, err
}
return s.beforeReceive(readMsg), nil
}
func (s *subStream) ProtocolID() ProtocolID {
return s.pID
}
func (s *subStream) Version() string {
return s.version
}
func (s *subStream) errorClose(err error) {
protectRun(func() {
s.eventSender <- protocolEvent{tag: subStreamOtherError, event: subStreamOtherErrorInner{err: err, pid: s.pID}}
}, nil)
s.closeStream()
}
func (s *subStream) closeStream() {
defer s.socket.Close()
if s.serviceProtoSender != nil {
s.serviceProtoSender <- serviceProtocolEvent{tag: serviceProtocolDisconnected, event: s.context.Sid}
}
if s.sessionProtoSender != nil {
s.sessionProtoSender <- sessionProtocolEvent{tag: sessionProtocolClosed}
if s.context.closed.Load().(bool) {
s.sessionProtoSender <- sessionProtocolEvent{tag: sessionProtocolDisconnected}
}
}
// if session close receiver first, here may panic, just ignore it
protectRun(
func() {
s.eventSender <- protocolEvent{tag: subStreamClose, event: subStreamCloseInner{sID: s.sID, pID: s.pID}}
},
nil,
)
}
func (s *subStream) sendToHandle(msg []byte) {
if s.serviceProtoSender != nil {
s.serviceProtoSender <- serviceProtocolEvent{tag: serviceProtocolReceived, event: serviceProtocolReceivedInner{id: s.context.Sid, data: msg}}
}
if s.sessionProtoSender != nil {
s.sessionProtoSender <- sessionProtocolEvent{tag: sessionProtocolReceived, event: msg}
}
}