v2fly/common/net/connection.go

166 lines
3.3 KiB
Go
Raw Normal View History

2018-04-16 22:31:10 +00:00
package net
2018-01-10 16:31:52 +00:00
import (
"io"
"net"
"time"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/errors"
"github.com/v2fly/v2ray-core/v4/common/signal/done"
2018-01-10 16:31:52 +00:00
)
2018-02-05 22:38:24 +00:00
type ConnectionOption func(*connection)
2018-04-16 22:31:10 +00:00
func ConnectionLocalAddr(a net.Addr) ConnectionOption {
2018-02-05 22:38:24 +00:00
return func(c *connection) {
2018-04-16 22:31:10 +00:00
c.local = a
2018-02-05 22:38:24 +00:00
}
}
2018-04-16 22:31:10 +00:00
func ConnectionRemoteAddr(a net.Addr) ConnectionOption {
2018-02-05 22:38:24 +00:00
return func(c *connection) {
2018-04-16 22:31:10 +00:00
c.remote = a
2018-02-05 22:38:24 +00:00
}
}
2018-04-16 22:31:10 +00:00
func ConnectionInput(writer io.Writer) ConnectionOption {
2018-02-05 22:38:24 +00:00
return func(c *connection) {
2018-04-16 22:31:10 +00:00
c.writer = buf.NewWriter(writer)
2018-02-05 22:38:24 +00:00
}
}
2018-04-16 22:31:10 +00:00
func ConnectionInputMulti(writer buf.Writer) ConnectionOption {
return func(c *connection) {
c.writer = writer
}
}
2018-01-10 16:31:52 +00:00
2018-04-16 22:31:10 +00:00
func ConnectionOutput(reader io.Reader) ConnectionOption {
return func(c *connection) {
2018-04-20 22:54:53 +00:00
c.reader = &buf.BufferedReader{Reader: buf.NewReader(reader)}
2018-04-16 22:31:10 +00:00
}
}
func ConnectionOutputMulti(reader buf.Reader) ConnectionOption {
return func(c *connection) {
2018-04-20 22:54:53 +00:00
c.reader = &buf.BufferedReader{Reader: reader}
2018-04-16 22:31:10 +00:00
}
2018-01-10 16:31:52 +00:00
}
2019-01-05 23:34:38 +00:00
func ConnectionOutputMultiUDP(reader buf.Reader) ConnectionOption {
return func(c *connection) {
c.reader = &buf.BufferedReader{
Reader: reader,
Spliter: buf.SplitFirstBytes,
}
}
}
func ConnectionOnClose(n io.Closer) ConnectionOption {
2018-04-16 22:31:10 +00:00
return func(c *connection) {
c.onClose = n
2018-04-16 22:31:10 +00:00
}
}
2018-02-05 22:38:24 +00:00
2018-04-16 22:31:10 +00:00
func NewConnection(opts ...ConnectionOption) net.Conn {
2018-02-05 22:38:24 +00:00
c := &connection{
2018-05-27 12:42:53 +00:00
done: done.New(),
local: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
remote: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
2018-01-10 16:31:52 +00:00
}
2018-02-05 22:38:24 +00:00
2018-04-16 22:31:10 +00:00
for _, opt := range opts {
2018-02-05 22:38:24 +00:00
opt(c)
}
return c
2018-01-10 16:31:52 +00:00
}
2018-04-16 22:31:10 +00:00
type connection struct {
reader *buf.BufferedReader
writer buf.Writer
2018-05-27 12:42:53 +00:00
done *done.Instance
onClose io.Closer
2018-04-16 22:31:10 +00:00
local Addr
remote Addr
}
2018-01-10 16:31:52 +00:00
func (c *connection) Read(b []byte) (int, error) {
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) {
2018-04-16 22:31:10 +00:00
if c.done.Done() {
2018-01-10 16:31:52 +00:00
return 0, io.ErrClosedPipe
}
if len(b)/buf.Size+1 > 64*1024*1024 {
return 0, errors.New("value too large")
}
2018-01-10 16:31:52 +00:00
l := len(b)
sliceSize := l/buf.Size + 1
mb := make(buf.MultiBuffer, 0, sliceSize)
2018-11-18 18:36:36 +00:00
mb = buf.MergeBytes(mb, b)
2018-04-16 22:31:10 +00:00
return l, c.writer.WriteMultiBuffer(mb)
2018-01-10 16:31:52 +00:00
}
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-04-16 22:31:10 +00:00
if c.done.Done() {
2019-02-20 21:55:57 +00:00
buf.ReleaseMulti(mb)
2018-01-10 16:31:52 +00:00
return io.ErrClosedPipe
}
2018-04-16 22:31:10 +00:00
return c.writer.WriteMultiBuffer(mb)
2018-01-10 16:31:52 +00:00
}
// Close implements net.Conn.Close().
func (c *connection) Close() error {
2018-04-16 22:31:10 +00:00
common.Must(c.done.Close())
2019-01-03 23:24:28 +00:00
common.Interrupt(c.reader)
2018-04-16 22:31:10 +00:00
common.Close(c.writer)
if c.onClose != nil {
return c.onClose.Close()
2018-02-05 22:38:24 +00:00
}
2018-04-16 22:31:10 +00:00
2018-01-10 16:31:52 +00:00
return nil
}
// LocalAddr implements net.Conn.LocalAddr().
func (c *connection) LocalAddr() net.Addr {
2018-04-16 22:31:10 +00:00
return c.local
2018-01-10 16:31:52 +00:00
}
// RemoteAddr implements net.Conn.RemoteAddr().
func (c *connection) RemoteAddr() net.Addr {
2018-04-16 22:31:10 +00:00
return c.remote
2018-01-10 16:31:52 +00:00
}
// 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
}
2018-04-03 09:11:54 +00:00
// SetWriteDeadline implements net.Conn.SetWriteDeadline().
2018-01-10 16:31:52 +00:00
func (c *connection) SetWriteDeadline(t time.Time) error {
return nil
}