mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.2 KiB
117 lines
2.2 KiB
9 years ago
|
package tcp
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"errors"
|
||
9 years ago
|
"io"
|
||
9 years ago
|
"net"
|
||
9 years ago
|
"reflect"
|
||
9 years ago
|
"time"
|
||
|
)
|
||
|
|
||
9 years ago
|
var (
|
||
|
ErrInvalidConn = errors.New("Invalid Connection.")
|
||
|
)
|
||
|
|
||
9 years ago
|
type ConnectionManager interface {
|
||
|
Recycle(string, net.Conn)
|
||
|
}
|
||
|
|
||
9 years ago
|
type RawConnection struct {
|
||
|
net.TCPConn
|
||
|
}
|
||
|
|
||
|
func (this *RawConnection) Reusable() bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (this *RawConnection) SetReusable(b bool) {}
|
||
|
|
||
9 years ago
|
type Connection struct {
|
||
9 years ago
|
dest string
|
||
9 years ago
|
conn net.Conn
|
||
9 years ago
|
listener ConnectionManager
|
||
9 years ago
|
reusable bool
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func NewConnection(dest string, conn net.Conn, manager ConnectionManager) *Connection {
|
||
|
return &Connection{
|
||
|
dest: dest,
|
||
|
conn: conn,
|
||
|
listener: manager,
|
||
|
reusable: effectiveConfig.ConnectionReuse,
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) Read(b []byte) (int, error) {
|
||
9 years ago
|
if this == nil || this.conn == nil {
|
||
9 years ago
|
return 0, io.EOF
|
||
9 years ago
|
}
|
||
|
|
||
|
return this.conn.Read(b)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) Write(b []byte) (int, error) {
|
||
9 years ago
|
if this == nil || this.conn == nil {
|
||
9 years ago
|
return 0, io.ErrClosedPipe
|
||
9 years ago
|
}
|
||
|
return this.conn.Write(b)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) Close() error {
|
||
9 years ago
|
if this == nil || this.conn == nil {
|
||
9 years ago
|
return io.ErrClosedPipe
|
||
9 years ago
|
}
|
||
9 years ago
|
if this.Reusable() {
|
||
9 years ago
|
this.listener.Recycle(this.dest, this.conn)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
9 years ago
|
err := this.conn.Close()
|
||
|
this.conn = nil
|
||
|
return err
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) LocalAddr() net.Addr {
|
||
9 years ago
|
return this.conn.LocalAddr()
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) RemoteAddr() net.Addr {
|
||
9 years ago
|
return this.conn.RemoteAddr()
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) SetDeadline(t time.Time) error {
|
||
9 years ago
|
return this.conn.SetDeadline(t)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) SetReadDeadline(t time.Time) error {
|
||
9 years ago
|
return this.conn.SetReadDeadline(t)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *Connection) SetWriteDeadline(t time.Time) error {
|
||
9 years ago
|
return this.conn.SetWriteDeadline(t)
|
||
|
}
|
||
9 years ago
|
|
||
|
func (this *Connection) SetReusable(reusable bool) {
|
||
9 years ago
|
if !effectiveConfig.ConnectionReuse {
|
||
|
return
|
||
|
}
|
||
9 years ago
|
this.reusable = reusable
|
||
|
}
|
||
|
|
||
|
func (this *Connection) Reusable() bool {
|
||
|
return this.reusable
|
||
|
}
|
||
9 years ago
|
|
||
|
func (this *Connection) SysFd() (int, error) {
|
||
|
cv := reflect.ValueOf(this.conn)
|
||
|
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
|
||
|
}
|