v2ray-core/transport/ray/direct.go

165 lines
2.3 KiB
Go
Raw Normal View History

2015-10-14 13:07:13 +00:00
package ray
import (
"context"
2016-04-18 16:44:10 +00:00
"io"
2017-04-16 07:57:28 +00:00
"sync"
2017-01-04 11:34:01 +00:00
"time"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2015-10-14 13:07:13 +00:00
)
2015-10-15 11:15:59 +00:00
// NewRay creates a new Ray for direct traffic transport.
func NewRay(ctx context.Context) Ray {
2015-10-14 13:07:13 +00:00
return &directRay{
Input: NewStream(ctx),
Output: NewStream(ctx),
2015-10-14 13:07:13 +00:00
}
}
type directRay struct {
2016-04-18 16:44:10 +00:00
Input *Stream
Output *Stream
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) OutboundInput() InputStream {
return v.Input
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) OutboundOutput() OutputStream {
return v.Output
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) InboundInput() OutputStream {
return v.Input
2015-10-14 13:07:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *directRay) InboundOutput() InputStream {
return v.Output
2015-10-14 13:07:13 +00:00
}
2016-04-18 16:44:10 +00:00
type Stream struct {
2017-04-16 07:57:28 +00:00
access sync.Mutex
data buf.MultiBuffer
ctx context.Context
2017-04-16 07:57:28 +00:00
wakeup chan bool
close bool
err bool
2016-04-18 16:44:10 +00:00
}
func NewStream(ctx context.Context) *Stream {
2016-04-18 16:44:10 +00:00
return &Stream{
ctx: ctx,
2017-04-16 07:57:28 +00:00
wakeup: make(chan bool, 1),
2016-04-18 16:44:10 +00:00
}
}
2017-04-16 07:57:28 +00:00
func (s *Stream) getData() (buf.MultiBuffer, error) {
s.access.Lock()
defer s.access.Unlock()
if s.data != nil {
mb := s.data
s.data = nil
return mb, nil
}
if s.close {
return nil, io.EOF
}
if s.err {
2016-12-24 23:42:03 +00:00
return nil, io.ErrClosedPipe
2017-04-16 07:57:28 +00:00
}
return nil, nil
}
func (s *Stream) Read() (buf.MultiBuffer, error) {
for {
mb, err := s.getData()
if err != nil {
return nil, err
}
if mb != nil {
return mb, nil
}
2016-12-24 23:42:03 +00:00
select {
2017-04-16 07:57:28 +00:00
case <-s.ctx.Done():
2017-04-16 19:31:16 +00:00
return nil, io.EOF
2017-04-16 07:57:28 +00:00
case <-s.wakeup:
2016-12-24 23:42:03 +00:00
}
2016-04-18 16:44:10 +00:00
}
}
2017-04-16 07:57:28 +00:00
func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
for {
mb, err := s.getData()
if err != nil {
return nil, err
}
if mb != nil {
return mb, nil
2017-03-27 06:56:16 +00:00
}
2017-01-04 11:34:01 +00:00
select {
2017-04-16 07:57:28 +00:00
case <-s.ctx.Done():
2017-04-16 19:31:16 +00:00
return nil, io.EOF
2017-01-04 11:34:01 +00:00
case <-time.After(timeout):
2017-03-27 09:12:34 +00:00
return nil, buf.ErrReadTimeout
2017-04-16 07:57:28 +00:00
case <-s.wakeup:
2017-01-04 11:34:01 +00:00
}
}
}
2017-04-19 09:20:08 +00:00
func (s *Stream) Write(data buf.MultiBuffer) error {
2016-12-26 23:44:11 +00:00
if data.IsEmpty() {
2017-04-19 09:20:08 +00:00
return nil
2016-12-26 23:44:11 +00:00
}
2017-04-16 07:57:28 +00:00
s.access.Lock()
defer s.access.Unlock()
2017-04-19 09:20:08 +00:00
if s.err || s.close {
2017-04-16 07:57:28 +00:00
data.Release()
2016-12-24 23:42:03 +00:00
return io.ErrClosedPipe
}
2016-04-18 16:44:10 +00:00
2017-04-16 07:57:28 +00:00
if s.data == nil {
s.data = data
} else {
s.data.AppendMulti(data)
}
s.wakeUp()
2016-12-22 16:28:06 +00:00
2017-04-16 07:57:28 +00:00
return nil
2016-04-18 16:44:10 +00:00
}
2017-04-16 07:57:28 +00:00
func (s *Stream) wakeUp() {
select {
case s.wakeup <- true:
default:
}
}
2016-12-22 16:28:06 +00:00
2017-04-16 07:57:28 +00:00
func (s *Stream) Close() {
s.access.Lock()
s.close = true
s.wakeUp()
s.access.Unlock()
}
2016-12-22 16:28:06 +00:00
2017-04-16 07:57:28 +00:00
func (s *Stream) CloseError() {
s.access.Lock()
s.err = true
if s.data != nil {
s.data.Release()
s.data = nil
2016-04-18 16:44:10 +00:00
}
2017-04-16 07:57:28 +00:00
s.wakeUp()
s.access.Unlock()
2016-12-22 16:28:06 +00:00
}