Make m_ip and m_port in TransportAddress private (adding setters and getters).
This commit is contained in:
@@ -35,8 +35,7 @@
|
||||
|
||||
NetworkManager::NetworkManager()
|
||||
{
|
||||
m_public_address.m_ip = 0;
|
||||
m_public_address.m_port = 0;
|
||||
m_public_address.clear();
|
||||
m_localhost = NULL;
|
||||
m_game_setup = NULL;
|
||||
}
|
||||
|
||||
@@ -81,22 +81,22 @@ void ConnectToPeer::asynchronousUpdate()
|
||||
if (m_listener->getProtocolState(m_current_protocol_id)
|
||||
== PROTOCOL_STATE_TERMINATED) // we know the peer address
|
||||
{
|
||||
if (m_peer_address.m_ip != 0 && m_peer_address.m_port != 0)
|
||||
if (m_peer_address.getIP() != 0 && m_peer_address.getPort() != 0)
|
||||
{
|
||||
// we're in the same lan (same public ip address) !!
|
||||
if (m_peer_address.m_ip == NetworkManager::getInstance()->getPublicAddress().m_ip)
|
||||
if (m_peer_address.getIP() == NetworkManager::getInstance()->getPublicAddress().getIP())
|
||||
{
|
||||
// just send a broadcast packet with the string aloha_stk inside, the client will know our ip address and will connect
|
||||
STKHost* host = NetworkManager::getInstance()->getHost();
|
||||
TransportAddress broadcast_address;
|
||||
broadcast_address.m_ip = -1; // 255.255.255.255
|
||||
broadcast_address.m_port = m_peer_address.m_port; // 0b10101100000101101101111111111111; // for test
|
||||
broadcast_address.setIP(-1); // 255.255.255.255
|
||||
broadcast_address.setPort(m_peer_address.getPort()); // 0b10101100000101101101111111111111; // for test
|
||||
char data[] = "aloha_stk\0";
|
||||
host->sendRawPacket((uint8_t*)(data), 10, broadcast_address);
|
||||
Log::info("ConnectToPeer", "Broadcast aloha sent.");
|
||||
StkTime::sleep(1);
|
||||
broadcast_address.m_ip = 0x7f000001; // 127.0.0.1 (localhost)
|
||||
broadcast_address.m_port = m_peer_address.m_port;
|
||||
broadcast_address.setIP(0x7f000001); // 127.0.0.1 (localhost)
|
||||
broadcast_address.setPort(m_peer_address.getPort());
|
||||
host->sendRawPacket((uint8_t*)(data), 10, broadcast_address);
|
||||
Log::info("ConnectToPeer", "Broadcast aloha to self.");
|
||||
}
|
||||
|
||||
@@ -137,7 +137,8 @@ void ConnectToServer::asynchronousUpdate()
|
||||
== PROTOCOL_STATE_TERMINATED) // we know the server address
|
||||
{
|
||||
Log::info("ConnectToServer", "Server's address known");
|
||||
if (m_server_address.m_ip == m_public_address.m_ip) // we're in the same lan (same public ip address) !!
|
||||
// we're in the same lan (same public ip address) !!
|
||||
if (m_server_address.getIP() == m_public_address.getIP())
|
||||
Log::info("ConnectToServer", "Server appears to be in the same LAN.");
|
||||
m_state = REQUESTING_CONNECTION;
|
||||
m_current_protocol_id = m_listener->requestStart(new RequestConnection(m_server_id));
|
||||
@@ -148,7 +149,8 @@ void ConnectToServer::asynchronousUpdate()
|
||||
== PROTOCOL_STATE_TERMINATED) // server knows we wanna connect
|
||||
{
|
||||
Log::info("ConnectToServer", "Connection request made");
|
||||
if (m_server_address.m_ip == 0 || m_server_address.m_port == 0)
|
||||
if (m_server_address.getIP() == 0 ||
|
||||
m_server_address.getPort() == 0 )
|
||||
{ // server data not correct, hide address and stop
|
||||
m_state = HIDING_ADDRESS;
|
||||
Log::error("ConnectToServer", "Server address is %s",
|
||||
@@ -156,7 +158,8 @@ void ConnectToServer::asynchronousUpdate()
|
||||
m_current_protocol_id = m_listener->requestStart(new HidePublicAddress());
|
||||
return;
|
||||
}
|
||||
if (m_server_address.m_ip == m_public_address.m_ip) // we're in the same lan (same public ip address) !!
|
||||
// we're in the same lan (same public ip address) !!
|
||||
if (m_server_address.getIP() == m_public_address.getIP())
|
||||
{
|
||||
// just send a broadcast packet, the client will know our ip address and will connect
|
||||
STKHost* host = NetworkManager::getInstance()->getHost();
|
||||
@@ -164,8 +167,8 @@ void ConnectToServer::asynchronousUpdate()
|
||||
TransportAddress sender;
|
||||
|
||||
TransportAddress broadcast_address;
|
||||
broadcast_address.m_ip = -1; // 255.255.255.255
|
||||
broadcast_address.m_port = 7321; // 0b10101100000101101101111111111111; // for test
|
||||
broadcast_address.setIP(-1); // 255.255.255.255
|
||||
broadcast_address.setPort(7321); // 0b10101100000101101101111111111111; // for test
|
||||
char data2[] = "aloha_stk\0";
|
||||
host->sendRawPacket((uint8_t*)(data2), 10, broadcast_address);
|
||||
|
||||
@@ -219,9 +222,9 @@ void ConnectToServer::asynchronousUpdate()
|
||||
for(unsigned int i=0; i<table->dwNumEntries; i++)
|
||||
{
|
||||
unsigned int ip = ntohl(table->table[i].dwAddr);
|
||||
if(sender.m_ip == ip) // this interface is ours
|
||||
if(sender.getIP() == ip) // this interface is ours
|
||||
{
|
||||
sender.m_ip = 0x7f000001; // 127.0.0.1
|
||||
sender.setIP(0x7f000001); // 127.0.0.1
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,12 +61,17 @@ void GetPeerAddress::asynchronousUpdate()
|
||||
if (rec_success == "yes")
|
||||
{
|
||||
TransportAddress* addr = static_cast<TransportAddress*>(m_callback_object);
|
||||
result->get("ip", &addr->m_ip);
|
||||
uint32_t ip;
|
||||
result->get("ip", &ip);
|
||||
addr->setIP(ip);
|
||||
|
||||
if (addr->m_ip == NetworkManager::getInstance()->getPublicAddress().m_ip)
|
||||
result->get("private_port", &addr->m_port);
|
||||
uint16_t port;
|
||||
if (addr->getIP() ==
|
||||
NetworkManager::getInstance()->getPublicAddress().getIP())
|
||||
result->get("private_port", &port);
|
||||
else
|
||||
result->get("port", &addr->m_port);
|
||||
result->get("port", &port);
|
||||
addr->setPort(port);
|
||||
|
||||
Log::debug("GetPeerAddress", "Address gotten successfully.");
|
||||
}
|
||||
|
||||
@@ -166,8 +166,8 @@ std::string GetPublicAddress::parseStunResponse()
|
||||
{
|
||||
assert(size == 8);
|
||||
assert(datas.getUInt8(pos+5) == 0x01); // Family IPv4 only
|
||||
address.m_port = datas.getUInt16(pos + 6);
|
||||
address.m_ip = datas.getUInt32(pos + 8);
|
||||
address.setPort(datas.getUInt16(pos + 6));
|
||||
address.setIP(datas.getUInt32(pos + 8));
|
||||
break;
|
||||
} // type = 0 or 1
|
||||
pos += 4 + size;
|
||||
|
||||
@@ -59,8 +59,12 @@ void QuickJoinProtocol::asynchronousUpdate()
|
||||
{
|
||||
if(rec_success == "yes")
|
||||
{
|
||||
result->get("ip", &res->m_ip);
|
||||
result->get("port", &res->m_port);
|
||||
uint32_t ip;
|
||||
result->get("ip", &ip);
|
||||
res->setIP(ip);
|
||||
uint16_t port;
|
||||
result->get("port", &port);
|
||||
res->setPort(port);
|
||||
result->get("hostid", m_server_id);
|
||||
Log::info("QuickJoinProtocol", "Quick joining %s (server#%d).",
|
||||
res->toString().c_str(), *m_server_id);
|
||||
|
||||
@@ -184,8 +184,8 @@ void ServerLobbyRoomProtocol::checkIncomingConnectionRequests()
|
||||
Online::XMLRequest* request = new Online::XMLRequest();
|
||||
PlayerManager::setUserDetails(request, "poll-connection-requests", Online::API::SERVER_PATH);
|
||||
|
||||
request->addParameter("address", addr.m_ip);
|
||||
request->addParameter("port", addr.m_port);
|
||||
request->addParameter("address", addr.getIP());
|
||||
request->addParameter("port", addr.getPort());
|
||||
|
||||
request->executeNow();
|
||||
assert(request->isDone());
|
||||
|
||||
@@ -45,8 +45,8 @@ void ShowPublicAddress::asynchronousUpdate()
|
||||
m_request = new Online::XMLRequest();
|
||||
PlayerManager::setUserDetails(m_request, "set", Online::API::SERVER_PATH);
|
||||
|
||||
m_request->addParameter("address", addr.m_ip);
|
||||
m_request->addParameter("port", addr.m_port);
|
||||
m_request->addParameter("address", addr.getIP());
|
||||
m_request->addParameter("port", addr.getPort());
|
||||
m_request->addParameter("private_port", NetworkManager::getInstance()->getHost()->getPort());
|
||||
|
||||
Log::info("ShowPublicAddress", "Showing addr %s", addr.toString().c_str());
|
||||
|
||||
@@ -44,8 +44,8 @@ void StartServer::asynchronousUpdate()
|
||||
m_request = new Online::XMLRequest();
|
||||
PlayerManager::setUserDetails(m_request, "start", Online::API::SERVER_PATH);
|
||||
|
||||
m_request->addParameter("address", addr.m_ip);
|
||||
m_request->addParameter("port", addr.m_port);
|
||||
m_request->addParameter("address", addr.getIP());
|
||||
m_request->addParameter("port", addr.getPort());
|
||||
m_request->addParameter("private_port", NetworkManager::getInstance()->getHost()->getPort());
|
||||
m_request->addParameter("max_players", UserConfigParams::m_server_max_players);
|
||||
|
||||
|
||||
@@ -49,8 +49,8 @@ void StopServer::asynchronousUpdate()
|
||||
m_request = new Online::XMLRequest();
|
||||
PlayerManager::setUserDetails(m_request, "stop", Online::API::SERVER_PATH);
|
||||
|
||||
m_request->addParameter("address", addr.m_ip);
|
||||
m_request->addParameter("port", addr.m_port);
|
||||
m_request->addParameter("address", addr.getIP());
|
||||
m_request->addParameter("port", addr.getPort());
|
||||
|
||||
Log::info("StopServer", "address %s", addr.toString().c_str());
|
||||
|
||||
|
||||
@@ -208,8 +208,8 @@ void STKHost::sendRawPacket(uint8_t* data, int length, TransportAddress dst)
|
||||
memset(&to,0,to_len);
|
||||
|
||||
to.sin_family = AF_INET;
|
||||
to.sin_port = htons(dst.m_port);
|
||||
to.sin_addr.s_addr = htonl(dst.m_ip);
|
||||
to.sin_port = htons(dst.getPort());
|
||||
to.sin_addr.s_addr = htonl(dst.getIP());
|
||||
|
||||
sendto(m_host->socket, (char*)data, length, 0,(sockaddr*)&to, to_len);
|
||||
Log::verbose("STKHost", "Raw packet sent to %s", dst.toString().c_str());
|
||||
@@ -265,14 +265,12 @@ uint8_t* STKHost::receiveRawPacket(TransportAddress* sender)
|
||||
Log::error("STKHost", "Problem with the socket. Please contact the dev team.");
|
||||
}
|
||||
// we received the data
|
||||
sender->m_ip = ntohl((uint32_t)(addr.sin_addr.s_addr));
|
||||
sender->m_port = ntohs(addr.sin_port);
|
||||
sender->setIP( ntohl((uint32_t)(addr.sin_addr.s_addr)) );
|
||||
sender->setPort( ntohs(addr.sin_port) );
|
||||
|
||||
if (addr.sin_family == AF_INET)
|
||||
{
|
||||
char s[20];
|
||||
inet_ntop(AF_INET, &(addr.sin_addr), s, 20);
|
||||
Log::info("STKHost", "IPv4 Address of the sender was %s", s);
|
||||
Log::info("STKHost", "IPv4 Address of the sender was %s", sender->toString());
|
||||
}
|
||||
STKHost::logPacket(NetworkString(std::string((char*)(buffer), len)), true);
|
||||
return buffer;
|
||||
@@ -294,7 +292,7 @@ uint8_t* STKHost::receiveRawPacket(TransportAddress sender, int max_tries)
|
||||
|
||||
int i = 0;
|
||||
// wait to receive the message because enet sockets are non-blocking
|
||||
while(len < 0 || addr.sin_addr.s_addr == sender.m_ip)
|
||||
while(len < 0 || addr.sin_addr.s_addr == sender.getIP())
|
||||
{
|
||||
i++;
|
||||
if (len>=0)
|
||||
@@ -339,8 +337,8 @@ bool STKHost::peerExists(TransportAddress peer)
|
||||
{
|
||||
for (unsigned int i = 0; i < m_host->peerCount; i++)
|
||||
{
|
||||
if (m_host->peers[i].address.host == ntohl(peer.m_ip) &&
|
||||
m_host->peers[i].address.port == peer.m_port)
|
||||
if (m_host->peers[i].address.host == ntohl(peer.getIP()) &&
|
||||
m_host->peers[i].address.port == peer.getPort() )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -62,16 +62,10 @@ STKPeer::~STKPeer()
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host,
|
||||
bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host,
|
||||
uint32_t channel_count, uint32_t data)
|
||||
{
|
||||
ENetAddress address;
|
||||
address.host =
|
||||
((host.m_ip & 0xff000000) >> 24)
|
||||
+ ((host.m_ip & 0x00ff0000) >> 8)
|
||||
+ ((host.m_ip & 0x0000ff00) << 8)
|
||||
+ ((host.m_ip & 0x000000ff) << 24); // because ENet wants little endian
|
||||
address.port = host.m_port;
|
||||
const ENetAddress address = host.toEnetAddress();
|
||||
|
||||
ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0);
|
||||
if (peer == NULL)
|
||||
@@ -79,9 +73,7 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host,
|
||||
Log::error("STKPeer", "Could not try to connect to server.\n");
|
||||
return false;
|
||||
}
|
||||
TransportAddress a;
|
||||
a.m_ip = peer->address.host;
|
||||
a.m_port = peer->address.port;
|
||||
TransportAddress a(peer->address);
|
||||
Log::verbose("STKPeer", "Connecting to %s", a.toString().c_str());
|
||||
return true;
|
||||
}
|
||||
@@ -97,10 +89,7 @@ void STKPeer::disconnect()
|
||||
|
||||
void STKPeer::sendPacket(NetworkString const& data, bool reliable)
|
||||
{
|
||||
TransportAddress a;
|
||||
a.m_ip = m_peer->address.host;
|
||||
a.m_port = m_peer->address.port;
|
||||
|
||||
TransportAddress a(m_peer->address);
|
||||
Log::verbose("STKPeer", "sending packet of size %d to %s",
|
||||
a.toString().c_str());
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ class STKPeer
|
||||
virtual ~STKPeer();
|
||||
|
||||
virtual void sendPacket(const NetworkString& data, bool reliable = true);
|
||||
static bool connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data);
|
||||
static bool connectToHost(STKHost* localhost, const TransportAddress& host,
|
||||
uint32_t channel_count, uint32_t data);
|
||||
void disconnect();
|
||||
|
||||
void setClientServerToken(const uint32_t& token) { *m_client_server_token = token; *m_token_set = true; }
|
||||
|
||||
@@ -48,10 +48,10 @@ class CallbackObject
|
||||
*/
|
||||
class TransportAddress : public CallbackObject
|
||||
{
|
||||
public:
|
||||
private:
|
||||
uint32_t m_ip; //!< The IPv4 address
|
||||
uint16_t m_port; //!< The port number
|
||||
|
||||
public:
|
||||
/** Constructor. */
|
||||
TransportAddress(uint32_t ip = 0, uint16_t port = 0)
|
||||
{
|
||||
@@ -59,6 +59,14 @@ public:
|
||||
m_port = port;
|
||||
} // TransportAddress
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Construct an transport address from an ENetAddress. */
|
||||
TransportAddress(const ENetAddress &a)
|
||||
{
|
||||
m_ip = a.host;
|
||||
m_port = a.port;
|
||||
} // TransportAddress(EnetAddress)
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
~TransportAddress() {}
|
||||
// ------------------------------------------------------------------------
|
||||
@@ -68,6 +76,37 @@ public:
|
||||
m_ip = 0;
|
||||
m_port = 0;
|
||||
} // clear
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the ip address. */
|
||||
uint32_t getIP() const { return m_ip; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the port number. */
|
||||
uint16_t getPort() const { return m_port; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Sets the ip address. */
|
||||
void setIP(uint32_t ip) { m_ip = ip; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Set the port. */
|
||||
void setPort(uint16_t port) { m_port = port; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Converts the address to an enet address. */
|
||||
ENetAddress toEnetAddress() const
|
||||
{
|
||||
ENetAddress a;
|
||||
// because ENet wants little endian
|
||||
a.host = ((m_ip & 0xff000000) >> 24)
|
||||
+ ((m_ip & 0x00ff0000) >> 8)
|
||||
+ ((m_ip & 0x0000ff00) << 8)
|
||||
+ ((m_ip & 0x000000ff) << 24);
|
||||
a.port = m_port;
|
||||
return a;
|
||||
} // toEnetAddress
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** Compares if ip address and port are identical. */
|
||||
bool operator==(const TransportAddress& other) const
|
||||
|
||||
Reference in New Issue
Block a user