Exemplo n.º 1
0
 private void DoConnect(EndPoint address)
 {
     ReportConnectFailure(() =>
     {
         _channel = DatagramChannel.Open();
         _channel.ConfigureBlocking(false);
         var socket = _channel.Socket;
         _connect.Options.ForEach(x => x.BeforeDatagramBind(socket));
         if (_connect.LocalAddress != null)
         {
             socket.Bind(_connect.LocalAddress);
         }
         _channel.Connect(_connect.RemoteAddress);
         _channelRegistry.Register(_channel, SocketAsyncOperation.Receive, Self);
     });
     _log.Debug("Successfully connected to [{0}]", _connect.RemoteAddress);
 }
Exemplo n.º 2
0
        public UdpListener(UdpExt udp, IChannelRegistry channelRegistry, IActorRef bindCommander, Udp.Bind bind)
        {
            _udp             = udp;
            _channelRegistry = channelRegistry;
            _bindCommander   = bindCommander;
            _bind            = bind;

            _selector = Context.Parent;

            Context.Watch(bind.Handler);        // sign death pact

            _channel = (bind.Options.OfType <Inet.DatagramChannelCreator>()
                        .FirstOrDefault() ?? new Inet.DatagramChannelCreator()).Create();
            _channel.ConfigureBlocking(false);

            var localAddress = new Func <object>(() =>
            {
                try
                {
                    var socket = Channel.Socket;
                    bind.Options.ForEach(x => x.BeforeDatagramBind(socket));
                    socket.Bind(bind.LocalAddress);
                    var ret = socket.LocalEndPoint;
                    if (ret == null)
                    {
                        throw new ArgumentException(string.Format("bound to unknown SocketAddress [{0}]", socket.LocalEndPoint));
                    }
                    channelRegistry.Register(Channel, SocketAsyncOperation.Receive, Self);
                    _log.Debug("Successfully bound to [{0}]", ret);
                    bind.Options.OfType <Inet.SocketOptionV2>().ForEach(x => x.AfterBind(socket));
                    return(ret);
                }
                catch (Exception e)
                {
                    bindCommander.Tell(new Udp.CommandFailed(bind));
                    _log.Error(e, "Failed to bind UDP channel to endpoint [{0}]", bind.LocalAddress);
                    Context.Stop(Self);
                    return(null);
                }
            })();
        }