1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/tcp/connection.go

125 lines
2.3 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package tcp
2016-04-27 17:01:31 -04:00
import (
2016-06-11 19:30:56 -04:00
"errors"
2016-06-14 16:54:08 -04:00
"io"
2016-04-27 17:01:31 -04:00
"net"
2016-06-11 19:30:56 -04:00
"reflect"
2016-04-27 17:01:31 -04:00
"time"
)
2016-06-11 19:30:56 -04:00
var (
ErrInvalidConn = errors.New("Invalid Connection.")
)
2016-05-30 18:21:41 -04:00
type ConnectionManager interface {
Recycle(string, net.Conn)
}
2016-06-14 16:54:08 -04:00
type RawConnection struct {
net.TCPConn
}
func (this *RawConnection) Reusable() bool {
return false
}
func (this *RawConnection) SetReusable(b bool) {}
2016-06-29 18:08:20 -04:00
func (this *RawConnection) SysFd() (int, error) {
return getSysFd(&this.TCPConn)
}
2016-05-02 17:53:16 -04:00
type Connection struct {
2016-05-30 18:21:41 -04:00
dest string
2016-05-02 17:53:16 -04:00
conn net.Conn
2016-05-30 18:21:41 -04:00
listener ConnectionManager
2016-05-29 16:33:04 -04:00
reusable bool
2016-04-27 17:01:31 -04:00
}
2016-06-14 16:54:08 -04:00
func NewConnection(dest string, conn net.Conn, manager ConnectionManager) *Connection {
return &Connection{
dest: dest,
conn: conn,
listener: manager,
reusable: effectiveConfig.ConnectionReuse,
}
}
2016-05-02 17:53:16 -04:00
func (this *Connection) Read(b []byte) (int, error) {
2016-04-27 17:01:31 -04:00
if this == nil || this.conn == nil {
2016-06-14 16:54:08 -04:00
return 0, io.EOF
2016-04-27 17:01:31 -04:00
}
return this.conn.Read(b)
}
2016-05-02 17:53:16 -04:00
func (this *Connection) Write(b []byte) (int, error) {
2016-04-27 17:01:31 -04:00
if this == nil || this.conn == nil {
2016-06-14 16:54:08 -04:00
return 0, io.ErrClosedPipe
2016-04-27 17:01:31 -04:00
}
return this.conn.Write(b)
}
2016-05-02 17:53:16 -04:00
func (this *Connection) Close() error {
2016-04-27 17:01:31 -04:00
if this == nil || this.conn == nil {
2016-06-14 16:54:08 -04:00
return io.ErrClosedPipe
2016-04-27 17:01:31 -04:00
}
2016-06-14 16:54:08 -04:00
if this.Reusable() {
2016-05-30 18:21:41 -04:00
this.listener.Recycle(this.dest, this.conn)
2016-05-29 16:33:04 -04:00
return nil
}
2016-06-14 16:54:08 -04:00
err := this.conn.Close()
this.conn = nil
return err
2016-04-27 17:01:31 -04:00
}
2016-05-02 17:53:16 -04:00
func (this *Connection) LocalAddr() net.Addr {
2016-04-27 17:01:31 -04:00
return this.conn.LocalAddr()
}
2016-05-02 17:53:16 -04:00
func (this *Connection) RemoteAddr() net.Addr {
2016-04-27 17:01:31 -04:00
return this.conn.RemoteAddr()
}
2016-05-02 17:53:16 -04:00
func (this *Connection) SetDeadline(t time.Time) error {
2016-04-27 17:01:31 -04:00
return this.conn.SetDeadline(t)
}
2016-05-02 17:53:16 -04:00
func (this *Connection) SetReadDeadline(t time.Time) error {
2016-04-27 17:01:31 -04:00
return this.conn.SetReadDeadline(t)
}
2016-05-02 17:53:16 -04:00
func (this *Connection) SetWriteDeadline(t time.Time) error {
2016-04-27 17:01:31 -04:00
return this.conn.SetWriteDeadline(t)
}
2016-05-29 16:33:04 -04:00
func (this *Connection) SetReusable(reusable bool) {
2016-06-14 16:54:08 -04:00
if !effectiveConfig.ConnectionReuse {
return
}
2016-05-29 16:33:04 -04:00
this.reusable = reusable
}
func (this *Connection) Reusable() bool {
return this.reusable
}
2016-06-11 19:30:56 -04:00
func (this *Connection) SysFd() (int, error) {
2016-06-29 18:08:20 -04:00
return getSysFd(this.conn)
}
func getSysFd(conn net.Conn) (int, error) {
cv := reflect.ValueOf(conn)
2016-06-11 19:30:56 -04:00
switch ce := cv.Elem(); ce.Kind() {
case reflect.Struct:
netfd := ce.FieldByName("conn").FieldByName("fd")
switch fe := netfd.Elem(); fe.Kind() {
case reflect.Struct:
fd := fe.FieldByName("sysfd")
return int(fd.Int()), nil
}
}
return 0, ErrInvalidConn
}