1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-06 17:36:40 -05:00
v2fly/transport/internet/tcp_hub.go

95 lines
2.1 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package internet
import (
"net"
"sync"
2017-01-03 08:53:59 -05:00
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2016-11-20 15:47:51 -05:00
"v2ray.com/core/common/retry"
2016-06-14 16:54:08 -04:00
)
var (
2016-06-27 02:53:35 -04:00
ErrClosedConnection = errors.New("Connection already closed.")
2016-06-14 16:54:08 -04:00
2017-01-03 09:16:48 -05:00
networkListenerCache = make(map[v2net.Network]ListenFunc)
2016-06-14 16:54:08 -04:00
)
2017-01-03 09:16:48 -05:00
func RegisterNetworkListener(network v2net.Network, listener ListenFunc) error {
if _, found := networkListenerCache[network]; found {
return errors.New("Internet|TCPHub: ", network, " listener already registered.")
}
networkListenerCache[network] = listener
return nil
}
2016-09-30 10:53:40 -04:00
type ListenFunc func(address v2net.Address, port v2net.Port, options ListenOptions) (Listener, error)
type ListenOptions struct {
2016-10-02 17:43:58 -04:00
Stream *StreamConfig
2016-09-30 10:53:40 -04:00
}
2016-06-14 16:54:08 -04:00
type Listener interface {
Accept() (Connection, error)
Close() error
Addr() net.Addr
}
type TCPHub struct {
sync.Mutex
listener Listener
connCallback ConnectionHandler
accepting bool
}
2016-10-02 17:43:58 -04:00
func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandler, settings *StreamConfig) (*TCPHub, error) {
2016-09-30 10:53:40 -04:00
options := ListenOptions{
Stream: settings,
}
2017-01-03 09:16:48 -05:00
listenFunc := networkListenerCache[settings.Network]
if listenFunc == nil {
return nil, errors.New("Internet|TCPHub: ", settings.Network, " listener not registered.")
2016-06-14 16:54:08 -04:00
}
2017-01-03 09:16:48 -05:00
listener, err := listenFunc(address, port, options)
2016-06-14 16:54:08 -04:00
if err != nil {
2017-01-03 09:16:48 -05:00
return nil, errors.Base(err).Message("Interent|TCPHub: Failed to listen: ")
2016-06-14 16:54:08 -04:00
}
hub := &TCPHub{
listener: listener,
connCallback: callback,
}
go hub.start()
return hub, nil
}
2016-11-27 15:39:09 -05:00
func (v *TCPHub) Close() {
v.accepting = false
v.listener.Close()
2016-06-14 16:54:08 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *TCPHub) start() {
v.accepting = true
for v.accepting {
2016-11-20 15:47:51 -05:00
var newConn Connection
err := retry.ExponentialBackoff(10, 200).On(func() error {
2016-11-27 15:39:09 -05:00
if !v.accepting {
2016-11-20 15:47:51 -05:00
return nil
}
2016-11-27 15:39:09 -05:00
conn, err := v.listener.Accept()
2016-11-20 15:47:51 -05:00
if err != nil {
2016-11-27 15:39:09 -05:00
if v.accepting {
2016-11-20 15:47:51 -05:00
log.Warning("Internet|Listener: Failed to accept new TCP connection: ", err)
}
return err
2016-06-14 16:54:08 -04:00
}
2016-11-20 15:47:51 -05:00
newConn = conn
return nil
})
if err == nil && newConn != nil {
2016-11-27 15:39:09 -05:00
go v.connCallback(newConn)
2016-06-14 16:54:08 -04:00
}
}
}