diff --git a/include/Actor.hpp b/include/Actor.hpp index 9285d07..44b4e21 100644 --- a/include/Actor.hpp +++ b/include/Actor.hpp @@ -92,7 +92,7 @@ namespace Actor : Actor(pos, inv), int_ip(ip), int_local_ip(lip), last_update_time(ct) {} Player(const Player& p) : Player(p.getPosition(), p.getIp(), p.getLocalIp(), p.getLastUpdateTime(), p.getInventory()) {} Player(const Multiplayer::PlayerData& pd) - : Player(pd.getPosition() * static_cast(Constants::getPIXEL_SIZE()), pd.getIp(), pd.getLocalIp(), pd.getTime(), pd.getInventory()) {} + : Player(pd.getPosition() * static_cast(Constants::getPIXEL_SIZE()), pd.getIp(), pd.getPort(), pd.getTime(), pd.getInventory()) {} inline int getIp() const { return int_ip; } inline int getLocalIp() const { return int_local_ip; } inline Multiplayer::Time getLastUpdateTime() const { return last_update_time; } diff --git a/include/Multiplayer.hpp b/include/Multiplayer.hpp index 24a584b..6ae21f6 100644 --- a/include/Multiplayer.hpp +++ b/include/Multiplayer.hpp @@ -63,12 +63,12 @@ namespace Multiplayer { public: PlayerData() : Transportable::Transportable() {} - PlayerData(const sf::Vector2f& p, const int& ip, const int& lip, const Time& t, const Inventory& ni) - : Transportable::Transportable(p, t), ip(ip), local_ip(lip), inventory(ni) {} + PlayerData(const sf::Vector2f& p, const int& ip, const unsigned int& port, const Time& t, const Inventory& ni) + : Transportable::Transportable(p, t), ip(ip), port(port), inventory(ni) {} PlayerData(const PlayerData& player) - : PlayerData(player.getPosition(), player.getIp(), player.getLocalIp(), player.getTime(), player.getInventory()) {} + : PlayerData(player.getPosition(), player.getIp(), player.getPort(), player.getTime(), player.getInventory()) {} inline int getIp() const { return ip; } - int getLocalIp() const { return local_ip; } + inline unsigned int getPort() const { return port; } const size_t objectNumber(Object::ObjectName) const; const size_t addObject(Object::ObjectName); const size_t removeObject(Object::ObjectName); @@ -76,7 +76,8 @@ namespace Multiplayer private: // data >> new_position.x >> new_position.y >> msg_ip >> msg_local_ip >> sent_time; - int ip, local_ip; + int ip; + unsigned int port; Inventory inventory; }; @@ -113,7 +114,7 @@ namespace Multiplayer public: UdpManager(const sf::IpAddress& address_receive, const sf::IpAddress& address_send); sf::Socket::Status receive(); - sf::Socket::Status send(sf::Packet& packet, const sf::IpAddress& dest_ip = sf::IpAddress()); + sf::Socket::Status send(sf::Packet& packet, const sf::IpAddress& dest_ip = sf::IpAddress(), const unsigned short& dest_port = 0); inline const PlayerDataPool& getPlayerDataPool() const { return player_data_pool; } inline const ObjectDataPool& getObjectDataPool() const { return object_data_pool; } inline void clearObjectDataPool() { object_data_pool.clear(); } @@ -127,12 +128,8 @@ namespace Multiplayer private: sf::UdpSocket socket; - sf::UdpSocket socket_send; - /*static*/ sf::IpAddress address_send; - /*static*/ sf::IpAddress address_receive; - /*static*/ sf::IpAddress ip, local_ip; + /*static*/ sf::IpAddress ip; /*static*/ unsigned short port; - /*static*/ unsigned short port_send; /*static*/ PlayerDataPool player_data_pool; /*static*/ ObjectDataPool object_data_pool; /*static*/ /* std::unordered_map terrain_data_pool; */ diff --git a/src/Multiplayer.cpp b/src/Multiplayer.cpp index fd8c0ce..10d3467 100644 --- a/src/Multiplayer.cpp +++ b/src/Multiplayer.cpp @@ -39,7 +39,7 @@ sf::Packet& operator >>(sf::Packet& data, Multiplayer::ObjectData& object_data) sf::Packet& operator <<(sf::Packet& packet, const Multiplayer::PlayerData& player_data) { - packet << player_data.getPosition().x << player_data.getPosition().y << player_data.getIp() << player_data.getLocalIp() << player_data.getTime(); + packet << player_data.getPosition().x << player_data.getPosition().y << player_data.getIp() << player_data.getPort() << player_data.getTime(); packet << static_cast(player_data.getInventory().size()); for (auto pair : player_data.getInventory()) { @@ -51,10 +51,11 @@ sf::Packet& operator <<(sf::Packet& packet, const Multiplayer::PlayerData& playe sf::Packet& operator >>(sf::Packet& packet, Multiplayer::PlayerData& player_data) { - int msg_local_ip, msg_ip; + int msg_ip; sf::Vector2f position; sf::Uint32 sent_time; - packet >> position.x >> position.y >> msg_ip >> msg_local_ip >> sent_time; + unsigned int msg_port; + packet >> position.x >> position.y >> msg_ip >> msg_port >> sent_time; sf::Uint32 inventory_size_uint32; packet >> inventory_size_uint32; size_t inventory_size = static_cast(inventory_size_uint32); @@ -67,7 +68,7 @@ sf::Packet& operator >>(sf::Packet& packet, Multiplayer::PlayerData& player_data size_t object_num = static_cast(object_num_uint32); inventory[static_cast(object_name_enum)] = std::move(object_num); } - player_data = Multiplayer::PlayerData(std::move(position), std::move(msg_ip), std::move(msg_local_ip), std::move(sent_time), std::move(inventory)); // no inventory needed! + player_data = Multiplayer::PlayerData(std::move(position), std::move(msg_ip), std::move(msg_port), std::move(sent_time), std::move(inventory)); // no inventory needed! return packet; } @@ -120,29 +121,24 @@ namespace Multiplayer UdpManager::UdpManager(const sf::IpAddress& address_receive, const sf::IpAddress& address_send) { - this->address_receive = sf::IpAddress(address_receive); - this->address_send = sf::IpAddress(address_send); port = Constants::getPORT_LISTEN(); - port_send = Constants::getPORT_SEND(); unsigned short port = Constants::getPORT_LISTEN(); - unsigned short port_send = Constants::getPORT_SEND(); auto broadcast_ip = Constants::getSERVER_IP(); ip = sf::IpAddress::getPublicAddress(sf::seconds(Constants::getMAX_PING())); - local_ip = sf::IpAddress::getLocalAddress(); socket.setBlocking(false); // bind the socket to a port if (socket.bind(port) != sf::Socket::Done) delete this; - if (socket_send.bind(port_send) != sf::Socket::Done) - delete this; } sf::Socket::Status UdpManager::receive() { data.clear(); - auto status = socket.receive(data, address_receive, port_send); + sf::IpAddress address_temp; + unsigned short port_temp; + auto status = socket.receive(data, address_temp, port_temp); if (status != sf::Socket::Status::Done) return status; DataType data_type; @@ -168,7 +164,7 @@ namespace Multiplayer //{ // std::cout << "its me" << std::endl; //} - auto id = sf::IpAddress(player_data.getIp()).toString() + sf::IpAddress(player_data.getLocalIp()).toString(); + auto id = sf::IpAddress(player_data.getIp()).toString() + sf::IpAddress(player_data.getPort()).toString(); sf::Uint32 time_now = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); int ping = static_cast(time_now) - static_cast(player_data.getTime()); if (player_data_pool.count(id)) @@ -187,7 +183,7 @@ namespace Multiplayer continue; data.clear(); data << DataType::Object << obj_data; - send(data, sf::IpAddress(player_data.getLocalIp())); + send(data, sf::IpAddress(player_data.getIp()), player_data.getPort()); sf::sleep(sf::milliseconds(1)); } } @@ -201,7 +197,7 @@ namespace Multiplayer data.clear(); auto object_data = ObjectData(sf::Vector2f(0, 0), old_time, iter.first, Object::Passability::foreground); data << DataType::Event << EventType::addObjectToInvectory << object_data; - send(data, sf::IpAddress(player_data.getLocalIp())); + send(data, sf::IpAddress(player_data.getIp()), player_data.getPort()); } } } @@ -270,7 +266,7 @@ namespace Multiplayer // receive user PlayerData player_data; data >> player_data; - auto id = sf::IpAddress(player_data.getIp()).toString() + sf::IpAddress(player_data.getLocalIp()).toString(); + auto id = sf::IpAddress(player_data.getIp()).toString() + sf::IpAddress(player_data.getPort()).toString(); sf::Uint32 time_now = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); int ping = static_cast(time_now) - static_cast(player_data.getTime()); if (player_data_pool.count(id)) @@ -290,10 +286,10 @@ namespace Multiplayer } data << DataType::Event << EventType::removeObject << object_data; for (auto iter = getPlayerDataPool().begin(); iter != getPlayerDataPool().end(); ++iter) - send(data, sf::IpAddress(iter->second.getLocalIp())); + send(data, sf::IpAddress(iter->second.getIp()), iter->second.getPort()); data.clear(); data << DataType::Event << EventType::addObjectToInvectory << object_data; - send(data, sf::IpAddress(player_data.getLocalIp())); + send(data, sf::IpAddress(player_data.getIp()), player_data.getPort()); } break; } @@ -433,12 +429,12 @@ namespace Multiplayer } - sf::Socket::Status UdpManager::send(sf::Packet& packet, const sf::IpAddress& dest_ip) + sf::Socket::Status UdpManager::send(sf::Packet& packet, const sf::IpAddress& dest_ip, const unsigned short& dest_port) { if (dest_ip == sf::IpAddress()) - return socket_send.send(packet, address_send, port); + return socket.send(packet, Constants::getSERVER_IP(), Constants::getPORT_LISTEN()); else - return socket_send.send(packet, dest_ip, port); // ? + return socket.send(packet, dest_ip, port); // ? } diff --git a/src/server/server.cpp b/src/server/server.cpp index 1fdda18..c1aacd7 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -6,7 +6,7 @@ #include #include #include -//#include +#include #include @@ -26,8 +26,8 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager) auto time_now = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); iter.second.setTime(time_now); int ping = static_cast(time_now) - static_cast(iter.second.getTime()); + std::cout << sf::IpAddress(iter.second.getIp()) << ":" << iter.second.getPort() << std::endl; //std::cout << "id: " << (*iter).first << ", last timepoint: " << std::to_string(time) << std::endl; - if (ping > Constants::getMAX_PING()) { //std::cout << "reached MAX_PING" << std::endl; @@ -35,10 +35,10 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager) continue; } auto sector_data = UdpManager.checkSector(iter.second.getPosition()); - UdpManager.send(sector_data, sf::IpAddress(iter.second.getLocalIp())); + UdpManager.send(sector_data, sf::IpAddress(iter.second.getIp()), iter.second.getPort()); for (auto dest_iter = UdpManager.getPlayerDataPool().begin(); dest_iter != UdpManager.getPlayerDataPool().end();) { - //std::cout << "sending " << (*iter).first << " data to " << (*dest_iter).first << std::endl; + //std::cout << "sending " << iter->first << " data to " << dest_iter->first << std::endl; if (dest_iter->first == iter.first) { //std::cout << "*dest_iter == *iter" << std::endl; @@ -48,8 +48,7 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager) } sf::Packet data; data << Multiplayer::DataType::Player << iter.second; - - UdpManager.send(data, sf::IpAddress(dest_iter++->second.getLocalIp())); + UdpManager.send(data, sf::IpAddress(dest_iter++->second.getIp()), dest_iter->second.getPort()); //std::cout << "sent" << std::endl; } }