Move UPnP port forwarding to a thread.
Fix some typos. Do not return the result, as we would need to allocate it, but we never use it. This was SVN commit r14372.
This commit is contained in:
parent
4df4c0dec0
commit
e83f24e3a6
@ -189,10 +189,14 @@ bool CNetServerWorker::SetupConnection()
|
|||||||
int ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this);
|
int ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this);
|
||||||
ENSURE(ret == 0);
|
ENSURE(ret == 0);
|
||||||
|
|
||||||
|
// Launch the UPnP thread
|
||||||
|
ret = pthread_create(&m_UPnPThread, NULL, &SetupUPnP, NULL);
|
||||||
|
ENSURE(ret == 0);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CNetServerWorker::SetupUPnP()
|
void* CNetServerWorker::SetupUPnP(void*)
|
||||||
{
|
{
|
||||||
// Values we want to set.
|
// Values we want to set.
|
||||||
char psPort[6];
|
char psPort[6];
|
||||||
@ -210,11 +214,13 @@ bool CNetServerWorker::SetupUPnP()
|
|||||||
struct UPNPUrls urls;
|
struct UPNPUrls urls;
|
||||||
struct IGDdatas data;
|
struct IGDdatas data;
|
||||||
struct UPNPDev* devlist = 0;
|
struct UPNPDev* devlist = 0;
|
||||||
// Cached root descripter URL.
|
|
||||||
|
// Cached root descriptor URL.
|
||||||
std::string rootDescURL = "";
|
std::string rootDescURL = "";
|
||||||
CFG_GET_VAL("network.upnprootdescurl", String, rootDescURL);
|
CFG_GET_VAL("network.upnprootdescurl", String, rootDescURL);
|
||||||
if (rootDescURL != "")
|
if (rootDescURL != "")
|
||||||
LOGMESSAGE(L"Net server: attempting to use cached root descripter URL: %hs", rootDescURL.c_str());
|
LOGMESSAGE(L"Net server: attempting to use cached root descriptor URL: %hs", rootDescURL.c_str());
|
||||||
|
|
||||||
// Init the return variable for UPNP_GetValidIGD to 1 so things behave when using cached URLs.
|
// Init the return variable for UPNP_GetValidIGD to 1 so things behave when using cached URLs.
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
|
||||||
@ -223,8 +229,9 @@ bool CNetServerWorker::SetupUPnP()
|
|||||||
|| ((devlist = upnpDiscover(10000, 0, 0, 0, 0, 0)) && (ret = UPNP_GetValidIGD(devlist, &urls, &data, internalIPAddress, sizeof(internalIPAddress))))))
|
|| ((devlist = upnpDiscover(10000, 0, 0, 0, 0, 0)) && (ret = UPNP_GetValidIGD(devlist, &urls, &data, internalIPAddress, sizeof(internalIPAddress))))))
|
||||||
{
|
{
|
||||||
LOGMESSAGE(L"Net server: upnpDiscover failed and no working cached URL.");
|
LOGMESSAGE(L"Net server: upnpDiscover failed and no working cached URL.");
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ret)
|
switch (ret)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
@ -245,7 +252,7 @@ bool CNetServerWorker::SetupUPnP()
|
|||||||
if (ret != UPNPCOMMAND_SUCCESS)
|
if (ret != UPNPCOMMAND_SUCCESS)
|
||||||
{
|
{
|
||||||
LOGMESSAGE(L"Net server: GetExternalIPAddress failed with code %d (%hs)", ret, strupnperror(ret));
|
LOGMESSAGE(L"Net server: GetExternalIPAddress failed with code %d (%hs)", ret, strupnperror(ret));
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
LOGMESSAGE(L"Net server: ExternalIPAddress = %hs", externalIPAddress);
|
LOGMESSAGE(L"Net server: ExternalIPAddress = %hs", externalIPAddress);
|
||||||
|
|
||||||
@ -256,7 +263,7 @@ bool CNetServerWorker::SetupUPnP()
|
|||||||
{
|
{
|
||||||
LOGMESSAGE(L"Net server: AddPortMapping(%hs, %hs, %hs) failed with code %d (%hs)",
|
LOGMESSAGE(L"Net server: AddPortMapping(%hs, %hs, %hs) failed with code %d (%hs)",
|
||||||
psPort, psPort, internalIPAddress, ret, strupnperror(ret));
|
psPort, psPort, internalIPAddress, ret, strupnperror(ret));
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the port was actually forwarded.
|
// Check that the port was actually forwarded.
|
||||||
@ -265,23 +272,26 @@ bool CNetServerWorker::SetupUPnP()
|
|||||||
psPort, protocall,
|
psPort, protocall,
|
||||||
intClient, intPort, NULL/*desc*/,
|
intClient, intPort, NULL/*desc*/,
|
||||||
NULL/*enabled*/, duration);
|
NULL/*enabled*/, duration);
|
||||||
|
|
||||||
if (ret != UPNPCOMMAND_SUCCESS)
|
if (ret != UPNPCOMMAND_SUCCESS)
|
||||||
{
|
{
|
||||||
LOGMESSAGE(L"Net server: GetSpecificPortMappingEntry() failed with code %d (%hs)", ret, strupnperror(ret));
|
LOGMESSAGE(L"Net server: GetSpecificPortMappingEntry() failed with code %d (%hs)", ret, strupnperror(ret));
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGMESSAGE(L"Net server: External %hs:%hs %hs is redirected to internal %hs:%hs (duration=%hs)",
|
LOGMESSAGE(L"Net server: External %hs:%hs %hs is redirected to internal %hs:%hs (duration=%hs)",
|
||||||
externalIPAddress, psPort, protocall, intClient, intPort, duration);
|
externalIPAddress, psPort, protocall, intClient, intPort, duration);
|
||||||
|
|
||||||
// Cache root descripter URL to try to avoid discovery next time.
|
// Cache root descriptor URL to try to avoid discovery next time.
|
||||||
g_ConfigDB.CreateValue(CFG_USER, "network.upnprootdescurl")->m_String = urls.controlURL;
|
g_ConfigDB.CreateValue(CFG_USER, "network.upnprootdescurl")->m_String = urls.controlURL;
|
||||||
g_ConfigDB.WriteFile(CFG_USER);
|
g_ConfigDB.WriteFile(CFG_USER);
|
||||||
LOGMESSAGE(L"Net server: cached UPnP root descripter URL as %hs", urls.controlURL);
|
LOGMESSAGE(L"Net server: cached UPnP root descriptor URL as %hs", urls.controlURL);
|
||||||
|
|
||||||
// Make sure everything is properly freed.
|
// Make sure everything is properly freed.
|
||||||
FreeUPNPUrls(&urls);
|
FreeUPNPUrls(&urls);
|
||||||
freeUPNPDevlist(devlist);
|
freeUPNPDevlist(devlist);
|
||||||
return true;
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CNetServerWorker::SendMessage(ENetPeer* peer, const CNetMessage* message)
|
bool CNetServerWorker::SendMessage(ENetPeer* peer, const CNetMessage* message)
|
||||||
@ -326,9 +336,6 @@ void* CNetServerWorker::RunThread(void* data)
|
|||||||
|
|
||||||
void CNetServerWorker::Run()
|
void CNetServerWorker::Run()
|
||||||
{
|
{
|
||||||
// Try to open the firewall (this could take a few seconds).
|
|
||||||
SetupUPnP();
|
|
||||||
|
|
||||||
// To avoid the need for JS_SetContextThread, we create and use and destroy
|
// To avoid the need for JS_SetContextThread, we create and use and destroy
|
||||||
// the script interface entirely within this network thread
|
// the script interface entirely within this network thread
|
||||||
m_ScriptInterface = new ScriptInterface("Engine", "Net server", ScriptInterface::CreateRuntime());
|
m_ScriptInterface = new ScriptInterface("Engine", "Net server", ScriptInterface::CreateRuntime());
|
||||||
|
@ -188,12 +188,6 @@ private:
|
|||||||
*/
|
*/
|
||||||
bool SetupConnection();
|
bool SetupConnection();
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to find a UPnP root on the network and setup port forwarding.
|
|
||||||
* @return true on success, false on error (e.g. no UPnP device)
|
|
||||||
*/
|
|
||||||
bool SetupUPnP();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call from the GUI to update the player assignments.
|
* Call from the GUI to update the player assignments.
|
||||||
* The given GUID will be (re)assigned to the given player ID.
|
* The given GUID will be (re)assigned to the given player ID.
|
||||||
@ -305,6 +299,12 @@ private:
|
|||||||
private:
|
private:
|
||||||
// Thread-related stuff:
|
// Thread-related stuff:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to find a UPnP root on the network and setup port forwarding.
|
||||||
|
*/
|
||||||
|
static void* SetupUPnP(void*);
|
||||||
|
pthread_t m_UPnPThread;
|
||||||
|
|
||||||
static void* RunThread(void* data);
|
static void* RunThread(void* data);
|
||||||
void Run();
|
void Run();
|
||||||
bool RunStep();
|
bool RunStep();
|
||||||
|
Loading…
Reference in New Issue
Block a user