You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/ray/direct.go

160 lines
2.5 KiB

package ray
import (
"errors"
"io"
"time"
"v2ray.com/core/common/buf"
)
const (
bufferSize = 512
)
var ErrReadTimeout = errors.New("Ray: timeout.")
9 years ago
// NewRay creates a new Ray for direct traffic transport.
func NewRay() Ray {
return &directRay{
Input: NewStream(),
Output: NewStream(),
}
}
type directRay struct {
Input *Stream
Output *Stream
}
8 years ago
func (v *directRay) OutboundInput() InputStream {
return v.Input
}
8 years ago
func (v *directRay) OutboundOutput() OutputStream {
return v.Output
}
8 years ago
func (v *directRay) InboundInput() OutputStream {
return v.Input
}
8 years ago
func (v *directRay) InboundOutput() InputStream {
return v.Output
}
func (v *directRay) AddInspector(inspector Inspector) {
if inspector == nil {
return
}
v.Input.inspector.AddInspector(inspector)
v.Output.inspector.AddInspector(inspector)
}
type Stream struct {
buffer chan *buf.Buffer
srcClose chan bool
destClose chan bool
inspector *InspectorChain
}
func NewStream() *Stream {
return &Stream{
buffer: make(chan *buf.Buffer, bufferSize),
srcClose: make(chan bool),
destClose: make(chan bool),
inspector: &InspectorChain{},
}
}
func (v *Stream) Read() (*buf.Buffer, error) {
select {
case <-v.destClose:
return nil, io.ErrClosedPipe
case b := <-v.buffer:
return b, nil
default:
select {
case b := <-v.buffer:
return b, nil
case <-v.srcClose:
return nil, io.EOF
case <-v.destClose:
return nil, io.ErrClosedPipe
}
}
}
func (v *Stream) ReadTimeout(timeout time.Duration) (*buf.Buffer, error) {
select {
case <-v.destClose:
return nil, io.ErrClosedPipe
case b := <-v.buffer:
return b, nil
default:
select {
case b := <-v.buffer:
return b, nil
case <-v.srcClose:
return nil, io.EOF
case <-v.destClose:
return nil, io.ErrClosedPipe
case <-time.After(timeout):
return nil, ErrReadTimeout
}
}
}
8 years ago
func (v *Stream) Write(data *buf.Buffer) (err error) {
if data.IsEmpty() {
return
}
select {
case <-v.destClose:
return io.ErrClosedPipe
case <-v.srcClose:
return io.ErrClosedPipe
default:
select {
case <-v.destClose:
return io.ErrClosedPipe
case <-v.srcClose:
return io.ErrClosedPipe
case v.buffer <- data:
v.inspector.Input(data)
return nil
}
}
}
8 years ago
func (v *Stream) Close() {
8 years ago
defer swallowPanic()
close(v.srcClose)
}
func (v *Stream) ForceClose() {
8 years ago
defer swallowPanic()
close(v.destClose)
v.Close()
8 years ago
n := len(v.buffer)
for i := 0; i < n; i++ {
select {
case b := <-v.buffer:
b.Release()
default:
return
}
}
8 years ago
}
func (v *Stream) Release() {}
8 years ago
func swallowPanic() {
recover()
}