1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/transport/pipe/pipe.go

70 lines
1.4 KiB
Go
Raw Normal View History

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