-
Notifications
You must be signed in to change notification settings - Fork 0
/
filechan.go
95 lines (81 loc) · 2.33 KB
/
filechan.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 preimage
import (
"errors"
"fmt"
"io"
"os"
"syscall"
)
// FileChannel is a unidirectional channel for file I/O
type FileChannel interface {
io.ReadWriteCloser
// Reader returns the file that is used for reading.
Reader() *os.File
// Writer returns the file that is used for writing.
Writer() *os.File
}
type ReadWritePair struct {
r *os.File
w *os.File
}
// NewReadWritePair creates a new FileChannel that uses the given files
func NewReadWritePair(r *os.File, w *os.File) *ReadWritePair {
return &ReadWritePair{r: r, w: w}
}
func (rw *ReadWritePair) Read(p []byte) (int, error) {
return rw.r.Read(p)
}
func (rw *ReadWritePair) Write(p []byte) (int, error) {
return rw.w.Write(p)
}
func (rw *ReadWritePair) Reader() *os.File {
return rw.r
}
func (rw *ReadWritePair) Writer() *os.File {
return rw.w
}
func (rw *ReadWritePair) Close() error {
var combinedErr error
if err := rw.r.Close(); err != nil {
combinedErr = errors.Join(fmt.Errorf("failed to close reader: %w", err))
}
if err := rw.w.Close(); err != nil {
combinedErr = errors.Join(fmt.Errorf("failed to close writer: %w", err))
}
return combinedErr
}
// CreateBidirectionalChannel creates a pair of FileChannels that are connected to each other.
func CreateBidirectionalChannel() (FileChannel, FileChannel, error) {
ar, bw, err := os.Pipe()
if err != nil {
return nil, nil, err
}
br, aw, err := os.Pipe()
if err != nil {
return nil, nil, err
}
return NewReadWritePair(ar, aw), NewReadWritePair(br, bw), nil
}
const (
HClientRFd = 3
HClientWFd = 4
PClientRFd = 5
PClientWFd = 6
)
func ClientHinterChannel() *ReadWritePair {
r := newFileNonBlocking(HClientRFd, "preimage-hint-read")
w := newFileNonBlocking(HClientWFd, "preimage-hint-write")
return NewReadWritePair(r, w)
}
// ClientPreimageChannel returns a FileChannel for the preimage oracle in a detached context
func ClientPreimageChannel() *ReadWritePair {
r := newFileNonBlocking(PClientRFd, "preimage-oracle-read")
w := newFileNonBlocking(PClientWFd, "preimage-oracle-write")
return NewReadWritePair(r, w)
}
func newFileNonBlocking(fd int, name string) *os.File {
// Try to enable non-blocking mode for IO so that read calls return when the file is closed
// This may not be possible on all systems so errors are ignored.
_ = syscall.SetNonblock(fd, true)
return os.NewFile(uintptr(fd), name)
}