-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
648 lines (547 loc) · 14 KB
/
pipe.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package named_pipe_ipc
import (
"bufio"
"bytes"
"context"
"encoding/binary"
uuid2 "github.com/satori/go.uuid"
"io"
"os"
"strings"
"syscall"
"time"
)
const (
defaultUmask = 0600
defaultDelim = '\n'
defaultNamedPipeForRead = "golang.pipe.1.r"
defaultNamedPipeForWrite = "golang.pipe.1.w"
)
const (
protoNormalType byte = '0'
protoResponseType byte = '1'
protoRetranType byte = '2'
protoFlag = "named-pipe-ipc"
)
var defaultOption = &options{
defaultDelim,
defaultNamedPipeForRead,
defaultNamedPipeForWrite,
}
type options struct {
delim byte
namedPipeForRead string
namedPipeForWrite string
}
type Option interface {
apply(*options)
}
type OptionsFunc func(o *options)
func (f OptionsFunc) apply(o *options) {
f(o)
}
func WithNamedPipeForRead(name string) Option {
return OptionsFunc(func(o *options) {
o.namedPipeForRead = name
})
}
func WithNamedPipeForWrite(name string) Option {
return OptionsFunc(func(o *options) {
o.namedPipeForWrite = name
})
}
func WithDelim(delim byte) Option {
return OptionsFunc(func(o *options) {
o.delim = delim
})
}
/**
protocol:
8byte - 14byte - 1byte - 16byte - 8byte - string
byteLength - flag - type - uuid - ttl - content
*/
type Message []byte
func (M Message) String() string {
return string(M)
}
func (M Message) Byte() []byte {
return M
}
func (M Message) segmentPackageLengthLen() int {
return 8
}
func (M Message) segmentTypeLen() int {
return 1
}
func (M Message) segmentUUIDLen() int {
return 16
}
func (M Message) segmentFlagLen() int {
return len(protoFlag)
}
func (M Message) segmentTTLLen() int {
return 8
}
func (M Message) segmentPackageLength() int64 {
return int64(binary.BigEndian.Uint64(M[0:M.segmentPackageLengthLen()]))
}
func (M Message) segmentFlag() (flag []byte) {
flag = M[M.segmentPackageLengthLen() : M.segmentPackageLengthLen()+M.segmentFlagLen()].Byte()
return flag
}
func (M Message) segmentType() (t byte) {
t = M[M.segmentPackageLengthLen()+M.segmentFlagLen() : M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()].Byte()[0]
return t
}
func (M Message) segmentUUID() (uuid uuid2.UUID, err error) {
uuid, err = uuid2.FromBytes(M[M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen() : M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()+M.segmentUUIDLen()])
return
}
func (M Message) segmentTTL() (ttl int64) {
timestamp := M[M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()+M.segmentUUIDLen() : M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()+M.segmentUUIDLen()+M.segmentTTLLen()]
ttl = int64(binary.BigEndian.Uint64(timestamp))
return
}
func (M Message) segmentPayload() Message {
return M[M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()+M.segmentUUIDLen()+M.segmentTTLLen():]
}
func (M Message) Payload() Message {
return M.segmentPayload()
}
func (M Message) isLegal() bool {
return bytes.Equal(M.segmentFlag(), []byte(protoFlag))
}
func (M Message) isRetran() bool {
return M[M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()-1] == protoRetranType
}
func (M Message) changeRetran() {
M[M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()-1] = protoRetranType
}
func (M Message) ResponsePayload(message Message) Message {
ma := make([]byte, 0)
ma = append(ma, M[M.segmentPackageLengthLen():M.segmentPackageLengthLen()+M.segmentFlagLen()+M.segmentTypeLen()+M.segmentUUIDLen()+M.segmentTTLLen()]...)
ma[M.segmentFlagLen()+M.segmentTypeLen()-1] = protoResponseType
ma = append(ma, message.Byte()...)
packageLengthBuf := make([]byte, 8)
// package-buf's length + delim's length
// 8 + 1s
binary.BigEndian.PutUint64(packageLengthBuf, uint64(len(ma)+8+1))
m := append(make([]byte, 0), packageLengthBuf...)
m = append(m, ma...)
return m
}
type Context struct {
out chan Message
role RoleType
delim byte
rPipe *os.File
wPipe *os.File
br *bufio.Reader
bw *bufio.Writer
context context.Context
chroot string
namedPipeForRead string
namedPipeForWrite string
clientID uuid2.UUID
}
func createFifo(nctx *Context) (err error) {
if ex, err := Exists(nctx.namedPipeForReadFullPath()); err != nil {
return err
} else {
if !ex {
err = syscall.Mkfifo(nctx.namedPipeForReadFullPath(), defaultUmask)
if err != nil {
return err
}
}
}
if ex, err := Exists(nctx.namedPipeForWriteFullPath()); err != nil {
return err
} else {
if !ex {
err = syscall.Mkfifo(nctx.namedPipeForWriteFullPath(), defaultUmask)
if err != nil {
return err
}
}
}
return nil
}
// openPipeFile
//
// Why use os.RDWR? not os.RD_ONLY or os.WD_ONLY ?
//
// When the FIFO is turned on, the non-blocking flag (O_NONBLOCK) has the following effects:
// i. O_NONBLOCK is not specified (i.e. open has no bit or O_NONBLOCK).
// 2. When a FIFO is opened in read-only mode, the FIFO is blocked until a process opens the FIFO for writing
// 3. When the FIFO is opened in write-only mode, it is blocked until a process opens the FIFO for reads.
// 4. When the FIFO is opened in read-only, write-only mode, it blocks. When the read function is called to read data from the FIFO, the read function also blocks.
// 4, Call the write function to write data to the FIFO, and write will block when the buffer is full.
// 5, communication process if the writing process first quit, then call the read function to read data from the FIFO does not block; If the writing process starts again, the read function is called to read data from the FIFO.
// 6. During the communication process, when the reader process exits and the writer process writes data to the named pipe, the writer process will also exit (receiving SIGPIPE signal).
// If no process has opened a FIFO for write, read - only open succeeds, and open is not blocked.
// ii. Specify O_NONBLOCK(that is, open bit or O_NONBLOCK)
// 1. If no process has opened a FIFO for read, writing only open will return -1.
// 2. Named pipes do not block when reading data.
// 3. During the communication process, when the reader process exits and the writer process writes data to the named pipe, the writer process will also exit (receiving SIGPIPE signal).
func openPipeFile(nctx *Context) (err error) {
nctx.rPipe, err = os.OpenFile(nctx.namedPipeForReadFullPath(), os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return err
}
nctx.wPipe, err = os.OpenFile(nctx.namedPipeForWriteFullPath(), os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return err
}
nctx.br = bufio.NewReader(nctx.rPipe)
nctx.bw = bufio.NewWriter(nctx.wPipe)
return nil
}
func NewContext(ctx context.Context, chroot string, role RoleType, opts ...Option) (*Context, error) {
if !IsDir(chroot) {
return nil, NotDirectory{}
}
if !strings.HasSuffix(chroot, "/") {
chroot += "/"
}
for _, o := range opts {
o.apply(defaultOption)
}
nctx := &Context{
role: role,
chroot: chroot,
delim: defaultOption.delim,
namedPipeForRead: defaultOption.namedPipeForRead,
namedPipeForWrite: defaultOption.namedPipeForWrite,
}
if nctx.role == C {
nctx.namedPipeForWrite = defaultOption.namedPipeForRead
nctx.namedPipeForRead = defaultOption.namedPipeForWrite
for {
nctx.clientID = uuid2.NewV4()
if !bytes.Contains(nctx.clientID.Bytes(), []byte{nctx.delim}) {
break
}
}
}
nctx.context = ctx
nctx.out = make(chan Message, 10)
if nctx.role == S {
err := createFifo(nctx)
if err != nil {
return nil, err
}
}
err := openPipeFile(nctx)
if err != nil {
return nil, err
}
return nctx, nil
}
func (nctx *Context) namedPipeForReadFullPath() string {
return nctx.chroot + nctx.namedPipeForRead
}
func (nctx *Context) namedPipeForWriteFullPath() string {
return nctx.chroot + nctx.namedPipeForWrite
}
func (nctx *Context) NamedPipeForRead() string {
return nctx.namedPipeForRead
}
func (nctx *Context) NamedPipeForWrite() string {
return nctx.namedPipeForWrite
}
func (nctx *Context) Chroot() string {
return nctx.chroot
}
// Send Message
//
// This API should work best with Write, but since most people are web developers
// the send()/ recv() combination is more acceptable
func (nctx *Context) Send(message Message) (int, error) {
if nctx.role == S {
if !message.isLegal() {
return 0, new(MessageNotLegal)
}
return nctx.directlySend(append(message, nctx.delim))
}
buf := make([]byte, 0, 0)
// flag
buf = append(buf, []byte(protoFlag)...)
// type
buf = append(buf, protoNormalType)
// uuid
buf = append(buf, nctx.clientID.Bytes()...)
// ttl 30 second
ttl := time.Now().Unix() + 10
timeBuf := make([]byte, 8)
binary.BigEndian.PutUint64(timeBuf, uint64(ttl))
buf = append(buf, timeBuf...)
// content
buf = append(buf, append(message, nctx.delim).Byte()...)
// package length
packageLengthBuf := make([]byte, 8)
binary.BigEndian.PutUint64(packageLengthBuf, uint64(len(buf)+8))
protocol := make([]byte, 0, len(packageLengthBuf)+len(buf))
protocol = append(protocol, packageLengthBuf...)
protocol = append(protocol, buf...)
nn, err := nctx.bw.Write(protocol)
if err != nil {
return 0, nil
}
err = nctx.bw.Flush()
if err != nil {
return 0, err
}
return nn, nil
}
func (nctx *Context) directlySend(message Message) (int, error) {
nn, err := nctx.bw.Write(message)
if err != nil {
return 0, nil
}
err = nctx.bw.Flush()
if err != nil {
return 0, err
}
return nn, nil
}
// Recv Message
//
// This API should work best with Read, but since most people are web developers
// the send()/ recv() combination is more acceptable
func (nctx *Context) Recv(block bool) (Message, error) {
if nctx.role == S {
if !block {
if len(nctx.out) == 0 {
time.Sleep(1 * time.Millisecond)
return nil, NoMessage{}
}
}
for {
select {
case <-nctx.context.Done():
return nil, nctx.context.Err()
case msg := <-nctx.out:
if msg == nil {
return msg, Closed{}
}
if msg.isRetran() {
_, err := nctx.Send(msg)
if err != nil {
return nil, err
}
continue
}
return msg, nil
}
}
} else {
var (
bf Message
err error
)
ok := make(chan bool, 1)
go func() {
ResetReadBytes:
var bfs = make([]Message, 0)
var expectLength int64 = 0
ReadBytes:
bf, err = nctx.br.ReadBytes(nctx.delim)
if err != nil && err != io.EOF {
if pe, ok := err.(*os.PathError); ok {
if pe.Err == os.ErrClosed {
bf = nil
err = Closed{}
return
}
}
bf = nil
return
}
if bf != nil {
if expectLength == 0 {
expectLength = bf.segmentPackageLength()
bfs = append(bfs, bf)
} else {
bfs = append(bfs, bf)
}
var sum int64 = 0
for _, tbf := range bfs {
sum += int64(len(tbf.Byte()))
}
if sum != expectLength {
if sum > expectLength {
goto ResetReadBytes
}
goto ReadBytes
}
var buf Message
for _, tbf := range bfs {
buf = append(buf, tbf.Byte()...)
}
message := buf
uuid, err := message.segmentUUID()
if err != nil {
return
}
if message.segmentTTL() < time.Now().Unix() {
// drop message
goto ReadBytes
}
if uuid != nctx.clientID {
// resend message to server
message.changeRetran()
_, err = nctx.directlySend(message)
if err != nil {
return
}
goto ReadBytes
}
// read not include nctx.delim
bf = buf[:len(buf)-1]
ok <- true
}
}()
for {
select {
case <-nctx.context.Done():
err = nctx.close()
return nil, HybridError{nctx.context.Err(), err}
case <-ok:
return bf, nil
}
}
}
}
// Listen Message
func (nctx *Context) Listen() error {
var err error
var bf Message
var bfs = make([]Message, 0)
var expectLength int64 = 0
for err == nil {
select {
case <-nctx.context.Done():
close(nctx.out)
return nil
default:
if nctx.context.Err() != nil {
close(nctx.out)
return nctx.context.Err()
}
}
bf, err = nctx.br.ReadBytes(nctx.delim)
if err != nil && err != io.EOF {
if pe, ok := err.(*os.PathError); ok {
if pe.Err == os.ErrClosed {
return nil
}
}
return err
}
if bf != nil {
if expectLength == 0 {
expectLength = bf.segmentPackageLength()
bfs = append(bfs, bf)
} else {
bfs = append(bfs, bf)
}
var sum int64 = 0
for _, tbf := range bfs {
sum += int64(len(tbf.Byte()))
}
if sum != expectLength {
continue
}
var buf Message
for _, tbf := range bfs {
buf = append(buf, tbf.Byte()...)
}
// read not include nctx.delim
buf = buf[:len(buf)-1]
nctx.out <- buf
expectLength = 0
bfs = make([]Message, 0)
}
}
return nil
}
func (nctx *Context) Close() error {
if err := nctx.close(); err != nil {
return err
}
if err := nctx.removeFiFo(); err != nil {
if pe, ok := err.(*os.PathError); ok {
if pe.Err != os.ErrClosed {
return err
}
} else {
return err
}
}
return nil
}
func (nctx *Context) close() error {
if nctx.rPipe != nil {
if err := nctx.rPipe.Close(); err != nil {
if pe, ok := err.(*os.PathError); ok {
if pe.Err != os.ErrClosed {
return err
}
} else {
return err
}
}
}
if nctx.wPipe != nil {
if err := nctx.wPipe.Close(); err != nil {
if pe, ok := err.(*os.PathError); ok {
if pe.Err != os.ErrClosed {
return err
}
} else {
return err
}
}
}
return nil
}
func (nctx *Context) removeFiFo() error {
if IsFile(nctx.namedPipeForWriteFullPath()) {
err := os.Remove(nctx.namedPipeForWriteFullPath())
if err != nil {
return err
}
}
if IsFile(nctx.namedPipeForReadFullPath()) {
err := os.Remove(nctx.namedPipeForReadFullPath())
if err != nil {
return err
}
}
return nil
}
func Exists(path string) (bool, error) {
s, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
if s.Mode() != os.ModeNamedPipe {
return true, AlreadyExistButNotNamedPipe{}
} else {
return true, nil
}
}
return false, nil
}
return true, nil
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func IsFile(path string) bool {
return !IsDir(path)
}