v2ray-core/transport/pipe/pipe.go

79 lines
1.5 KiB
Go
Raw Normal View History

2018-04-16 12:57:13 +00:00
package pipe
import (
2018-05-25 10:08:28 +00:00
"context"
2018-04-16 12:57:13 +00:00
"v2ray.com/core/common/signal"
2018-06-09 02:47:37 +00:00
"v2ray.com/core/common/signal/done"
2018-10-11 20:34:31 +00:00
"v2ray.com/core/features/policy"
2018-04-16 12:57:13 +00:00
)
2018-05-25 21:20:24 +00:00
// Option for creating new Pipes.
2018-04-16 12:57:13 +00:00
type Option func(*pipe)
2018-09-02 22:56:43 +00:00
// WithoutSizeLimit returns an Option for Pipe to have no size limit.
2018-04-16 12:57:13 +00:00
func WithoutSizeLimit() Option {
return func(p *pipe) {
p.limit = -1
}
}
2018-09-02 22:56:43 +00:00
// WithSizeLimit returns an Option for Pipe to have the given size limit.
2018-04-16 12:57:13 +00:00
func WithSizeLimit(limit int32) Option {
return func(p *pipe) {
p.limit = limit
}
}
2018-09-02 22:56:43 +00:00
// DiscardOverflow returns an Option for Pipe to discard writes if full.
func DiscardOverflow() Option {
return func(p *pipe) {
p.discardOverflow = true
}
}
// OptionsFromContext returns a list of Options from context.
2018-05-25 10:08:28 +00:00
func OptionsFromContext(ctx context.Context) []Option {
var opt []Option
2018-10-11 20:34:31 +00:00
bp := policy.BufferPolicyFromContext(ctx)
2018-05-25 11:12:00 +00:00
if bp.PerConnection >= 0 {
opt = append(opt, WithSizeLimit(bp.PerConnection))
2018-05-25 10:08:28 +00:00
} else {
opt = append(opt, WithoutSizeLimit())
}
return opt
}
2018-04-16 22:45:38 +00:00
// New creates a new Reader and Writer that connects to each other.
2018-04-16 12:57:13 +00:00
func New(opts ...Option) (*Reader, *Writer) {
p := &pipe{
2018-05-25 11:12:00 +00:00
limit: -1,
2018-04-16 12:57:13 +00:00
readSignal: signal.NewNotifier(),
writeSignal: signal.NewNotifier(),
2018-06-09 02:47:37 +00:00
done: done.New(),
2018-04-16 12:57:13 +00:00
}
for _, opt := range opts {
opt(p)
}
return &Reader{
pipe: p,
}, &Writer{
pipe: p,
}
}
2018-04-16 22:31:10 +00:00
type closeError interface {
CloseError()
}
2018-04-18 13:40:43 +00:00
// CloseError invokes CloseError() method if the object is either Reader or Writer.
2018-04-16 22:31:10 +00:00
func CloseError(v interface{}) {
if c, ok := v.(closeError); ok {
c.CloseError()
}
}