Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
package pipe
 
import (
    "errors"
    "io"
    "runtime"
    "sync"
    "time"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/buf"
    "github.com/v2fly/v2ray-core/v5/common/signal"
    "github.com/v2fly/v2ray-core/v5/common/signal/done"
)
 
type state byte
 
const (
    open state = iota
    closed
    errord
)
 
type pipeOption struct {
    limit           int32 // maximum buffer size in bytes
    discardOverflow bool
}
 
func (o *pipeOption) isFull(curSize int32) bool {
    return o.limit >= 0 && curSize > o.limit
}
 
type pipe struct {
    sync.Mutex
    data        buf.MultiBuffer
    readSignal  *signal.Notifier
    writeSignal *signal.Notifier
    done        *done.Instance
    option      pipeOption
    state       state
}
 
var (
    errBufferFull = errors.New("buffer full")
    errSlowDown   = errors.New("slow down")
)
 
func (p *pipe) getState(forRead bool) error {
    switch p.state {
    case open:
        if !forRead && p.option.isFull(p.data.Len()) {
            return errBufferFull
        }
        return nil
    case closed:
        if !forRead {
            return io.ErrClosedPipe
        }
        if !p.data.IsEmpty() {
            return nil
        }
        return io.EOF
    case errord:
        return io.ErrClosedPipe
    default:
        panic("impossible case")
    }
}
 
func (p *pipe) readMultiBufferInternal() (buf.MultiBuffer, error) {
    p.Lock()
    defer p.Unlock()
 
    if err := p.getState(true); err != nil {
        return nil, err
    }
 
    data := p.data
    p.data = nil
    return data, nil
}
 
func (p *pipe) ReadMultiBuffer() (buf.MultiBuffer, error) {
    for {
        data, err := p.readMultiBufferInternal()
        if data != nil || err != nil {
            p.writeSignal.Signal()
            return data, err
        }
 
        select {
        case <-p.readSignal.Wait():
        case <-p.done.Wait():
        }
    }
}
 
func (p *pipe) ReadMultiBufferTimeout(d time.Duration) (buf.MultiBuffer, error) {
    timer := time.NewTimer(d)
    defer timer.Stop()
 
    for {
        data, err := p.readMultiBufferInternal()
        if data != nil || err != nil {
            p.writeSignal.Signal()
            return data, err
        }
 
        select {
        case <-p.readSignal.Wait():
        case <-p.done.Wait():
        case <-timer.C:
            return nil, buf.ErrReadTimeout
        }
    }
}
 
func (p *pipe) writeMultiBufferInternal(mb buf.MultiBuffer) error {
    p.Lock()
    defer p.Unlock()
 
    if err := p.getState(false); err != nil {
        return err
    }
 
    if p.data == nil {
        p.data = mb
        return nil
    }
 
    p.data, _ = buf.MergeMulti(p.data, mb)
    return errSlowDown
}
 
func (p *pipe) WriteMultiBuffer(mb buf.MultiBuffer) error {
    if mb.IsEmpty() {
        return nil
    }
 
    for {
        err := p.writeMultiBufferInternal(mb)
        if err == nil {
            p.readSignal.Signal()
            return nil
        }
 
        if err == errSlowDown {
            p.readSignal.Signal()
 
            // Yield current goroutine. Hopefully the reading counterpart can pick up the payload.
            runtime.Gosched()
            return nil
        }
 
        if err == errBufferFull && p.option.discardOverflow {
            buf.ReleaseMulti(mb)
            return nil
        }
 
        if err != errBufferFull {
            buf.ReleaseMulti(mb)
            p.readSignal.Signal()
            return err
        }
 
        select {
        case <-p.writeSignal.Wait():
        case <-p.done.Wait():
            return io.ErrClosedPipe
        }
    }
}
 
func (p *pipe) Close() error {
    p.Lock()
    defer p.Unlock()
 
    if p.state == closed || p.state == errord {
        return nil
    }
 
    p.state = closed
    common.Must(p.done.Close())
    return nil
}
 
// Interrupt implements common.Interruptible.
func (p *pipe) Interrupt() {
    p.Lock()
    defer p.Unlock()
 
    if p.state == closed || p.state == errord {
        return
    }
 
    p.state = errord
 
    if !p.data.IsEmpty() {
        buf.ReleaseMulti(p.data)
        p.data = nil
    }
 
    common.Must(p.done.Close())
}