2015-10-14 09:07:13 -04:00
|
|
|
package ray
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/alloc"
|
2015-10-14 09:07:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-03 09:12:46 -05:00
|
|
|
bufferSize = 128
|
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.
|
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 {
|
2016-07-29 06:13:09 -04:00
|
|
|
for !this.closed {
|
2016-05-08 22:04:51 -04:00
|
|
|
err := this.TryWriteOnce(data)
|
2016-11-17 17:21:44 -05:00
|
|
|
if err != io.ErrNoProgress {
|
2016-05-08 22:04:51 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 17:21:44 -05:00
|
|
|
return io.ErrClosedPipe
|
2016-05-08 22:04:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-11-17 17:21:44 -05:00
|
|
|
return io.ErrClosedPipe
|
2016-04-19 04:30:42 -04:00
|
|
|
}
|
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-11-17 17:21:44 -05:00
|
|
|
return io.ErrNoProgress
|
2016-05-08 22:04:51 -04:00
|
|
|
}
|
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
|
|
|
|
}
|