2016-04-27 17:01:31 -04:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
2016-06-01 19:49:25 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/transport"
|
2016-04-27 17:01:31 -04:00
|
|
|
)
|
|
|
|
|
2016-05-02 17:53:16 -04:00
|
|
|
type ConnectionHandler func(*Connection)
|
2016-04-27 17:01:31 -04:00
|
|
|
|
2016-05-30 18:21:41 -04:00
|
|
|
type ConnectionManager interface {
|
|
|
|
Recycle(string, net.Conn)
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
return 0, ErrorClosedConnection
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return 0, ErrorClosedConnection
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return ErrorClosedConnection
|
|
|
|
}
|
2016-06-02 14:52:52 -04:00
|
|
|
if transport.IsConnectionReusable() && 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
|
|
|
|
}
|
|
|
|
return this.conn.Close()
|
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) {
|
|
|
|
this.reusable = reusable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Connection) Reusable() bool {
|
|
|
|
return this.reusable
|
|
|
|
}
|