1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-12 08:14:24 -04:00
v2fly/transport/ray/direct.go

126 lines
1.9 KiB
Go
Raw Normal View History

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-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.
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-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 {
access sync.RWMutex
closed bool
buffer chan *alloc.Buffer
}
func NewStream() *Stream {
return &Stream{
buffer: make(chan *alloc.Buffer, bufferSize),
}
}
2016-11-27 15:39:09 -05:00
func (v *Stream) Read() (*alloc.Buffer, error) {
if v.buffer == nil {
2016-04-18 12:44:10 -04:00
return nil, io.EOF
}
2016-11-27 15:39:09 -05:00
v.access.RLock()
if v.buffer == nil {
v.access.RUnlock()
2016-04-18 12:44:10 -04:00
return nil, io.EOF
}
2016-11-27 15:39:09 -05:00
channel := v.buffer
v.access.RUnlock()
2016-04-19 08:51:52 -04:00
result, open := <-channel
2016-04-18 12:44:10 -04:00
if !open {
return nil, io.EOF
}
return result, nil
}
2016-11-27 15:39:09 -05:00
func (v *Stream) Write(data *alloc.Buffer) error {
for !v.closed {
err := v.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
}
2016-11-27 15:39:09 -05:00
func (v *Stream) TryWriteOnce(data *alloc.Buffer) error {
v.access.RLock()
defer v.access.RUnlock()
if v.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 {
2016-11-27 15:39:09 -05:00
case v.buffer <- data:
2016-05-08 22:04:51 -04:00
return nil
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
}
2016-11-27 15:39:09 -05:00
func (v *Stream) Close() {
if v.closed {
2016-04-18 12:44:10 -04:00
return
}
2016-11-27 15:39:09 -05:00
v.access.Lock()
defer v.access.Unlock()
if v.closed {
2016-04-18 12:44:10 -04:00
return
}
2016-11-27 15:39:09 -05:00
v.closed = true
close(v.buffer)
2016-04-18 12:44:10 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *Stream) Release() {
if v.buffer == nil {
2016-04-18 12:44:10 -04:00
return
}
2016-11-27 15:39:09 -05:00
v.Close()
v.access.Lock()
defer v.access.Unlock()
if v.buffer == nil {
2016-04-18 12:44:10 -04:00
return
}
2016-11-27 15:39:09 -05:00
for data := range v.buffer {
2016-04-18 12:44:10 -04:00
data.Release()
}
2016-11-27 15:39:09 -05:00
v.buffer = nil
2016-04-18 12:44:10 -04:00
}