v2ray-core/transport/ray/direct.go

175 lines
2.9 KiB
Go
Raw Normal View History

2015-10-14 13:07:13 +00:00
package ray
import (
2017-01-04 11:34:01 +00:00
"errors"
2016-04-18 16:44:10 +00:00
"io"
2017-01-04 11:34:01 +00:00
"time"
"context"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2015-10-14 13:07:13 +00:00
)
const (
2016-12-02 13:22:46 +00:00
bufferSize = 512
2015-10-14 13:07:13 +00:00
)
2017-01-04 11:34:01 +00:00
var ErrReadTimeout = errors.New("Ray: timeout.")
2015-10-15 11:15:59 +00:00
// NewRay creates a new Ray for direct traffic transport.
func NewRay(ctx context.Context) Ray {
2015-10-14 13:07:13 +00:00
return &directRay{
Input: NewStream(ctx),
Output: NewStream(ctx),
2015-10-14 13:07:13 +00:00
}
}
type directRay struct {
2016-04-18 16:44:10 +00:00
Input *Stream
Output *Stream
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) OutboundInput() InputStream {
return v.Input
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) OutboundOutput() OutputStream {
return v.Output
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) InboundInput() OutputStream {
return v.Input
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) InboundOutput() InputStream {
return v.Output
2015-10-14 13:07:13 +00:00
}
2016-04-18 16:44:10 +00:00
2017-01-04 11:34:01 +00:00
func (v *directRay) AddInspector(inspector Inspector) {
if inspector == nil {
return
}
v.Input.inspector.AddInspector(inspector)
v.Output.inspector.AddInspector(inspector)
}
2016-04-18 16:44:10 +00:00
type Stream struct {
2016-12-24 23:42:03 +00:00
buffer chan *buf.Buffer
ctx context.Context
2017-01-10 13:22:42 +00:00
close chan bool
err chan bool
2017-01-04 11:34:01 +00:00
inspector *InspectorChain
2016-04-18 16:44:10 +00:00
}
func NewStream(ctx context.Context) *Stream {
2016-04-18 16:44:10 +00:00
return &Stream{
ctx: ctx,
2016-12-24 23:42:03 +00:00
buffer: make(chan *buf.Buffer, bufferSize),
2017-01-10 13:22:42 +00:00
close: make(chan bool),
err: make(chan bool),
2017-01-04 11:34:01 +00:00
inspector: &InspectorChain{},
2016-04-18 16:44:10 +00:00
}
}
2016-12-09 10:35:27 +00:00
func (v *Stream) Read() (*buf.Buffer, error) {
2016-12-24 23:42:03 +00:00
select {
case <-v.ctx.Done():
return nil, io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.err:
2016-12-24 23:42:03 +00:00
return nil, io.ErrClosedPipe
case b := <-v.buffer:
return b, nil
default:
select {
case <-v.ctx.Done():
return nil, io.ErrClosedPipe
2016-12-24 23:42:03 +00:00
case b := <-v.buffer:
return b, nil
2017-01-10 13:22:42 +00:00
case <-v.close:
2016-12-24 23:42:03 +00:00
return nil, io.EOF
2017-01-10 13:22:42 +00:00
case <-v.err:
2016-12-29 23:32:20 +00:00
return nil, io.ErrClosedPipe
2016-12-24 23:42:03 +00:00
}
2016-04-18 16:44:10 +00:00
}
}
2017-01-04 11:34:01 +00:00
func (v *Stream) ReadTimeout(timeout time.Duration) (*buf.Buffer, error) {
select {
case <-v.ctx.Done():
return nil, io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.err:
2017-01-04 11:34:01 +00:00
return nil, io.ErrClosedPipe
case b := <-v.buffer:
return b, nil
default:
select {
case <-v.ctx.Done():
return nil, io.ErrClosedPipe
2017-01-04 11:34:01 +00:00
case b := <-v.buffer:
return b, nil
2017-01-10 13:22:42 +00:00
case <-v.close:
2017-01-04 11:34:01 +00:00
return nil, io.EOF
2017-01-10 13:22:42 +00:00
case <-v.err:
2017-01-04 11:34:01 +00:00
return nil, io.ErrClosedPipe
case <-time.After(timeout):
return nil, ErrReadTimeout
}
}
}
2016-12-22 16:28:06 +00:00
func (v *Stream) Write(data *buf.Buffer) (err error) {
2016-12-26 23:44:11 +00:00
if data.IsEmpty() {
return
}
2016-12-24 23:42:03 +00:00
select {
case <-v.ctx.Done():
return io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.err:
2016-12-24 23:42:03 +00:00
return io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.close:
2016-12-24 23:42:03 +00:00
return io.ErrClosedPipe
default:
select {
case <-v.ctx.Done():
return io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.err:
2016-12-24 23:42:03 +00:00
return io.ErrClosedPipe
2017-01-10 13:22:42 +00:00
case <-v.close:
2016-12-24 23:42:03 +00:00
return io.ErrClosedPipe
case v.buffer <- data:
2017-01-04 11:34:01 +00:00
v.inspector.Input(data)
2016-12-24 23:42:03 +00:00
return nil
2016-05-09 02:04:51 +00:00
}
2016-12-24 23:42:03 +00:00
}
2016-04-18 16:44:10 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Stream) Close() {
2016-12-22 16:28:06 +00:00
defer swallowPanic()
2017-01-10 13:22:42 +00:00
close(v.close)
2016-04-18 16:44:10 +00:00
}
2017-01-10 13:22:42 +00:00
func (v *Stream) CloseError() {
2016-12-22 16:28:06 +00:00
defer swallowPanic()
2017-01-10 13:22:42 +00:00
close(v.err)
2016-12-22 16:28:06 +00:00
2016-12-24 23:42:03 +00:00
n := len(v.buffer)
for i := 0; i < n; i++ {
select {
case b := <-v.buffer:
b.Release()
default:
return
}
2016-04-18 16:44:10 +00:00
}
2016-12-22 16:28:06 +00:00
}
2016-12-29 23:32:20 +00:00
func (v *Stream) Release() {}
2016-12-22 16:28:06 +00:00
func swallowPanic() {
recover()
2016-04-18 16:44:10 +00:00
}