2016-01-28 11:08:32 -05:00
|
|
|
package hub
|
2016-01-27 16:11:31 -05:00
|
|
|
|
|
|
|
import (
|
2016-03-09 05:34:27 -05:00
|
|
|
"errors"
|
2016-01-27 16:11:31 -05:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
)
|
|
|
|
|
2016-03-09 05:34:27 -05:00
|
|
|
var (
|
|
|
|
ErrorClosedConnection = errors.New("Connection already closed.")
|
|
|
|
)
|
|
|
|
|
2016-01-27 16:11:31 -05:00
|
|
|
type TCPConn struct {
|
|
|
|
conn *net.TCPConn
|
2016-01-28 14:47:00 -05:00
|
|
|
listener *TCPHub
|
2016-01-27 16:11:31 -05:00
|
|
|
dirty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) Read(b []byte) (int, error) {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.conn == nil {
|
|
|
|
return 0, ErrorClosedConnection
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
return this.conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) Write(b []byte) (int, error) {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.conn == nil {
|
|
|
|
return 0, ErrorClosedConnection
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
return this.conn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) Close() error {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.conn == nil {
|
|
|
|
return ErrorClosedConnection
|
|
|
|
}
|
|
|
|
err := this.conn.Close()
|
|
|
|
this.conn = nil
|
|
|
|
this.listener = nil
|
|
|
|
return err
|
2016-01-27 16:11:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) Release() {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.listener == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:11:31 -05:00
|
|
|
if this.dirty {
|
|
|
|
this.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.listener.recycle(this.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) LocalAddr() net.Addr {
|
|
|
|
return this.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) RemoteAddr() net.Addr {
|
|
|
|
return this.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) SetDeadline(t time.Time) error {
|
|
|
|
return this.conn.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return this.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return this.conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) CloseRead() error {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
return this.conn.CloseRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPConn) CloseWrite() error {
|
2016-03-09 05:34:27 -05:00
|
|
|
if this == nil || this.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
return this.conn.CloseWrite()
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:47:00 -05:00
|
|
|
type TCPHub struct {
|
2016-01-27 16:11:31 -05:00
|
|
|
listener *net.TCPListener
|
|
|
|
connCallback func(*TCPConn)
|
|
|
|
accepting bool
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:47:00 -05:00
|
|
|
func ListenTCP(port v2net.Port, callback func(*TCPConn)) (*TCPHub, error) {
|
2016-01-27 16:11:31 -05:00
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
|
IP: []byte{0, 0, 0, 0},
|
|
|
|
Port: int(port),
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-28 14:47:00 -05:00
|
|
|
tcpListener := &TCPHub{
|
2016-01-27 16:11:31 -05:00
|
|
|
listener: listener,
|
|
|
|
connCallback: callback,
|
|
|
|
}
|
|
|
|
go tcpListener.start()
|
|
|
|
return tcpListener, nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:47:00 -05:00
|
|
|
func (this *TCPHub) Close() {
|
2016-01-27 16:11:31 -05:00
|
|
|
this.accepting = false
|
|
|
|
this.listener.Close()
|
2016-03-09 05:34:27 -05:00
|
|
|
this.listener = nil
|
2016-01-27 16:11:31 -05:00
|
|
|
}
|
|
|
|
|
2016-01-28 14:47:00 -05:00
|
|
|
func (this *TCPHub) start() {
|
2016-01-27 16:11:31 -05:00
|
|
|
this.accepting = true
|
|
|
|
for this.accepting {
|
|
|
|
conn, err := this.listener.AcceptTCP()
|
|
|
|
if err != nil {
|
2016-02-18 05:36:21 -05:00
|
|
|
if this.accepting {
|
|
|
|
log.Warning("Listener: Failed to accept new TCP connection: ", err)
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
go this.connCallback(&TCPConn{
|
|
|
|
conn: conn,
|
|
|
|
listener: this,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:47:00 -05:00
|
|
|
func (this *TCPHub) recycle(conn *net.TCPConn) {
|
2016-01-27 16:11:31 -05:00
|
|
|
|
|
|
|
}
|