Allow auto-fallback to another unused port if needed

This commit is contained in:
Benau 2018-02-23 16:16:43 +08:00
parent a93182740e
commit d586ab9011
2 changed files with 17 additions and 6 deletions

View File

@ -45,19 +45,29 @@ Synchronised<FILE*>Network::m_log_file = NULL;
* \param channel_limit : The maximum number of channels per peer.
* \param max_incoming_bandwidth : The maximum incoming bandwidth.
* \param max_outgoing_bandwidth : The maximum outgoing bandwidth.
* \param change_port_if_bound : Use another port if the prefered port is
* already bound to a socket.
*/
Network::Network(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth,
ENetAddress* address)
ENetAddress* address, bool change_port_if_bound)
{
m_host = enet_host_create(address, peer_count, channel_limit, 0, 0);
if (!m_host)
if (m_host)
return;
if (change_port_if_bound)
{
Log::fatal("Network", "An error occurred while trying to create an "
"ENet client host, maybe you started multiple instance "
"of STK server?");
ENetAddress new_addr;
new_addr.host = address->host;
// Any port
new_addr.port = 0;
m_host = enet_host_create(&new_addr, peer_count, channel_limit, 0, 0);
if (m_host)
return;
}
Log::fatal("Network", "An error occurred while trying to create an ENet "
"client host, maybe you started multiple instance of STK server?");
} // Network
// ----------------------------------------------------------------------------

View File

@ -54,7 +54,8 @@ public:
Network(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth,
ENetAddress* address);
ENetAddress* address,
bool change_port_if_bound = false);
virtual ~Network();
static void openLog();