1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-19 03:34:31 -04:00
v2fly/transport/hub/tcp.go

64 lines
1.1 KiB
Go
Raw Normal View History

2016-01-28 11:08:32 -05:00
package hub
import (
"errors"
"net"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
ErrorClosedConnection = errors.New("Connection already closed.")
)
2016-01-28 14:47:00 -05:00
type TCPHub struct {
listener *net.TCPListener
2016-04-27 17:01:31 -04:00
connCallback ConnectionHandler
accepting bool
}
2016-04-27 17:01:31 -04:00
func ListenTCP(port v2net.Port, callback ConnectionHandler) (*TCPHub, error) {
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{
listener: listener,
connCallback: callback,
}
go tcpListener.start()
return tcpListener, nil
}
2016-01-28 14:47:00 -05:00
func (this *TCPHub) Close() {
this.accepting = false
this.listener.Close()
this.listener = nil
}
2016-01-28 14:47:00 -05:00
func (this *TCPHub) start() {
this.accepting = true
for this.accepting {
conn, err := this.listener.AcceptTCP()
if err != nil {
if this.accepting {
log.Warning("Listener: Failed to accept new TCP connection: ", err)
}
continue
}
2016-04-27 17:01:31 -04:00
go this.connCallback(&TCPConnection{
conn: conn,
listener: this,
})
}
}
2016-01-28 14:47:00 -05:00
func (this *TCPHub) recycle(conn *net.TCPConn) {
}