forked from markkurossi/mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
117 lines (102 loc) · 2.18 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
//
// pipe.go
//
// Copyright (c) 2023-2024 Markku Rossi
//
// All rights reserved.
package ot
import (
"fmt"
"io"
)
var (
_ IO = &Pipe{}
)
// Pipe implements the IO interface with in-memory io.Pipe.
type Pipe struct {
rBuf []byte
wBuf []byte
r *io.PipeReader
w *io.PipeWriter
}
// NewPipe creates a new in-memory pipe.
func NewPipe() (*Pipe, *Pipe) {
ar, aw := io.Pipe()
br, bw := io.Pipe()
return &Pipe{
rBuf: make([]byte, 64*1024),
wBuf: make([]byte, 64*1024),
r: ar,
w: bw,
}, &Pipe{
rBuf: make([]byte, 64*1024),
wBuf: make([]byte, 64*1024),
r: br,
w: aw,
}
}
// SendByte sends a byte value.
func (p *Pipe) SendByte(val byte) error {
p.wBuf[0] = val
_, err := p.w.Write(p.wBuf[:1])
return err
}
// SendUint32 sends an uint32 value.
func (p *Pipe) SendUint32(val int) error {
bo.PutUint32(p.wBuf, uint32(val))
_, err := p.w.Write(p.wBuf[:4])
return err
}
// SendData sends binary data.
func (p *Pipe) SendData(val []byte) error {
l := len(val)
bo.PutUint32(p.wBuf, uint32(l))
n := copy(p.wBuf[4:], val)
if n != l {
return fmt.Errorf("pipe buffer too short: %d > %d", l, len(p.wBuf))
}
_, err := p.w.Write(p.wBuf[:4+l])
return err
}
// Flush flushed any pending data in the connection.
func (p *Pipe) Flush() error {
return nil
}
// Drain consumes all input from the pipe.
func (p *Pipe) Drain() error {
_, err := io.Copy(io.Discard, p.r)
return err
}
// Close closes the pipe.
func (p *Pipe) Close() error {
return p.w.Close()
}
// ReceiveByte receives a byte value.
func (p *Pipe) ReceiveByte() (byte, error) {
_, err := p.r.Read(p.rBuf[:1])
if err != nil {
return 0, err
}
return p.rBuf[0], nil
}
// ReceiveUint32 receives an uint32 value.
func (p *Pipe) ReceiveUint32() (int, error) {
_, err := p.r.Read(p.rBuf[:4])
if err != nil {
return 0, err
}
return int(bo.Uint32(p.rBuf)), nil
}
// ReceiveData receives binary data.
func (p *Pipe) ReceiveData() ([]byte, error) {
_, err := p.r.Read(p.rBuf[:4])
if err != nil {
return nil, err
}
l := bo.Uint32(p.rBuf)
if l > uint32(len(p.rBuf)) {
return nil, fmt.Errorf("pipe buffer too short: %d > %d", l, len(p.rBuf))
}
n, err := p.r.Read(p.rBuf[:])
return p.rBuf[:n], err
}