2015-10-14 13:07:13 +00:00
|
|
|
package ray
|
|
|
|
|
|
|
|
import (
|
2016-04-18 16:44:10 +00:00
|
|
|
"io"
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2015-10-14 13:07:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-12-02 13:22:46 +00:00
|
|
|
bufferSize = 512
|
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.
|
2015-10-14 13:07:13 +00:00
|
|
|
func NewRay() Ray {
|
|
|
|
return &directRay{
|
2016-04-18 16:44:10 +00:00
|
|
|
Input: NewStream(),
|
|
|
|
Output: NewStream(),
|
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 {
|
2016-12-09 10:35:27 +00:00
|
|
|
buffer chan *buf.Buffer
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStream() *Stream {
|
|
|
|
return &Stream{
|
2016-12-09 10:35:27 +00:00
|
|
|
buffer: make(chan *buf.Buffer, bufferSize),
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
func (v *Stream) Read() (*buf.Buffer, error) {
|
2016-12-22 16:28:06 +00:00
|
|
|
buffer, open := <-v.buffer
|
2016-04-18 16:44:10 +00:00
|
|
|
if !open {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2016-12-22 16:28:06 +00:00
|
|
|
return buffer, nil
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:28:06 +00:00
|
|
|
func (v *Stream) Write(data *buf.Buffer) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = io.ErrClosedPipe
|
2016-05-09 02:04:51 +00:00
|
|
|
}
|
2016-12-22 16:28:06 +00:00
|
|
|
}()
|
2016-05-09 02:04:51 +00:00
|
|
|
|
2016-12-22 16:28:06 +00:00
|
|
|
v.buffer <- data
|
|
|
|
return nil
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Stream) Close() {
|
2016-12-22 16:28:06 +00:00
|
|
|
defer swallowPanic()
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
close(v.buffer)
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Stream) Release() {
|
2016-12-22 16:28:06 +00:00
|
|
|
defer swallowPanic()
|
|
|
|
|
|
|
|
close(v.buffer)
|
|
|
|
|
|
|
|
for b := range v.buffer {
|
|
|
|
b.Release()
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
2016-12-22 16:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func swallowPanic() {
|
|
|
|
recover()
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|