1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-01 16:57:46 -04:00
v2fly/transport/hub/connection.go

79 lines
1.4 KiB
Go
Raw Normal View History

2016-04-27 17:01:31 -04:00
package hub
import (
"net"
"time"
)
2016-05-02 17:53:16 -04:00
type ConnectionHandler func(*Connection)
2016-04-27 17:01:31 -04:00
2016-05-02 17:53:16 -04:00
type Connection struct {
conn net.Conn
2016-04-27 17:01:31 -04:00
listener *TCPHub
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-05-29 16:33:04 -04:00
if this.Reusable() {
this.listener.Recycle(this.conn)
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) Release() {
2016-04-27 17:01:31 -04:00
if this == nil || this.listener == nil {
return
}
this.Close()
this.conn = nil
this.listener = nil
}
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
}