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