Exemplo n.º 1
0
func LayerCBDialer(cb DialLayerCallback, d transport.Dialer) transport.Dialer {
	return transport.DialerFunc(func(network, address string) (net.Conn, error) {
		cb.Start()
		c, err := d.Dial(network, address)
		cb.Done(err)
		return c, err
	})
}
Exemplo n.º 2
0
func ConstAddrLayer(addr string, l Layer) Layer {
	return Layer{l.Name, func(d transport.Dialer) (transport.Dialer, error) {
		forward, err := l.Builder(d)
		if err != nil {
			return nil, err
		}

		return transport.DialerFunc(func(network, _ string) (net.Conn, error) {
			return forward.Dial(network, addr)
		}), nil
	}}
}
Exemplo n.º 3
0
func ConstAddrDialer(name, addr string, to time.Duration) NetDialer {
	return NetDialer{name, transport.DialerFunc(func(network, _ string) (net.Conn, error) {
		switch network {
		case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
		default:
			return nil, fmt.Errorf("unsupported network type %v", network)
		}

		dialer := &net.Dialer{Timeout: to}
		return dialer.Dial(network, addr)
	})}
}
Exemplo n.º 4
0
func LayerDeltaCBDialer(
	cb DialLayerCallback,
	dialer transport.Dialer,
	layer func(transport.Dialer) (transport.Dialer, error),
) (transport.DialerFunc, error) {
	starter := transport.DialerFunc(func(network, address string) (net.Conn, error) {
		c, err := dialer.Dial(network, address)
		cb.Start()
		return c, err
	})

	layerInstance, err := layer(starter)
	if err != nil {
		return nil, err
	}

	return func(network, address string) (net.Conn, error) {
		c, err := layerInstance.Dial(network, address)
		cb.Done(err)
		return c, err
	}, nil
}