2016-06-14 16:54:08 -04:00
|
|
|
package tcp
|
2016-04-27 17:01:31 -04:00
|
|
|
|
|
|
|
import (
|
2016-06-14 16:54:08 -04:00
|
|
|
"io"
|
2016-04-27 17:01:31 -04:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2016-06-11 19:30:56 -04:00
|
|
|
)
|
|
|
|
|
2016-05-30 18:21:41 -04:00
|
|
|
type ConnectionManager interface {
|
2016-11-24 17:16:05 -05:00
|
|
|
Put(internal.ConnectionId, net.Conn)
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
type RawConnection struct {
|
|
|
|
net.TCPConn
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RawConnection) Reusable() bool {
|
2016-06-14 16:54:08 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RawConnection) SetReusable(b bool) {}
|
2016-06-14 16:54:08 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RawConnection) SysFd() (int, error) {
|
|
|
|
return internal.GetSysFd(&v.TCPConn)
|
2016-06-29 18:08:20 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 17:53:16 -04:00
|
|
|
type Connection struct {
|
2016-11-24 17:16:05 -05:00
|
|
|
id internal.ConnectionId
|
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-10-02 17:43:58 -04:00
|
|
|
config *Config
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 17:16:05 -05:00
|
|
|
func NewConnection(id internal.ConnectionId, conn net.Conn, manager ConnectionManager, config *Config) *Connection {
|
2016-06-14 16:54:08 -04:00
|
|
|
return &Connection{
|
2016-11-24 17:16:05 -05:00
|
|
|
id: id,
|
2016-06-14 16:54:08 -04:00
|
|
|
conn: conn,
|
|
|
|
listener: manager,
|
2016-10-17 08:35:13 -04:00
|
|
|
reusable: config.ConnectionReuse.IsEnabled(),
|
2016-10-02 17:43:58 -04:00
|
|
|
config: config,
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Read(b []byte) (int, error) {
|
|
|
|
if v == nil || v.conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return 0, io.EOF
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.conn.Read(b)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Write(b []byte) (int, error) {
|
|
|
|
if v == nil || v.conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return 0, io.ErrClosedPipe
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.conn.Write(b)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Close() error {
|
|
|
|
if v == nil || v.conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return io.ErrClosedPipe
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.Reusable() {
|
|
|
|
v.listener.Put(v.id, v.conn)
|
2016-05-29 16:33:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
err := v.conn.Close()
|
|
|
|
v.conn = nil
|
2016-06-14 16:54:08 -04:00
|
|
|
return err
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) LocalAddr() net.Addr {
|
|
|
|
return v.conn.LocalAddr()
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) RemoteAddr() net.Addr {
|
|
|
|
return v.conn.RemoteAddr()
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetDeadline(t time.Time) error {
|
|
|
|
return v.conn.SetDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetReadDeadline(t time.Time) error {
|
|
|
|
return v.conn.SetReadDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetWriteDeadline(t time.Time) error {
|
|
|
|
return v.conn.SetWriteDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2016-05-29 16:33:04 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetReusable(reusable bool) {
|
|
|
|
if !v.config.ConnectionReuse.IsEnabled() {
|
2016-06-14 16:54:08 -04:00
|
|
|
return
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
v.reusable = reusable
|
2016-05-29 16:33:04 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Reusable() bool {
|
|
|
|
return v.reusable
|
2016-05-29 16:33:04 -04:00
|
|
|
}
|
2016-06-11 19:30:56 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SysFd() (int, error) {
|
|
|
|
return internal.GetSysFd(v.conn)
|
2016-06-11 19:30:56 -04:00
|
|
|
}
|