v2ray-core/transport/hub/connection.go

79 lines
1.4 KiB
Go
Raw Normal View History

2016-04-27 21:01:31 +00:00
package hub
import (
"net"
"time"
)
2016-05-02 21:53:16 +00:00
type ConnectionHandler func(*Connection)
2016-04-27 21:01:31 +00:00
2016-05-02 21:53:16 +00:00
type Connection struct {
conn net.Conn
2016-04-27 21:01:31 +00:00
listener *TCPHub
2016-05-29 20:33:04 +00:00
reusable bool
2016-04-27 21:01:31 +00:00
}
2016-05-02 21:53:16 +00:00
func (this *Connection) Read(b []byte) (int, error) {
2016-04-27 21:01:31 +00:00
if this == nil || this.conn == nil {
return 0, ErrorClosedConnection
}
return this.conn.Read(b)
}
2016-05-02 21:53:16 +00:00
func (this *Connection) Write(b []byte) (int, error) {
2016-04-27 21:01:31 +00:00
if this == nil || this.conn == nil {
return 0, ErrorClosedConnection
}
return this.conn.Write(b)
}
2016-05-02 21:53:16 +00:00
func (this *Connection) Close() error {
2016-04-27 21:01:31 +00:00
if this == nil || this.conn == nil {
return ErrorClosedConnection
}
2016-05-29 20:33:04 +00:00
if this.Reusable() {
this.listener.Recycle(this.conn)
return nil
}
return this.conn.Close()
2016-04-27 21:01:31 +00:00
}
2016-05-02 21:53:16 +00:00
func (this *Connection) Release() {
2016-04-27 21:01:31 +00:00
if this == nil || this.listener == nil {
return
}
this.Close()
this.conn = nil
this.listener = nil
}
2016-05-02 21:53:16 +00:00
func (this *Connection) LocalAddr() net.Addr {
2016-04-27 21:01:31 +00:00
return this.conn.LocalAddr()
}
2016-05-02 21:53:16 +00:00
func (this *Connection) RemoteAddr() net.Addr {
2016-04-27 21:01:31 +00:00
return this.conn.RemoteAddr()
}
2016-05-02 21:53:16 +00:00
func (this *Connection) SetDeadline(t time.Time) error {
2016-04-27 21:01:31 +00:00
return this.conn.SetDeadline(t)
}
2016-05-02 21:53:16 +00:00
func (this *Connection) SetReadDeadline(t time.Time) error {
2016-04-27 21:01:31 +00:00
return this.conn.SetReadDeadline(t)
}
2016-05-02 21:53:16 +00:00
func (this *Connection) SetWriteDeadline(t time.Time) error {
2016-04-27 21:01:31 +00:00
return this.conn.SetWriteDeadline(t)
}
2016-05-29 20:33:04 +00:00
func (this *Connection) SetReusable(reusable bool) {
this.reusable = reusable
}
func (this *Connection) Reusable() bool {
return this.reusable
}