1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 11:17:44 -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 (
transportListenerCache = make(map[TransportProtocol]ListenFunc)
2016-06-14 16:54:08 -04:00
)
func RegisterTransportListener(protocol TransportProtocol, listener ListenFunc) error {
if _, found := transportListenerCache[protocol]; found {
return errors.New("Internet|TCPHub: ", protocol, " listener already registered.")
2017-01-03 09:16:48 -05:00
}
transportListenerCache[protocol] = listener
2017-01-03 09:16:48 -05:00
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 {
Stream *StreamConfig
RecvOrigDest bool
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,
}
protocol := settings.GetEffectiveProtocol()
listenFunc := transportListenerCache[protocol]
2017-01-03 09:16:48 -05:00
if listenFunc == nil {
return nil, errors.New("Internet|TCPHub: ", protocol, " 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 {
return nil, errors.Base(err).Message("Interent|TCPHub: Failed to listen on address: ", address, ":", port)
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
}
}
}