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

130 lines
2.6 KiB
Go
Raw Normal View History

2018-01-10 11:31:52 -05:00
package ray
import (
"io"
"net"
"time"
"v2ray.com/core/common/buf"
2018-02-05 17:38:24 -05:00
"v2ray.com/core/common/signal"
2018-01-10 11:31:52 -05:00
)
2018-02-05 17:38:24 -05:00
type ConnectionOption func(*connection)
func ConnLocalAddr(addr net.Addr) ConnectionOption {
return func(c *connection) {
c.localAddr = addr
}
}
func ConnRemoteAddr(addr net.Addr) ConnectionOption {
return func(c *connection) {
c.remoteAddr = addr
}
}
func ConnCloseSignal(s *signal.Notifier) ConnectionOption {
return func(c *connection) {
c.closeSignal = s
}
}
2018-01-10 11:31:52 -05:00
type connection struct {
2018-02-05 17:38:24 -05:00
input InputStream
output OutputStream
closed bool
localAddr net.Addr
remoteAddr net.Addr
closeSignal *signal.Notifier
2018-01-10 11:31:52 -05:00
reader *buf.BufferedReader
}
2018-02-05 17:38:24 -05:00
var zeroAddr net.Addr = &net.TCPAddr{IP: []byte{0, 0, 0, 0}}
2018-01-10 11:31:52 -05:00
// NewConnection wraps a Ray into net.Conn.
2018-02-05 17:38:24 -05:00
func NewConnection(input InputStream, output OutputStream, options ...ConnectionOption) net.Conn {
c := &connection{
input: input,
output: output,
localAddr: zeroAddr,
remoteAddr: zeroAddr,
reader: buf.NewBufferedReader(input),
2018-01-10 11:31:52 -05:00
}
2018-02-05 17:38:24 -05:00
for _, opt := range options {
opt(c)
}
return c
2018-01-10 11:31:52 -05:00
}
// Read implements net.Conn.Read().
func (c *connection) Read(b []byte) (int, error) {
if c.closed {
return 0, io.EOF
}
return c.reader.Read(b)
}
// ReadMultiBuffer implements buf.Reader.
func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
return c.reader.ReadMultiBuffer()
}
// Write implements net.Conn.Write().
func (c *connection) Write(b []byte) (int, error) {
if c.closed {
return 0, io.ErrClosedPipe
}
l := len(b)
mb := buf.NewMultiBufferCap(l/buf.Size + 1)
mb.Write(b)
2018-02-05 17:38:24 -05:00
return l, c.output.WriteMultiBuffer(mb)
2018-01-10 11:31:52 -05:00
}
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
if c.closed {
return io.ErrClosedPipe
}
2018-02-05 17:38:24 -05:00
return c.output.WriteMultiBuffer(mb)
2018-01-10 11:31:52 -05:00
}
// Close implements net.Conn.Close().
func (c *connection) Close() error {
c.closed = true
2018-02-05 17:38:24 -05:00
c.output.Close()
c.input.CloseError()
if c.closeSignal != nil {
c.closeSignal.Signal()
}
2018-01-10 11:31:52 -05:00
return nil
}
// LocalAddr implements net.Conn.LocalAddr().
func (c *connection) LocalAddr() net.Addr {
return c.localAddr
}
// RemoteAddr implements net.Conn.RemoteAddr().
func (c *connection) RemoteAddr() net.Addr {
return c.remoteAddr
}
// SetDeadline implements net.Conn.SetDeadline().
func (c *connection) SetDeadline(t time.Time) error {
return nil
}
// SetReadDeadline implements net.Conn.SetReadDeadline().
func (c *connection) SetReadDeadline(t time.Time) error {
return nil
}
// SetWriteDeadline implement net.Conn.SetWriteDeadline().
func (c *connection) SetWriteDeadline(t time.Time) error {
return nil
}