-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
385 lines (327 loc) · 9.93 KB
/
builder.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package tentacle
import (
"io"
"net"
"sync/atomic"
"time"
"github.com/driftluo/tentacle-go/secio"
"github.com/hashicorp/yamux"
"github.com/libp2p/go-msgio"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
// Codec use on protocol stream to en/decode message
type Codec interface {
// ReadMsg reads the next message from the Reader
ReadMsg() ([]byte, error)
// WriteMsg writes the msg in the passed
WriteMsg([]byte) error
io.Closer
}
// CodecFn generate a codec
type CodecFn func(net.Conn) Codec
// BeforeSend unified processing of messages before they are sent
type BeforeSend func([]byte) []byte
// BeforeReceive unified processing of messages before user received
type BeforeReceive func([]byte) []byte
// SessionProtocolFn generate SessionProtocol
type SessionProtocolFn func() SessionProtocol
// DefaultCodec use by default, is a LengthDelimitedCodec
var DefaultCodec = func(conn net.Conn) Codec {
return msgio.Combine(msgio.NewWriter(conn), msgio.NewReader(conn))
}
// DefaultBeforeSend use by default, do nothing
var DefaultBeforeSend = func(b []byte) []byte {
return b
}
// DefaultBeforeReceive use by default, do nothing
var DefaultBeforeReceive = func(b []byte) []byte {
return b
}
// MetaBuilder builder for protocol meta
type MetaBuilder struct {
id ProtocolID
name NameFn
supportVersions []string
codec CodecFn
serviceHandle ServiceProtocol
sessionHandle SessionProtocolFn
protoSpawn ProtocolSpawn
selectVersion SelectFn
beforeSend BeforeSend
beforeReceive BeforeReceive
}
// DefaultMeta make a default builder
func DefaultMeta() *MetaBuilder {
return &MetaBuilder{
id: ProtocolID(0),
name: DefaultNameFn,
supportVersions: []string{"0.0.1"},
codec: DefaultCodec,
serviceHandle: nil,
sessionHandle: nil,
protoSpawn: nil,
selectVersion: SelectVersion,
beforeSend: DefaultBeforeSend,
beforeReceive: DefaultBeforeReceive,
}
}
// ID define protocol id
//
// It is just an internal index of the system that
// identifies the open/close and message transfer for the specified protocol.
func (m *MetaBuilder) ID(pid ProtocolID) *MetaBuilder {
m.id = pid
return m
}
// Name define protocol name, default is "/p2p/protocol_id"
//
// Used to interact with the remote service to determine whether the protocol is supported.
//
// If not found, the protocol connection(not session just sub stream) will be closed,
// and return a `ProtocolSelectError` event.
func (m *MetaBuilder) Name(name NameFn) *MetaBuilder {
m.name = name
return m
}
// SupportVersions define protocol support versions, default is `[]string{"0.0.1"}`
//
// Used to interact with the remote service to confirm that both parties
// open the same version of the protocol.
//
// If not found, the protocol connection(not session just sub stream) will be closed,
// and return a `ProtocolSelectError` event.
func (m *MetaBuilder) SupportVersions(versions []string) *MetaBuilder {
m.supportVersions = versions
return m
}
// Codec define protocol codec, default is LengthDelimitedCodec
func (m *MetaBuilder) Codec(codec CodecFn) *MetaBuilder {
m.codec = codec
return m
}
// ServiceHandle define protocol service handle, default is nil
func (m *MetaBuilder) ServiceHandle(service ServiceProtocol) *MetaBuilder {
m.serviceHandle = service
return m
}
// SessionHandle define protocol session handle, default is nil
func (m *MetaBuilder) SessionHandle(sessionFn SessionProtocolFn) *MetaBuilder {
m.sessionHandle = sessionFn
return m
}
// ProtoSpawn define the spawn process of the protocol read part
//
// Mutually exclusive with protocol handle
func (m *MetaBuilder) ProtoSpawn(protoSpawn ProtocolSpawn) *MetaBuilder {
m.protoSpawn = protoSpawn
return m
}
// SelectVersion protocol version selection rule, default is `SelectVersion`
func (m *MetaBuilder) SelectVersion(selectfn SelectFn) *MetaBuilder {
m.selectVersion = selectfn
return m
}
// BeforeSend unified processing of messages before user received
func (m *MetaBuilder) BeforeSend(beforeSend BeforeSend) *MetaBuilder {
m.beforeSend = beforeSend
return m
}
// BeforeReceive unified processing of messages before user received
func (m *MetaBuilder) BeforeReceive(beforeRecv BeforeReceive) *MetaBuilder {
m.beforeReceive = beforeRecv
return m
}
// Build combine the configuration of this builder to create a ProtocolMeta
func (m *MetaBuilder) Build() ProtocolMeta {
if m.protoSpawn != nil && (m.serviceHandle != nil || m.sessionHandle != nil) {
panic("It is not allowed to use handle and spawn at the same time ")
}
return ProtocolMeta{
inner: &meta{
id: m.id,
name: m.name,
supportVersions: m.supportVersions,
codec: m.codec,
selectVersion: m.selectVersion,
beforeReceive: m.beforeReceive,
spawn: m.protoSpawn,
},
serviceHandle: m.serviceHandle,
sessionHandleFn: m.sessionHandle,
beforeSend: m.beforeSend,
}
}
// ServiceBuilder builder for Service
type ServiceBuilder struct {
inner map[ProtocolID]ProtocolMeta
keyPair secio.PrivKey
forever bool
config serviceConfig
}
// DefaultServiceBuilder create a default empty builder
func DefaultServiceBuilder() *ServiceBuilder {
return &ServiceBuilder{
inner: make(map[ProtocolID]ProtocolMeta),
keyPair: nil,
forever: false,
config: serviceConfig{
timeout: 10 * time.Second,
yamuxConfig: yamux.DefaultConfig(),
maxConnectionNumber: 65535,
channelSize: 128,
tcpBind: nil,
wsBind: nil,
},
}
}
// InsertProtocol insert a custom protocol
func (s *ServiceBuilder) InsertProtocol(protocol ProtocolMeta) *ServiceBuilder {
s.inner[protocol.inner.id] = protocol
return s
}
// KeyPair enable encrypted communication mode.
//
// If you do not need encrypted communication, you do not need to call this method
func (s *ServiceBuilder) KeyPair(key secio.PrivKey) *ServiceBuilder {
s.keyPair = key
return s
}
// Forever when the service has no tasks, it will be turned off by default.
// If you do not want to close service, set it to true.
func (s *ServiceBuilder) Forever(forever bool) *ServiceBuilder {
s.forever = forever
return s
}
// TimeOut for handshake and connect
// Default 10 second
func (s *ServiceBuilder) TimeOut(timeout time.Duration) *ServiceBuilder {
s.config.timeout = timeout
return s
}
// YamuxConfig for service
func (s *ServiceBuilder) YamuxConfig(config *yamux.Config) *ServiceBuilder {
s.config.yamuxConfig = config
return s
}
// MaxConnectionNumber the limit of max open connection(file descriptors)
// If not limited, service will try to serve as many connections as possible until it exhausts system resources(os error),
// and then close the listener, no longer accepting new connection requests, and the established connections remain working
//
// Default is 65535
func (s *ServiceBuilder) MaxConnectionNumber(num uint) *ServiceBuilder {
s.config.maxConnectionNumber = num
return s
}
// ChannelSize the size of each channel used on tentacle
//
// Default is 128
func (s *ServiceBuilder) ChannelSize(size uint) *ServiceBuilder {
s.config.channelSize = size
return s
}
// TCPBind use to bind all tcp session to listen port
func (s *ServiceBuilder) TCPBind(addr ma.Multiaddr) *ServiceBuilder {
netTy, host, err := manet.DialArgs(addr)
if err != nil {
return s
}
switch netTy {
case "tcp", "tcp4", "tcp6":
default:
return s
}
s.config.tcpBind = &host
return s
}
// WsBind use to bind all ws session to listen port
func (s *ServiceBuilder) WsBind(addr ma.Multiaddr) *ServiceBuilder {
netTy, host, err := manet.DialArgs(addr)
if err != nil {
return s
}
switch netTy {
case "tcp", "tcp4", "tcp6":
default:
return s
}
s.config.wsBind = &host
return s
}
// Build combine the configuration of this builder with service handle to create a Service.
func (s *ServiceBuilder) Build(handle ServiceHandle) *Service {
var state *serviceState
if s.forever {
state = &serviceState{tag: forever}
} else {
state = &serviceState{tag: running, workers: 0}
}
quickTask := make(chan serviceTask, s.config.channelSize)
task := make(chan serviceTask, s.config.channelSize)
sessionChan := make(chan sessionEvent, s.config.channelSize)
handleChan := make(chan interface{}, s.config.channelSize)
shutdown := atomic.Value{}
shutdown.Store(false)
serviceContext := &ServiceContext{
Listens: []ma.Multiaddr{},
Key: s.keyPair,
quickTaskSender: quickTask,
taskSender: task,
}
service := service{
protoclConfigs: s.inner,
serviceContext: serviceContext,
state: state,
listens: make(map[ma.Multiaddr]manet.Listener),
dialProtocols: make(map[ma.Multiaddr]TargetProtocol),
config: s.config,
nextSession: SessionID(0),
beforeSends: make(map[ProtocolID]BeforeSend),
handleSender: handleChan,
serviceProtoHandles: make(map[ProtocolID]chan<- serviceProtocolEvent),
sessionProtoHandles: make(map[sessionProto]chan<- sessionProtocolEvent),
sessionEventChan: sessionChan,
sessions: make(map[SessionID]sessionController),
taskReceiver: task,
quickTaskReceiver: quickTask,
shutdown: shutdown,
}
handleProc := serviceHandleProc{
handle: handle,
serviceContext: serviceContext,
shutdown: &service.shutdown,
recv: handleChan,
}
go handleProc.run()
go service.run()
return service.control()
}
type serviceHandleProc struct {
handle ServiceHandle
serviceContext *ServiceContext
shutdown *atomic.Value
recv <-chan interface{}
}
func (h *serviceHandleProc) run() {
for {
if h.shutdown.Load().(bool) {
break
}
select {
case <-time.After(100 * time.Microsecond):
continue
case event := <-h.recv:
if h.handle != nil {
ev, ok := event.(ServiceEvent)
if ok {
h.handle.HandleEvent(h.serviceContext, ev)
continue
}
e, ok := event.(ServiceError)
if ok {
h.handle.HandleError(h.serviceContext, e)
}
}
}
}
}