1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 15:26:29 -04:00
v2fly/transport/pipe/pipe.go
Darien Raymond 70b2b22047
fix #1087
2018-05-08 17:40:16 +02:00

64 lines
1.1 KiB
Go

package pipe
import (
"v2ray.com/core/common/platform"
"v2ray.com/core/common/signal"
)
type Option func(*pipe)
func WithoutSizeLimit() Option {
return func(p *pipe) {
p.limit = -1
}
}
func WithSizeLimit(limit int32) Option {
return func(p *pipe) {
p.limit = limit
}
}
// New creates a new Reader and Writer that connects to each other.
func New(opts ...Option) (*Reader, *Writer) {
p := &pipe{
limit: defaultLimit,
readSignal: signal.NewNotifier(),
writeSignal: signal.NewNotifier(),
}
for _, opt := range opts {
opt(p)
}
return &Reader{
pipe: p,
}, &Writer{
pipe: p,
}
}
type closeError interface {
CloseError()
}
// CloseError invokes CloseError() method if the object is either Reader or Writer.
func CloseError(v interface{}) {
if c, ok := v.(closeError); ok {
c.CloseError()
}
}
var defaultLimit int32 = 10 * 1024 * 1024
func init() {
const raySizeEnvKey = "v2ray.ray.buffer.size"
size := platform.EnvFlag{
Name: raySizeEnvKey,
AltName: platform.NormalizeEnvName(raySizeEnvKey),
}.GetValueAsInt(10)
if size > 0 {
defaultLimit = int32(size) * 1024 * 1024
}
}