bind method

Future<StatsdConnection> bind (
  1. String address,
  2. int port
)

Implementation

static Future<StatsdConnection> bind(String address, int port) {
  var completer = new Completer<StatsdConnection>();

  InternetAddress.lookup(address).then((_) {
    var address = _.first;
    _logger.info('Internet address lookup succeeded. Using: ${address}.');
    RawDatagramSocket.bind(InternetAddress.anyIPv4, 0).then((socket) {
      _logger.info('Connected to port ${socket.port}.');
      completer.complete(new StatsdUdpConnection._(address, port, socket));
    }, onError: (e, stackTrace) {
      _logger.warning('Error binding to a port. Error: ${e}', e, stackTrace);
      completer.complete(new StatsdUdpConnection._(address, port, null));
    });
  }, onError: (e, stackTrace) {
    _logger.warning(
        'Internet address lookup failed. Error: ${e}.', e, stackTrace);
    completer.complete(new StatsdUdpConnection._(null, port, null));
  });

  return completer.future;
}