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