Move port detection to Network class
This commit is contained in:
parent
e1188c5532
commit
652bf5480a
@ -24,6 +24,7 @@
|
||||
#include "network/network_config.hpp"
|
||||
#include "network/network_string.hpp"
|
||||
#include "network/socket_address.hpp"
|
||||
#include "network/stk_ipv6.hpp"
|
||||
#include "network/transport_address.hpp"
|
||||
#include "utils/file_utils.hpp"
|
||||
#include "utils/log.hpp"
|
||||
@ -63,10 +64,9 @@ Network::Network(int peer_count, int channel_limit,
|
||||
uint32_t max_outgoing_bandwidth,
|
||||
ENetAddress* address, bool change_port_if_bound)
|
||||
{
|
||||
m_port = 0;
|
||||
m_host = enet_host_create(address, peer_count, channel_limit, 0, 0);
|
||||
if (m_host)
|
||||
return;
|
||||
if (change_port_if_bound)
|
||||
if (!m_host && change_port_if_bound)
|
||||
{
|
||||
Log::warn("Network", "%d port is in used, use another port",
|
||||
address->port);
|
||||
@ -78,6 +78,30 @@ Network::Network(int peer_count, int channel_limit,
|
||||
if (!m_host)
|
||||
Log::fatal("Network", "Failed to create socket with any port.");
|
||||
}
|
||||
if (m_host && isIPv6Socket())
|
||||
{
|
||||
struct sockaddr_in6 sin6;
|
||||
socklen_t len = sizeof(sin6);
|
||||
if (getsockname(m_host->socket, (struct sockaddr*)&sin6, &len) == -1)
|
||||
{
|
||||
Log::error("STKHost", "Error while using getsockname().");
|
||||
m_port = 0;
|
||||
}
|
||||
else
|
||||
m_port = ntohs(sin6.sin6_port);
|
||||
}
|
||||
else if (m_host)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(m_host->socket, (struct sockaddr*)&sin, &len) == -1)
|
||||
{
|
||||
Log::error("STKHost", "Error while using getsockname().");
|
||||
m_port = 0;
|
||||
}
|
||||
else
|
||||
m_port = ntohs(sin.sin_port);
|
||||
}
|
||||
} // Network
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@ -49,6 +49,8 @@ private:
|
||||
/** ENet host interfacing sockets. */
|
||||
ENetHost* m_host;
|
||||
|
||||
uint16_t m_port;
|
||||
|
||||
/** Where to log packets. If NULL for FILE* logging is disabled. */
|
||||
static Synchronised<FILE*> m_log_file;
|
||||
|
||||
@ -75,7 +77,8 @@ public:
|
||||
SocketAddress* sender, int max_tries = -1);
|
||||
void broadcastPacket(NetworkString *data,
|
||||
bool reliable = true);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
uint16_t getPort() const { return m_port; }
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns a pointer to the ENet host object. */
|
||||
ENetHost* getENetHost() { return m_host; }
|
||||
|
@ -469,7 +469,7 @@ bool ConnectToServer::tryConnect(int timeout, int retry, bool another_port,
|
||||
break;
|
||||
Log::info("ConnectToServer", "Trying connecting to %s from port %d, "
|
||||
"retry remain: %d", connecting_address.c_str(),
|
||||
nw->getENetHost()->address.port, m_retry_count);
|
||||
nw->getPort(), m_retry_count);
|
||||
while (enet_host_service(nw->getENetHost(), &event, timeout) != 0)
|
||||
{
|
||||
if (event.type == ENET_EVENT_TYPE_CONNECT)
|
||||
|
@ -298,9 +298,8 @@ STKHost::STKHost(bool server)
|
||||
Log::fatal("STKHost", "An error occurred while trying to create an "
|
||||
"ENet server host.");
|
||||
}
|
||||
setPrivatePort();
|
||||
if (server)
|
||||
Log::info("STKHost", "Server port is %d", m_private_port);
|
||||
Log::info("STKHost", "Server port is %d", getPrivatePort());
|
||||
} // STKHost
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@ -681,37 +680,6 @@ void STKHost::setPublicAddress(bool ipv4)
|
||||
}
|
||||
} // setPublicAddress
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void STKHost::setPrivatePort()
|
||||
{
|
||||
if (isIPv6Socket())
|
||||
{
|
||||
struct sockaddr_in6 sin6;
|
||||
socklen_t len = sizeof(sin6);
|
||||
ENetHost *host = m_network->getENetHost();
|
||||
if (getsockname(host->socket, (struct sockaddr *)&sin6, &len) == -1)
|
||||
{
|
||||
Log::error("STKHost", "Error while using getsockname().");
|
||||
m_private_port = 0;
|
||||
}
|
||||
else
|
||||
m_private_port = ntohs(sin6.sin6_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
ENetHost *host = m_network->getENetHost();
|
||||
if (getsockname(host->socket, (struct sockaddr *)&sin, &len) == -1)
|
||||
{
|
||||
Log::error("STKHost", "Error while using getsockname().");
|
||||
m_private_port = 0;
|
||||
}
|
||||
else
|
||||
m_private_port = ntohs(sin.sin_port);
|
||||
}
|
||||
} // setPrivatePort
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Disconnect all connected peers.
|
||||
*/
|
||||
@ -1209,7 +1177,7 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket,
|
||||
std::string command;
|
||||
message.decodeString(&command);
|
||||
const std::string connection_cmd = std::string("connection-request") +
|
||||
StringUtils::toString(m_private_port);
|
||||
StringUtils::toString(getPrivatePort());
|
||||
|
||||
if (command == "stk-server")
|
||||
{
|
||||
@ -1223,7 +1191,7 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket,
|
||||
s.encodeString(name);
|
||||
s.addUInt8((uint8_t)ServerConfig::m_server_max_players);
|
||||
s.addUInt8((uint8_t)sl->getLobbyPlayers());
|
||||
s.addUInt16(m_private_port);
|
||||
s.addUInt16(getPrivatePort());
|
||||
s.addUInt8((uint8_t)sl->getDifficulty());
|
||||
s.addUInt8((uint8_t)sl->getGameMode());
|
||||
s.addUInt8(!pw.empty());
|
||||
@ -1255,7 +1223,7 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket,
|
||||
else if (command == "stk-server-port")
|
||||
{
|
||||
BareNetworkString s;
|
||||
s.addUInt16(m_private_port);
|
||||
s.addUInt16(getPrivatePort());
|
||||
direct_socket->sendRawPacket(s, sender);
|
||||
}
|
||||
else
|
||||
@ -1482,7 +1450,6 @@ void STKHost::initClientNetwork(ENetEvent& event, Network* new_network)
|
||||
m_next_unique_host_id++);
|
||||
stk_peer->setValidated(true);
|
||||
m_peers[event.peer] = stk_peer;
|
||||
setPrivatePort();
|
||||
auto pm = ProtocolManager::lock();
|
||||
if (pm && !pm->isExiting())
|
||||
pm->propagateEvent(new Event(&event, stk_peer));
|
||||
@ -1599,9 +1566,16 @@ int STKHost::receiveRawPacket(char *buffer, int buffer_len,
|
||||
return m_network->receiveRawPacket(buffer, buffer_len, sender,
|
||||
max_tries);
|
||||
} // receiveRawPacket
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void STKHost::sendRawPacket(const BareNetworkString &buffer,
|
||||
const SocketAddress& dst)
|
||||
{
|
||||
m_network->sendRawPacket(buffer, dst);
|
||||
} // sendRawPacket
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
uint16_t STKHost::getPrivatePort() const
|
||||
{
|
||||
return m_network->getPort();
|
||||
} // getPrivatePort
|
||||
|
@ -115,9 +115,6 @@ private:
|
||||
/** Id of thread listening to enet events. */
|
||||
std::thread m_listening_thread;
|
||||
|
||||
/** The private port enet socket is bound. */
|
||||
uint16_t m_private_port;
|
||||
|
||||
/** Flag which is set from the protocol manager thread which
|
||||
* triggers a shutdown of the STKHost (and the Protocolmanager). */
|
||||
std::atomic_bool m_shutdown;
|
||||
@ -217,9 +214,7 @@ public:
|
||||
const SocketAddress* getStunIPv6Address() const
|
||||
{ return m_stun_ipv6.get(); }
|
||||
// ------------------------------------------------------------------------
|
||||
uint16_t getPrivatePort() const { return m_private_port; }
|
||||
// ------------------------------------------------------------------------
|
||||
void setPrivatePort();
|
||||
uint16_t getPrivatePort() const;
|
||||
// ------------------------------------------------------------------------
|
||||
void setPublicAddress(bool ipv4);
|
||||
// ------------------------------------------------------------------------
|
||||
@ -410,7 +405,6 @@ public:
|
||||
void replaceNetwork(Network* new_network)
|
||||
{
|
||||
m_network = new_network;
|
||||
setPrivatePort();
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
static BareNetworkString getStunRequest(uint8_t* stun_tansaction_id);
|
||||
|
Loading…
Reference in New Issue
Block a user