Skip to content

Commit

Permalink
transportable packets changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ISPlatonov committed Jan 18, 2024
1 parent e10388a commit ef359fc
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 55 deletions.
13 changes: 3 additions & 10 deletions include/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
#include <SFML/Window.hpp>
#endif
#include <SFML/Network.hpp>
#include "Time.hpp"
#include <string>
#include <fstream>
#include <random>
#include <chrono>


namespace Time {
typedef sf::Uint32 Time;

inline Time getTimeNow() { return static_cast<Time>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
}


/**
Expand Down Expand Up @@ -226,8 +219,8 @@ class Constants
// Network
static inline size_t PORT_LISTEN = 55000;
static inline size_t PORT_SEND = 55001;
static inline std::string SERVER_IP = "77.73.71.158";
static inline int MAX_PING = 5000;
static inline std::string SERVER_IP = "77.73.69.78";
static inline int MAX_PING = 1000;
static inline size_t UDP_PACKETS_GAP = 1000;
static inline PlayerId id = {0, 0, 0, 0};
};
15 changes: 15 additions & 0 deletions include/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
#include "Constants.hpp"


template<>
struct std::hash<PlayerId>
{
inline size_t operator()(const PlayerId& id) const
{
size_t tmp = id.getIp();
tmp ^= id.getPort() + 0x9e3779b9 + (tmp << 6) + (tmp >> 2);
tmp ^= id.getTimestamp() + 0x9e3779b9 + (tmp << 6) + (tmp >> 2);
tmp ^= id.getKey() + 0x9e3779b9 + (tmp << 6) + (tmp >> 2);

return tmp;
}
};


template <>
struct std::hash<SocketInfo>
{
Expand Down
21 changes: 12 additions & 9 deletions include/Multiplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ namespace Multiplayer
{
public:
Transportable() {}
Transportable(sf::Vector2f pos, Time::Time sent) : position(pos), sent_time(sent) {}
Transportable(sf::Vector2f pos, Time::Time sent, SocketInfo socket) : position(pos), sent_time(sent), socket_info(socket) {}
inline void setPosition(const sf::Vector2f& new_position) { position = new_position; };
inline void setTime(const Time::Time& new_time) { sent_time = new_time; }
inline void setSocketInfo(const SocketInfo& new_socket_info) { socket_info = new_socket_info; }
inline const sf::Vector2f& getPosition() const { return position; }
inline const Time::Time& getTime() const { return sent_time; }
inline const SocketInfo& getSocketInfo() const { return socket_info; }

private:
sf::Vector2f position;
Time::Time sent_time = 0;
SocketInfo socket_info;
};


Expand All @@ -61,10 +64,10 @@ namespace Multiplayer
{
public:
PlayerData() : Transportable::Transportable() {}
PlayerData(const sf::Vector2f& p, const PlayerId& p_id, const Time::Time& t, const Inventory& ni)
: Transportable::Transportable(p, t), player_id(p_id), inventory(ni) {}
PlayerData(const sf::Vector2f& p, const PlayerId& p_id, const Time::Time& t, const SocketInfo& s, const Inventory& ni)
: Transportable::Transportable(p, t, s), player_id(p_id), inventory(ni) {}
PlayerData(const PlayerData& player)
: PlayerData(player.getPosition(), player.getId(), player.getTime(), player.getInventory()) {}
: PlayerData(player.getPosition(), player.getId(), player.getTime(), player.getSocketInfo(), player.getInventory()) {}
inline PlayerId getId() const { return player_id; }
const size_t objectNumber(Object::ObjectName) const;
const size_t addObject(Object::ObjectName);
Expand All @@ -86,9 +89,9 @@ namespace Multiplayer
{
public:
ObjectData() : Transportable::Transportable() {}
ObjectData(sf::Vector2f pos, Time::Time t, Object::ObjectName name, Object::Passability pass)
: Transportable::Transportable(pos, t), object_name(name), passability(pass) {}
ObjectData(const ObjectData& object) : ObjectData(object.getPosition(), object.getTime(), object.getName(), object.getPassability()) {}
ObjectData(sf::Vector2f pos, Time::Time t, SocketInfo s, Object::ObjectName name, Object::Passability pass)
: Transportable::Transportable(pos, t, s), object_name(name), passability(pass) {}
ObjectData(const ObjectData& object) : ObjectData(object.getPosition(), object.getTime(), object.getSocketInfo(), object.getName(), object.getPassability()) {}
const Object::ObjectName& getName() const { return object_name; }
const Object::Passability& getPassability() const { return passability; }
inline bool operator ==(const ObjectData& object_data) const { return object_name == object_data.getName() && passability == object_data.getPassability(); }
Expand All @@ -99,7 +102,7 @@ namespace Multiplayer
};


typedef std::unordered_map<SocketInfo, PlayerData> PlayerDataPool;
typedef std::unordered_map<PlayerId, PlayerData> PlayerDataPool;
typedef std::unordered_map<sf::Vector2f, std::vector<ObjectData>> ObjectDataPool;

/**
Expand All @@ -114,7 +117,7 @@ namespace Multiplayer
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(); }
PlayerDataPool::iterator removePlayerBySocketInfo(const SocketInfo& id);
PlayerDataPool::iterator removePlayerByPlayerId(const PlayerId& id);
bool removeObject(const ObjectData&);
void addObject(const Object::Object& object);
void addObject(const Multiplayer::ObjectData& object_data);
Expand Down
11 changes: 11 additions & 0 deletions include/Time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <SFML/System.hpp>
#include <chrono>


namespace Time {
typedef sf::Uint32 Time;

inline Time getTimeNow() { return static_cast<Time>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
}
12 changes: 6 additions & 6 deletions src/common/Controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Controls::addEvent(const sf::Event& event)
data << Multiplayer::DataType::Event;
data << Multiplayer::EventType::takeObjectToInventory;
sf::Vector2f object_position(iter.second.getPosition().x / Constants::getPIXEL_SIZE(), iter.second.getPosition().y / Constants::getPIXEL_SIZE());
auto object_data = Multiplayer::ObjectData(object_position, Time::getTimeNow(), iter.second.getName(), iter.second.getPassability());
auto object_data = Multiplayer::ObjectData(object_position, Time::getTimeNow(), SocketInfo(0, 0) /* ??? */, iter.second.getName(), iter.second.getPassability());
data << object_data;
data << user;
udp_manager.send(data);
Expand Down Expand Up @@ -232,21 +232,21 @@ void Controls::handleFrameStep()
auto time = iter->second.getTime();
Time::Time time_now = Time::getTimeNow();
int ping = static_cast<int>(time_now) - static_cast<int>(time);
if (!player_pool.count(iter->first))
if (!player_pool.count(iter->second.getSocketInfo()))
{
player_pool[iter->first] = Actor::Player(iter->second);
player_pool[iter->second.getSocketInfo()] = Actor::Player(iter->second);
++iter;
continue;
}
else if (ping > Constants::getMAX_PING())
{
player_pool.erase(iter->first);
udp_manager.removePlayerBySocketInfo(iter++->first);
player_pool.erase(iter->second.getSocketInfo());
udp_manager.removePlayerByPlayerId(iter++->second.getId());
continue;
}
else
{
player_pool[iter->first] << iter->second;
player_pool[iter->second.getSocketInfo()] << iter->second;
++iter;
continue;
}
Expand Down
39 changes: 19 additions & 20 deletions src/common/Multiplayer/Multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ namespace Multiplayer
sf::Socket::Status UdpManager::receive()
{
data.clear();
sf::IpAddress address_temp;
unsigned short port_temp;
SocketInfo socket_info;
auto status = sf::Socket::Status::NotReady;
status = socket.receive(data, address_temp, port_temp);
status = socket.receive(data, socket_info.first, socket_info.second);
if (status != sf::Socket::Status::Done)
return status;
DataType data_type;
sf::Uint32 data_type_enum;
data >> data_type_enum;
data_type = static_cast<DataType>(data_type_enum);
{
sf::Uint32 data_type_enum;
data >> data_type_enum;
data_type = static_cast<DataType>(data_type_enum);
}

switch (data_type)
{
Expand All @@ -76,19 +77,16 @@ namespace Multiplayer
// receive object
Multiplayer::ObjectData object_data;
data >> object_data;
object_data.setSocketInfo(socket_info);
addObject(object_data);
break;
}
case DataType::Player:
{
PlayerData player_data;
data >> player_data;
//player_data = PlayerData(player_data.getPosition(), address_temp.toInteger(), port_temp, player_data.getTime(), player_data.getInventory());
//if (msg_local_ip == local_ip.toInteger())
//{
// std::cout << "its me" << std::endl;
//}
auto id = SocketInfo(address_temp, port_temp);
player_data.setSocketInfo(socket_info);
auto id = player_data.getId();
Time::Time time_now = Time::getTimeNow();
int ping = static_cast<int>(time_now) - static_cast<int>(player_data.getTime());
if (player_data_pool.count(id))
Expand All @@ -107,7 +105,7 @@ namespace Multiplayer
continue;
data.clear();
data << DataType::Object << obj_data;
send(data, address_temp, port_temp);
send(data, socket_info.first, socket_info.second);
sf::sleep(sf::milliseconds(1));
}
}
Expand All @@ -119,9 +117,9 @@ namespace Multiplayer
for (size_t i = 0; i < iter.second; ++i)
{
data.clear();
auto object_data = ObjectData(sf::Vector2f(0, 0), old_time, iter.first, Object::Passability::foreground);
auto object_data = ObjectData(sf::Vector2f(0, 0), old_time, socket_info, iter.first, Object::Passability::foreground);
data << DataType::Event << EventType::addObjectToInvectory << object_data;
send(data, address_temp, port_temp);
send(data, socket_info.first, socket_info.second);
}
}
}
Expand Down Expand Up @@ -190,7 +188,7 @@ namespace Multiplayer
// receive user
PlayerData player_data;
data >> player_data;
auto id = SocketInfo(address_temp, port_temp);
auto id = player_data.getId();
Time::Time time_now = Time::getTimeNow();
int ping = static_cast<int>(time_now) - static_cast<int>(player_data.getTime());
if (player_data_pool.count(id))
Expand All @@ -210,10 +208,10 @@ namespace Multiplayer
}
data << DataType::Event << EventType::removeObject << object_data;
for (auto iter = getPlayerDataPool().begin(); iter != getPlayerDataPool().end(); ++iter)
send(data, id.first, id.second);
send(data, socket_info.first, socket_info.second);
data.clear();
data << DataType::Event << EventType::addObjectToInvectory << object_data;
send(data, address_temp, port_temp);
send(data, socket_info.first, socket_info.second);
}
break;
}
Expand Down Expand Up @@ -262,7 +260,7 @@ namespace Multiplayer
}


PlayerDataPool::iterator UdpManager::removePlayerBySocketInfo(const SocketInfo& id)
PlayerDataPool::iterator UdpManager::removePlayerByPlayerId(const PlayerId& id)
{
auto iter = player_data_pool.find(id);
if (iter == player_data_pool.end())
Expand Down Expand Up @@ -303,6 +301,7 @@ namespace Multiplayer
{
ObjectData object_data( object.getPosition() / static_cast<float>(Constants::getPIXEL_SIZE()),
Time::getTimeNow(),
SocketInfo(0, 0), // ?
object.getName(),
object.getPassability());
addObject(object_data);
Expand Down Expand Up @@ -386,7 +385,7 @@ namespace Multiplayer
{
object_name = Object::ObjectName::dirt;
}
addObject(Multiplayer::ObjectData(point, time_now, object_name, passability));
addObject(Multiplayer::ObjectData(point, time_now, SocketInfo(0, 0), object_name, passability));
//for (auto iter : player_data_pool)
//{
// sf::Packet data;
Expand Down
8 changes: 3 additions & 5 deletions src/common/Multiplayer/Operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sf::Packet& operator <<(sf::Packet& packet, Multiplayer::ObjectData& object_data)
{
object_data.setTime(Time::getTimeNow());
packet << object_data.getPosition().x << object_data.getPosition().y << static_cast<sf::Uint32>(object_data.getTime()) << object_data.getName() << object_data.getPassability();
packet << object_data.getPosition().x << object_data.getPosition().y << object_data.getTime() << object_data.getName() << object_data.getPassability();
return packet;
}

Expand All @@ -30,7 +30,7 @@ sf::Packet& operator >>(sf::Packet& data, Multiplayer::ObjectData& object_data)
data >> position.x >> position.y >> sent_time >> object_name_enum >> passability_enum;
object_name = static_cast<Object::ObjectName>(object_name_enum);
passability = static_cast<Object::Passability>(passability_enum);
object_data = Multiplayer::ObjectData(std::move(position), std::move(sent_time), std::move(object_name), std::move(passability));
object_data = Multiplayer::ObjectData(std::move(position), std::move(sent_time), SocketInfo(0, 0), std::move(object_name), std::move(passability));
return data;
}

Expand Down Expand Up @@ -58,10 +58,8 @@ sf::Packet& operator >>(sf::Packet& packet, PlayerId& player_id)

sf::Packet& operator >>(sf::Packet& packet, Multiplayer::PlayerData& player_data)
{
int msg_ip;
sf::Vector2f position;
sf::Uint32 sent_time;
unsigned int msg_port;
PlayerId player_id;
packet >> position.x >> position.y >> player_id >> sent_time;
sent_time = Time::getTimeNow();
Expand All @@ -77,6 +75,6 @@ sf::Packet& operator >>(sf::Packet& packet, Multiplayer::PlayerData& player_data
size_t object_num = static_cast<size_t>(object_num_uint32);
inventory[static_cast<Object::ObjectName>(object_name_enum)] = std::move(object_num);
}
player_data = Multiplayer::PlayerData(std::move(position), std::move(player_id), std::move(sent_time), std::move(inventory)); // no inventory needed!
player_data = Multiplayer::PlayerData(std::move(position), std::move(player_id), std::move(sent_time), SocketInfo(0, 0), std::move(inventory)); // no inventory needed!
return packet;
}
9 changes: 5 additions & 4 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager)
auto time_now = Time::getTimeNow();
iter.second.setTime(time_now);
int ping = static_cast<int>(time_now) - static_cast<int>(iter.second.getTime());
std::cout << iter.first.first << ":" << iter.first.second << std::endl;
std::cout << iter.second.getSocketInfo().first << ":" << iter.second.getSocketInfo().second << std::endl;
//std::cout << "id: " << (*iter).first << ", last timepoint: " << std::to_string(time) << std::endl;
if (ping > Constants::getMAX_PING())
{
Expand All @@ -35,7 +35,7 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager)
continue;
}
auto sector_data = UdpManager.checkSector(iter.second.getPosition());
UdpManager.send(sector_data, iter.first.first, iter.first.second);
UdpManager.send(sector_data, iter.second.getSocketInfo().first, iter.second.getSocketInfo().second);
for (auto dest_iter = UdpManager.getPlayerDataPool().begin(); dest_iter != UdpManager.getPlayerDataPool().end(); ++dest_iter)
{
//std::cout << "sending " << iter->first << " data to " << dest_iter->first << std::endl;
Expand All @@ -46,8 +46,8 @@ void UdpWorker(Multiplayer::UdpManager& UdpManager)
continue;
}
sf::Packet data;
data << Multiplayer::DataType::Player << iter.second;
UdpManager.send(data, dest_iter->first.first, dest_iter->first.second);
data << Multiplayer::DataType::Player << iter.first;
UdpManager.send(data, iter.second.getSocketInfo().first, iter.second.getSocketInfo().second);
//std::cout << "sent" << std::endl;
}
}
Expand Down Expand Up @@ -75,6 +75,7 @@ std::vector<Multiplayer::ObjectData> load_terrain(const std::string& path)
object_data_pool_init.push_back(Multiplayer::ObjectData(
sf::Vector2f(x, y),
Time::getTimeNow(),
SocketInfo(0, 0), // ?
name,
pass
));
Expand Down
2 changes: 1 addition & 1 deletion textures/constants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ true
55000
55001
77.73.69.78
5000
1000
1000

0 comments on commit ef359fc

Please sign in to comment.