1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/common/net/connection.go

163 lines
3.1 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2018-04-16 18:31:10 -04:00
package net
2018-01-10 11:31:52 -05:00
import (
"io"
"net"
"time"
2018-04-16 18:31:10 -04:00
"v2ray.com/core/common"
2018-01-10 11:31:52 -05:00
"v2ray.com/core/common/buf"
2018-05-27 08:42:53 -04:00
"v2ray.com/core/common/signal/done"
2018-01-10 11:31:52 -05:00
)
2018-02-05 17:38:24 -05:00
type ConnectionOption func(*connection)
2018-04-16 18:31:10 -04:00
func ConnectionLocalAddr(a net.Addr) ConnectionOption {
2018-02-05 17:38:24 -05:00
return func(c *connection) {
2018-04-16 18:31:10 -04:00
c.local = a
2018-02-05 17:38:24 -05:00
}
}
2018-04-16 18:31:10 -04:00
func ConnectionRemoteAddr(a net.Addr) ConnectionOption {
2018-02-05 17:38:24 -05:00
return func(c *connection) {
2018-04-16 18:31:10 -04:00
c.remote = a
2018-02-05 17:38:24 -05:00
}
}
2018-04-16 18:31:10 -04:00
func ConnectionInput(writer io.Writer) ConnectionOption {
2018-02-05 17:38:24 -05:00
return func(c *connection) {
2018-04-16 18:31:10 -04:00
c.writer = buf.NewWriter(writer)
2018-02-05 17:38:24 -05:00
}
}
2018-04-16 18:31:10 -04:00
func ConnectionInputMulti(writer buf.Writer) ConnectionOption {
return func(c *connection) {
c.writer = writer
}
}
2018-01-10 11:31:52 -05:00
2018-04-16 18:31:10 -04:00
func ConnectionOutput(reader io.Reader) ConnectionOption {
return func(c *connection) {
2018-04-20 18:54:53 -04:00
c.reader = &buf.BufferedReader{Reader: buf.NewReader(reader)}
2018-04-16 18:31:10 -04:00
}
}
func ConnectionOutputMulti(reader buf.Reader) ConnectionOption {
return func(c *connection) {
2018-04-20 18:54:53 -04:00
c.reader = &buf.BufferedReader{Reader: reader}
2018-04-16 18:31:10 -04:00
}
2018-01-10 11:31:52 -05:00
}
2019-01-05 18:34:38 -05: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 18:31:10 -04:00
return func(c *connection) {
c.onClose = n
2018-04-16 18:31:10 -04:00
}
}
2018-02-05 17:38:24 -05:00
2018-04-16 18:31:10 -04:00
func NewConnection(opts ...ConnectionOption) net.Conn {
2018-02-05 17:38:24 -05:00
c := &connection{
2018-05-27 08:42:53 -04: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 11:31:52 -05:00
}
2018-02-05 17:38:24 -05:00
2018-04-16 18:31:10 -04:00
for _, opt := range opts {
2018-02-05 17:38:24 -05:00
opt(c)
}
return c
2018-01-10 11:31:52 -05:00
}
2018-04-16 18:31:10 -04:00
type connection struct {
reader *buf.BufferedReader
writer buf.Writer
2018-05-27 08:42:53 -04:00
done *done.Instance
onClose io.Closer
2018-04-16 18:31:10 -04:00
local Addr
remote Addr
}
2018-01-10 11:31:52 -05: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 18:31:10 -04:00
if c.done.Done() {
2018-01-10 11:31:52 -05:00
return 0, io.ErrClosedPipe
}
l := len(b)
2018-11-16 05:29:16 -05:00
mb := make(buf.MultiBuffer, 0, l/buf.Size+1)
2018-11-18 13:36:36 -05:00
mb = buf.MergeBytes(mb, b)
2018-04-16 18:31:10 -04:00
return l, c.writer.WriteMultiBuffer(mb)
2018-01-10 11:31:52 -05:00
}
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-04-16 18:31:10 -04:00
if c.done.Done() {
2019-02-20 16:55:57 -05:00
buf.ReleaseMulti(mb)
2018-01-10 11:31:52 -05:00
return io.ErrClosedPipe
}
2018-04-16 18:31:10 -04:00
return c.writer.WriteMultiBuffer(mb)
2018-01-10 11:31:52 -05:00
}
// Close implements net.Conn.Close().
func (c *connection) Close() error {
2018-04-16 18:31:10 -04:00
common.Must(c.done.Close())
2019-01-03 18:24:28 -05:00
common.Interrupt(c.reader)
2018-04-16 18:31:10 -04:00
common.Close(c.writer)
if c.onClose != nil {
return c.onClose.Close()
2018-02-05 17:38:24 -05:00
}
2018-04-16 18:31:10 -04:00
2018-01-10 11:31:52 -05:00
return nil
}
// LocalAddr implements net.Conn.LocalAddr().
func (c *connection) LocalAddr() net.Addr {
2018-04-16 18:31:10 -04:00
return c.local
2018-01-10 11:31:52 -05:00
}
// RemoteAddr implements net.Conn.RemoteAddr().
func (c *connection) RemoteAddr() net.Addr {
2018-04-16 18:31:10 -04:00
return c.remote
2018-01-10 11:31:52 -05: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 05:11:54 -04:00
// SetWriteDeadline implements net.Conn.SetWriteDeadline().
2018-01-10 11:31:52 -05:00
func (c *connection) SetWriteDeadline(t time.Time) error {
return nil
}