2015-10-14 09:07:13 -04:00
|
|
|
package ray
|
|
|
|
|
|
|
|
import (
|
2016-05-08 22:04:51 -04:00
|
|
|
"errors"
|
2016-04-18 12:44:10 -04:00
|
|
|
"io"
|
|
|
|
"sync"
|
2016-05-08 22:04:51 -04:00
|
|
|
"time"
|
2016-04-18 12:44:10 -04:00
|
|
|
|
2015-10-14 09:07:13 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-03 09:12:46 -05:00
|
|
|
bufferSize = 128
|
2015-10-14 09:07:13 -04:00
|
|
|
)
|
|
|
|
|
2016-05-08 22:04:51 -04:00
|
|
|
var (
|
|
|
|
ErrorIOTimeout = errors.New("IO Timeout")
|
|
|
|
)
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// NewRay creates a new Ray for direct traffic transport.
|
2015-10-14 09:07:13 -04:00
|
|
|
func NewRay() Ray {
|
|
|
|
return &directRay{
|
2016-04-18 12:44:10 -04:00
|
|
|
Input: NewStream(),
|
|
|
|
Output: NewStream(),
|
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-04-18 12:44:10 -04:00
|
|
|
func (this *directRay) OutboundInput() InputStream {
|
2015-11-27 15:57:15 -05:00
|
|
|
return this.Input
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 12:44:10 -04:00
|
|
|
func (this *directRay) OutboundOutput() OutputStream {
|
2015-11-27 15:57:15 -05:00
|
|
|
return this.Output
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 12:44:10 -04:00
|
|
|
func (this *directRay) InboundInput() OutputStream {
|
2015-11-27 15:57:15 -05:00
|
|
|
return this.Input
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 12:44:10 -04:00
|
|
|
func (this *directRay) InboundOutput() InputStream {
|
2015-11-27 15:57:15 -05:00
|
|
|
return this.Output
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
2016-04-18 12:44:10 -04:00
|
|
|
|
|
|
|
type Stream struct {
|
|
|
|
access sync.RWMutex
|
|
|
|
closed bool
|
|
|
|
buffer chan *alloc.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStream() *Stream {
|
|
|
|
return &Stream{
|
|
|
|
buffer: make(chan *alloc.Buffer, bufferSize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Stream) Read() (*alloc.Buffer, error) {
|
|
|
|
if this.buffer == nil {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
this.access.RLock()
|
|
|
|
if this.buffer == nil {
|
2016-04-19 08:51:52 -04:00
|
|
|
this.access.RUnlock()
|
2016-04-18 12:44:10 -04:00
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2016-04-19 08:51:52 -04:00
|
|
|
channel := this.buffer
|
|
|
|
this.access.RUnlock()
|
|
|
|
result, open := <-channel
|
2016-04-18 12:44:10 -04:00
|
|
|
if !open {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Stream) Write(data *alloc.Buffer) error {
|
|
|
|
if this.closed {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2016-05-08 22:04:51 -04:00
|
|
|
for {
|
|
|
|
err := this.TryWriteOnce(data)
|
|
|
|
if err != ErrorIOTimeout {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Stream) TryWriteOnce(data *alloc.Buffer) error {
|
2016-04-18 12:44:10 -04:00
|
|
|
this.access.RLock()
|
|
|
|
defer this.access.RUnlock()
|
2016-04-19 04:30:42 -04:00
|
|
|
if this.closed {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2016-05-08 22:04:51 -04:00
|
|
|
select {
|
|
|
|
case this.buffer <- data:
|
|
|
|
return nil
|
2016-05-09 11:23:46 -04:00
|
|
|
case <-time.After(2 * time.Second):
|
2016-05-08 22:04:51 -04:00
|
|
|
return ErrorIOTimeout
|
|
|
|
}
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Stream) Close() {
|
|
|
|
if this.closed {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 08:51:52 -04:00
|
|
|
this.access.Lock()
|
|
|
|
defer this.access.Unlock()
|
2016-04-18 12:44:10 -04:00
|
|
|
if this.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.closed = true
|
|
|
|
close(this.buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Stream) Release() {
|
|
|
|
if this.buffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.Close()
|
|
|
|
this.access.Lock()
|
|
|
|
defer this.access.Unlock()
|
|
|
|
if this.buffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for data := range this.buffer {
|
|
|
|
data.Release()
|
|
|
|
}
|
|
|
|
this.buffer = nil
|
|
|
|
}
|