示例#1
0
        public void StartListen(Action <int> readyCallback)
        {
            _dcc                  = new DccChat();
            _dcc.Connected       += dcc_Connected;
            _dcc.Disconnected    += dcc_Disconnected;
            _dcc.Error           += dcc_Error;
            _dcc.MessageReceived += dcc_MessageReceived;

            try
            {
                _port = _dcc.Listen(App.Settings.Current.Dcc.LowPort, App.Settings.Current.Dcc.HighPort);
            }
            catch (InvalidOperationException)
            {
                this.Write("Error", "No available ports.");
                _port = 0;
            }

            if (App.Settings.Current.Dcc.EnableUpnp && NatHelper.IsAvailable)
            {
                this.Write("Client", "Forwarding port...");
                NatHelper.BeginAddForwardingRule(_port, System.Net.Sockets.ProtocolType.Tcp, "Floe DCC", (o) =>
                {
                    this.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.Write("Client", "Waiting for connection...");
                        readyCallback(_port);
                    }));
                });
                _isPortForwarding = true;
            }
            else
            {
                this.Write("Client", "Waiting for connection...");
                readyCallback(_port);
            }
        }