Extract InitNetworkClient method

This commit is contained in:
Robert Müller 2023-04-01 10:57:06 +02:00
parent b3f384f312
commit c9f68901b7
2 changed files with 45 additions and 40 deletions

View file

@ -3024,46 +3024,8 @@ void CClient::Run()
#endif #endif
#ifndef CONF_WEBASM #ifndef CONF_WEBASM
// open socket if(!InitNetworkClient())
{ return;
NETADDR BindAddr;
if(g_Config.m_Bindaddr[0] == '\0')
{
mem_zero(&BindAddr, sizeof(BindAddr));
}
else if(net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) != 0)
{
dbg_msg("client", "The configured bindaddr '%s' cannot be resolved", g_Config.m_Bindaddr);
return;
}
BindAddr.type = NETTYPE_ALL;
for(unsigned int i = 0; i < std::size(m_aNetClient); i++)
{
int &PortRef = i == CONN_MAIN ? g_Config.m_ClPort : i == CONN_DUMMY ? g_Config.m_ClDummyPort : g_Config.m_ClContactPort;
if(PortRef < 1024) // Reject users setting ports that we don't want to use
{
PortRef = 0;
}
BindAddr.port = PortRef;
unsigned RemainingAttempts = 25;
while(BindAddr.port == 0 || !m_aNetClient[i].Open(BindAddr))
{
if(BindAddr.port != 0)
{
--RemainingAttempts;
if(RemainingAttempts == 0)
{
if(g_Config.m_Bindaddr[0])
dbg_msg("client", "Could not open network client, try changing or unsetting the bindaddr '%s'", g_Config.m_Bindaddr);
else
dbg_msg("client", "Could not open network client");
return;
}
}
BindAddr.port = (secure_rand() % 64511) + 1024;
}
}
}
#endif #endif
// init font rendering // init font rendering
@ -3421,6 +3383,48 @@ void CClient::Run()
delete m_pEditor; delete m_pEditor;
} }
bool CClient::InitNetworkClient()
{
NETADDR BindAddr;
if(g_Config.m_Bindaddr[0] == '\0')
{
mem_zero(&BindAddr, sizeof(BindAddr));
}
else if(net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) != 0)
{
dbg_msg("client", "The configured bindaddr '%s' cannot be resolved", g_Config.m_Bindaddr);
return false;
}
BindAddr.type = NETTYPE_ALL;
for(unsigned int i = 0; i < std::size(m_aNetClient); i++)
{
int &PortRef = i == CONN_MAIN ? g_Config.m_ClPort : i == CONN_DUMMY ? g_Config.m_ClDummyPort : g_Config.m_ClContactPort;
if(PortRef < 1024) // Reject users setting ports that we don't want to use
{
PortRef = 0;
}
BindAddr.port = PortRef;
unsigned RemainingAttempts = 25;
while(BindAddr.port == 0 || !m_aNetClient[i].Open(BindAddr))
{
if(BindAddr.port != 0)
{
--RemainingAttempts;
if(RemainingAttempts == 0)
{
if(g_Config.m_Bindaddr[0])
dbg_msg("client", "Could not open network client, try changing or unsetting the bindaddr '%s'", g_Config.m_Bindaddr);
else
dbg_msg("client", "Could not open network client");
return false;
}
}
BindAddr.port = (secure_rand() % 64511) + 1024;
}
}
return true;
}
bool CClient::CtrlShiftKey(int Key, bool &Last) bool CClient::CtrlShiftKey(int Key, bool &Last)
{ {
if(Input()->ModifierIsPressed() && Input()->ShiftIsPressed() && !Last && Input()->KeyIsPressed(Key)) if(Input()->ModifierIsPressed() && Input()->ShiftIsPressed() && !Last && Input()->KeyIsPressed(Key))

View file

@ -425,6 +425,7 @@ public:
void Run(); void Run();
bool InitNetworkClient();
bool CtrlShiftKey(int Key, bool &Last); bool CtrlShiftKey(int Key, bool &Last);
static void Con_Connect(IConsole::IResult *pResult, void *pUserData); static void Con_Connect(IConsole::IResult *pResult, void *pUserData);