1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 14:26:11 -04:00
v2fly/transport/internet/tcp_hub.go
2018-09-07 14:50:25 +02:00

43 lines
1.1 KiB
Go

package internet
import (
"context"
"v2ray.com/core/common/net"
)
var (
transportListenerCache = make(map[string]ListenFunc)
)
func RegisterTransportListener(protocol string, listener ListenFunc) error {
if _, found := transportListenerCache[protocol]; found {
return newError(protocol, " listener already registered.").AtError()
}
transportListenerCache[protocol] = listener
return nil
}
type ConnHandler func(Connection)
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error)
type Listener interface {
Close() error
Addr() net.Addr
}
func ListenTCP(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error) {
settings := StreamSettingsFromContext(ctx)
protocol := settings.ProtocolName
listenFunc := transportListenerCache[protocol]
if listenFunc == nil {
return nil, newError(protocol, " listener not registered.").AtError()
}
listener, err := listenFunc(ctx, address, port, handler)
if err != nil {
return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
}
return listener, nil
}