| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | 
							- package ray
 
- import (
 
- 	"context"
 
- 	"io"
 
- 	"sync"
 
- 	"time"
 
- 	"v2ray.com/core/common/buf"
 
- )
 
- // NewRay creates a new Ray for direct traffic transport.
 
- func NewRay(ctx context.Context) Ray {
 
- 	return &directRay{
 
- 		Input:  NewStream(ctx),
 
- 		Output: NewStream(ctx),
 
- 	}
 
- }
 
- type directRay struct {
 
- 	Input  *Stream
 
- 	Output *Stream
 
- }
 
- func (v *directRay) OutboundInput() InputStream {
 
- 	return v.Input
 
- }
 
- func (v *directRay) OutboundOutput() OutputStream {
 
- 	return v.Output
 
- }
 
- func (v *directRay) InboundInput() OutputStream {
 
- 	return v.Input
 
- }
 
- func (v *directRay) InboundOutput() InputStream {
 
- 	return v.Output
 
- }
 
- type Stream struct {
 
- 	access sync.RWMutex
 
- 	data   buf.MultiBuffer
 
- 	ctx    context.Context
 
- 	wakeup chan bool
 
- 	close  bool
 
- 	err    bool
 
- }
 
- func NewStream(ctx context.Context) *Stream {
 
- 	return &Stream{
 
- 		ctx:    ctx,
 
- 		wakeup: make(chan bool, 1),
 
- 	}
 
- }
 
- 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 {
 
- 		return nil, io.ErrClosedPipe
 
- 	}
 
- 	return nil, nil
 
- }
 
- func (s *Stream) Peek(b *buf.Buffer) {
 
- 	s.access.RLock()
 
- 	defer s.access.RUnlock()
 
- 	b.Reset(func(data []byte) (int, error) {
 
- 		return s.data.Copy(data), 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
 
- 		}
 
- 		select {
 
- 		case <-s.ctx.Done():
 
- 			return nil, io.EOF
 
- 		case <-s.wakeup:
 
- 		}
 
- 	}
 
- }
 
- 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
 
- 		}
 
- 		select {
 
- 		case <-s.ctx.Done():
 
- 			return nil, io.EOF
 
- 		case <-time.After(timeout):
 
- 			return nil, buf.ErrReadTimeout
 
- 		case <-s.wakeup:
 
- 		}
 
- 	}
 
- }
 
- func (s *Stream) Write(data buf.MultiBuffer) error {
 
- 	if data.IsEmpty() {
 
- 		return nil
 
- 	}
 
- 	s.access.Lock()
 
- 	defer s.access.Unlock()
 
- 	if s.err || s.close {
 
- 		data.Release()
 
- 		return io.ErrClosedPipe
 
- 	}
 
- 	if s.data == nil {
 
- 		s.data = data
 
- 	} else {
 
- 		s.data.AppendMulti(data)
 
- 	}
 
- 	s.wakeUp()
 
- 	return nil
 
- }
 
- func (s *Stream) wakeUp() {
 
- 	select {
 
- 	case s.wakeup <- true:
 
- 	default:
 
- 	}
 
- }
 
- func (s *Stream) Close() {
 
- 	s.access.Lock()
 
- 	s.close = true
 
- 	s.wakeUp()
 
- 	s.access.Unlock()
 
- }
 
- func (s *Stream) CloseError() {
 
- 	s.access.Lock()
 
- 	s.err = true
 
- 	if s.data != nil {
 
- 		s.data.Release()
 
- 		s.data = nil
 
- 	}
 
- 	s.wakeUp()
 
- 	s.access.Unlock()
 
- }
 
 
  |