lontivero / Open.NAT

Lightweight and easy-to-use class library to allow port forwarding in NAT devices with UPNP and/or PMP
MIT License
421 stars 99 forks source link

Catch Exception in 3.5 .Net environment #47

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi lucas thanks for your fgrat jib, you lib is really usefull for my project.

I encountering a problem to get Exception under 3.5 .NET on GUI project

private Task OpenUPNP()
        {
            var nat = new NatDiscoverer();
            var cts = new CancellationTokenSource();
            cts.CancelAfter(5000);
            Device = null;
            IPAddress ip = null;
            return nat.DiscoverDeviceAsync(PortMapper.Upnp, cts)
                .ContinueWith(task =>
                {
                    Device = task.Result;
                    return Device.GetExternalIPAsync();

                })
                .Unwrap()
                .ContinueWith(task =>
                {
                    LocalIP = task.Result;
                    return Device.CreatePortMapAsync(new Mapping(Open.Nat.Protocol.Udp, m_Port, m_Port, 0, "FFSTracker"));
                });
        }
private void InitUPNP(bool flag)
        {
            if (flag)
            {
                if (m_bUPNP) return;
                try
                {
                    OpenUPNP().Wait();
                    Log.LogMessage("P2PManager: Adresse IP = " + LocalIP.ToString(), Color.DarkBlue, 1);
                    Log.LogMessage("P2PManager: Ouverture du port UPNP ok", Color.DarkBlue, 1);
                    m_bUPNP = true;
                }
                catch (NatDeviceNotFoundException e)
                {
                    Log.LogMessage("P2PManager: Aucun routeur compatible UPNP détecté.", Color.DarkViolet);
                    return;
                }
                catch (MappingException me)
                {
                    switch (me.ErrorCode)
                    {
                        case 718:
                            Log.LogMessage("P2PManager: Le port externer est déjà utilisé.", Color.DarkViolet);
                            break;
                        case 728:
                            Log.LogMessage("P2PManager: La taple du routeur est pleine.");
                            break;
                    }
                }
            }
            else
            {
                if ((!m_bUPNP) || (Device==null)) return;
                Task myTask = new Task(() =>
                { 
                    try
                    {
                        Device.DeletePortMapAsync(new Mapping(Open.Nat.Protocol.Udp, m_Port, m_Port));
                        Log.LogMessage("P2PManager: Fermeture du port UPNP ok", Color.Blue, 1);
                    }
                    catch (Open.Nat.MappingException e)
                    {
                        Log.LogMessage("P2PManager: Fermeture du port UPNP impossible: " + e.Message, Color.DarkViolet, 1);
                    }
                });
                myTask.Start();
                myTask.Wait();
                m_bUPNP = false;
            }
        }

The OpenPNP tasks not trigger a catch and give me an unhandled exception crash when for exemple, My router have UPNP disable.

Unfortunatly, I Can't port my code on .NET 4 or more, because I have dependency block me on 3.5

lontivero commented 8 years ago

@Neophile76 the problem is not Open.NAT, it is the way you are implementing async operations in your example. Anyway, i've update the Open.Nat.ConsoleTest/Main.cs in order you can play with it and see how to catch the exceptions.

I hope it is useful for you.

ghost commented 8 years ago

Hello, Thanks you very much, it was very helpfull. Now I success to manage exception correctly.