Ticket #2338: fix-trac-2338-style2_v2.patch

File fix-trac-2338-style2_v2.patch, 2.3 KB (added by Echelon9, 10 years ago)

Revised version of style2 patch (post code review comments)

  • source/network/NetServer.cpp

     
    225225    if (!rootDescURL.empty())
    226226        LOGMESSAGE(L"Net server: attempting to use cached root descriptor URL: %hs", rootDescURL.c_str());
    227227
    228     // Init the return variable for UPNP_GetValidIGD to 1 so things behave when using cached URLs.
    229     int ret = 1;
     228    // Init the return variable for UPNP_GetValidIGD to 0.
     229    int ret = 0;
     230    bool bAllocatedDescriptorURL = false;
    230231
    231232    // If we have a cached URL, try that first, otherwise try getting a valid UPnP device for 10 seconds. We also get our LAN address here.
    232     if (!((!rootDescURL.empty() && UPNP_GetIGDFromUrl(rootDescURL.c_str(), &urls, &data, internalIPAddress, sizeof(internalIPAddress)))
    233       || ((devlist = upnpDiscover(10000, 0, 0, 0, 0, 0)) != NULL && (ret = UPNP_GetValidIGD(devlist, &urls, &data, internalIPAddress, sizeof(internalIPAddress))) != 0)))
     233    if (!rootDescURL.empty() && UPNP_GetIGDFromUrl(rootDescURL.c_str(), &urls, &data, internalIPAddress, sizeof(internalIPAddress)))
    234234    {
     235        LOGMESSAGE(L"Net server: using cached IGD = %hs", urls.controlURL);
     236        // Init the return variable for UPNP_GetValidIGD to 1 so things behave when using cached URLs.
     237        ret = 1;
     238    } else if ((devlist = upnpDiscover(10000, 0, 0, 0, 0, 0)) != NULL) {
     239        // Else if we don't have a cached URL, or the one cached didn't respond. Try getting a valid UPnP device for 10 seconds
     240   
     241        // UPNP_GetValidIGD() allocates struct UPNPUrls urls on the stack for all non-zero return value.
     242        if (ret = UPNP_GetValidIGD(devlist, &urls, &data, internalIPAddress, sizeof(internalIPAddress)))
     243            bAllocatedDescriptorURL = true;
     244    } else {
    235245        LOGMESSAGE(L"Net server: upnpDiscover failed and no working cached URL.");
    236246        return NULL;
    237247    }
    238248
    239249    switch (ret)
    240250    {
     251    case 0:
     252        LOGMESSAGE(L"Net server: No IGD found");
     253        break;
    241254    case 1:
    242255        LOGMESSAGE(L"Net server: found valid IGD = %hs", urls.controlURL);
    243256        break;
     
    295308    LOGMESSAGE(L"Net server: cached UPnP root descriptor URL as %hs", urls.controlURL);
    296309
    297310    // Make sure everything is properly freed.
    298     FreeUPNPUrls(&urls);
     311    if (bAllocatedDescriptorURL)
     312        FreeUPNPUrls(&urls);
     313   
    299314    freeUPNPDevlist(devlist);
    300315
    301316    return NULL;