Skip to content

Commit

Permalink
Feat (GameEngine, Server): Connection -> shared_ptr<Connection>
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjuin1 committed Jan 9, 2024
1 parent 3a8fb26 commit 391550f
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 76 deletions.
9 changes: 7 additions & 2 deletions GameEngine/src/Network/Connection/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ struct NetworkInfo {

class Connection {
public:
Connection(asio::ip::udp::endpoint endpoint) { _endpoint = endpoint; };
Connection(asio::ip::udp::endpoint endpoint) {
_endpoint = endpoint;
};

Connection(asio::ip::udp::endpoint endpoint, uint64_t worldId) {
_endpoint = endpoint;
_worldId = worldId;
};

Connection() = default;
~Connection() = default;
~Connection() {
_packetNeedAck.clear();
std::cout << "Connection destroyed in destructor" << std::endl;
};

void ResendNeedAck(Exodia::Network::UDPSocket &socket) {
for (auto &packet : _packetNeedAck) {
Expand Down
23 changes: 11 additions & 12 deletions GameEngine/src/Network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Exodia {
#define COMMAND_NETWORK(x) \
std::bind(&x, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
#define RECEIVE_ARG \
const std::vector<char> message, size_t size, Connection &senderConnection, Exodia::Network::Header header
const std::vector<char> message, size_t size, std::shared_ptr<Connection> senderConnection, Exodia::Network::Header header
#define STRING_FROM_ENDPOINT(x) x.address().to_string() + ":" + std::to_string(x.port())

enum class NetworkType { NONE, CLIENT, SERVER, SINGLEPLAYER };
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace Exodia {
*
* @return std::unordered_map<std::string, Connection> Unordered map of pair of string and Connection
*/
std::map<std::string, Connection> &GetConnections() { return _connections; }
std::map<std::string, std::shared_ptr<Connection>> &GetConnections() { return _connections; }

/**
* @brief Return a queue of uint32_t representing the events received
Expand All @@ -114,8 +114,8 @@ namespace Exodia {
int32_t i = 0;

for (auto &connection : _connections) {
std::cout << connection.second.GetEndpoint() << ", " << i << std::endl;
if (connection.second.GetEndpoint() == endpoint)
std::cout << connection.second->GetEndpoint() << ", " << i << std::endl;
if (connection.second->GetEndpoint() == endpoint)
return i;
i++;
}
Expand All @@ -126,7 +126,7 @@ namespace Exodia {

UDPSocket &GetSocket() { return _socket; }

NetworkInfo GetNetworkInfo() { return _server_connection.GetLastNetworkInfo(); }
NetworkInfo GetNetworkInfo() { return _server_connection->GetLastNetworkInfo(); }

/**
* @brief Use to disconnect a user
Expand All @@ -135,8 +135,8 @@ namespace Exodia {
*
* @return void
*/
void Disconnect(Connection connection) {
auto it = _connections.find(STRING_FROM_ENDPOINT(connection.GetEndpoint()));
void Disconnect(std::shared_ptr<Connection> connection) {
auto it = _connections.find(STRING_FROM_ENDPOINT(connection->GetEndpoint()));
if (it != _connections.end()) {
_connections.erase(it);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ namespace Exodia {
* @return void
*/
void connect(const std::string &ip, short port) {
_server_connection = Connection(asio::ip::udp::endpoint(asio::ip::address::from_string(ip), port));
_server_connection = std::make_shared<Connection>(asio::ip::udp::endpoint(asio::ip::address::from_string(ip), port));
}

int64_t GetIndexPacketNeedAck(Connection connection) {
Expand All @@ -175,17 +175,16 @@ namespace Exodia {
return -1;
}

World *GetWorld(Connection connection) {
(void)connection;
World *GetWorld() {
return _world;
}

private:
uint64_t id = 0;
World *_world;
UDPSocket _socket;
std::map<std::string, Connection> _connections;
Connection _server_connection;
std::map<std::string, std::shared_ptr<Connection>> _connections;
std::shared_ptr<Connection> _server_connection;
NetworkType _networkType = NetworkType::NONE;
IOContextManager &_ioContextManager;
std::vector<std::pair<std::pair<uint32_t, bool>, asio::ip::udp::endpoint>> _events;
Expand Down
63 changes: 32 additions & 31 deletions GameEngine/src/Network/NetworkReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ namespace Exodia::Network {
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count() -
header.getTimestamp();
senderConnection.SetPing((uint16_t)ping);
senderConnection->SetPing((uint16_t)ping);

std::memcpy(&packet_received, message.data(), sizeof(int));
std::memcpy(&packet_sent, message.data() + sizeof(int), sizeof(int));

int32_t diff_sent = senderConnection.GetReceivedPacket() - packet_sent;
int32_t diff_received = senderConnection.GetSendPacket() - packet_received;
int32_t diff_sent = senderConnection->GetReceivedPacket() - packet_sent;
int32_t diff_received = senderConnection->GetSendPacket() - packet_received;

int32_t packet_sent_loss = 0;
int32_t packet_received_loss = 0;

if (senderConnection.GetReceivedPacket() != 0) {
packet_sent_loss = diff_sent * 100 / senderConnection.GetReceivedPacket();
if (senderConnection->GetReceivedPacket() != 0) {
packet_sent_loss = diff_sent * 100 / senderConnection->GetReceivedPacket();
}

if (senderConnection.GetSendPacket() != 0) {
packet_received_loss = diff_received * 100 / senderConnection.GetSendPacket();
if (senderConnection->GetSendPacket() != 0) {
packet_received_loss = diff_received * 100 / senderConnection->GetSendPacket();
}

senderConnection.SetPacketLoss(packet_sent_loss, packet_received_loss);
senderConnection.SetReceivedPacket(0);
senderConnection.SetSendPacket(0);
senderConnection.SetKiloByteSent(0);
senderConnection.SetKiloByteReceived(0);
senderConnection->SetPacketLoss(packet_sent_loss, packet_received_loss);
senderConnection->SetReceivedPacket(0);
senderConnection->SetSendPacket(0);
senderConnection->SetKiloByteSent(0);
senderConnection->SetKiloByteReceived(0);
}

/**
Expand All @@ -76,7 +76,7 @@ namespace Exodia::Network {
void Network::ReceiveDeleteEntity(RECEIVE_ARG) {
(void)size;

World *world = GetWorld(senderConnection);
World *world = GetWorld();
world->LockMutex();
uint64_t id = 0;
std::memcpy(&id, message.data(), sizeof(unsigned long));
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace Exodia::Network {
std::memcpy(component_name.data(), message.data() + sizeof(unsigned long) + sizeof(unsigned int),
size_of_string);

World *world = GetWorld(senderConnection);
World *world = GetWorld();
world->LockMutex();
Entity *entity = world->GetEntityByID(id);
std::function<Exodia::IComponentContainer *(Exodia::Buffer)> func =
Expand Down Expand Up @@ -183,8 +183,8 @@ namespace Exodia::Network {

uint64_t command_id = 0;
std::memcpy(&command_id, message.data(), sizeof(uint64_t));
if (senderConnection.GetPacketNeedAck().find(command_id) != senderConnection.GetPacketNeedAck().end()) {
senderConnection.RemovePacketNeedAck(command_id);
if (senderConnection->GetPacketNeedAck().find(command_id) != senderConnection->GetPacketNeedAck().end()) {
senderConnection->RemovePacketNeedAck(command_id);
std::cout << "Remove packet need ack " << command_id << std::endl;
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ namespace Exodia::Network {
message.data() + sizeof(unsigned long) + sizeof(unsigned int) + size_of_string + sizeof(uint32_t),
size_of_data);

World *world = GetWorld(senderConnection);
World *world = GetWorld();
world->LockMutex();
Entity *entity = world->CreateEntity(id);

Expand Down Expand Up @@ -247,7 +247,7 @@ namespace Exodia::Network {

void Network::ReceiveConnect(RECEIVE_ARG) {

asio::ip::udp::endpoint senderEndpoint = senderConnection.GetEndpoint();
asio::ip::udp::endpoint senderEndpoint = senderConnection->GetEndpoint();

// Check if already Connected
if (_connections.find(STRING_FROM_ENDPOINT(senderEndpoint)) != _connections.end()) {
Expand All @@ -266,15 +266,15 @@ namespace Exodia::Network {
const std::string name = STRING_FROM_ENDPOINT(senderEndpoint);
auto find = _connections.find(name);
if (find == _connections.end())
_connections[STRING_FROM_ENDPOINT(senderEndpoint)] = Connection(senderEndpoint);
_connections[STRING_FROM_ENDPOINT(senderEndpoint)] = std::make_shared<Connection>(senderEndpoint);
std::string connect = "Connected to " + STRING_FROM_ENDPOINT(senderEndpoint);
EXODIA_CORE_INFO(connect);

Connection connection = _connections[STRING_FROM_ENDPOINT(senderEndpoint)];
std::shared_ptr<Connection> connection = _connections[STRING_FROM_ENDPOINT(senderEndpoint)];
std::shared_ptr<Exodia::Network::Packet> packet = std::make_shared<Exodia::Network::Packet>(CONNECT_ACCEPT);
packet->SetContent(buffer.ToVector());
std::cout << "Send accept connect" << std::endl;
connection.SendPacket(_socket, packet);
connection->SendPacket(_socket, packet);
}

/**
Expand All @@ -287,7 +287,7 @@ namespace Exodia::Network {
* @param senderEndpoint (Type: asio::ip::udp::endpoint) The endpoint of the sender
*/
void Network::ReceiveEvent(RECEIVE_ARG) {
asio::ip::udp::endpoint senderEndpoint = senderConnection.GetEndpoint();
asio::ip::udp::endpoint senderEndpoint = senderConnection->GetEndpoint();
(void)size;
(void)message;
float Timestamp = 0;
Expand Down Expand Up @@ -352,11 +352,12 @@ namespace Exodia::Network {
commands[DISCONNECT] = COMMAND_NETWORK(Network::ReceiveDisconnect); // Reject client connection
commands[EVENT] = COMMAND_NETWORK(Network::ReceiveEvent); // Send an event

Connection &senderConnection = _server_connection;
std::shared_ptr<Connection>senderConnection = _server_connection;

if (header.getCommand() == CONNECT) {
senderConnection = Connection(senderEndpoint);
senderConnection = std::make_shared<Connection>(senderEndpoint);
} else {
if (_connections.size() > 0) {
if (_networkType == NetworkType::SERVER) {
auto find = _connections.find(STRING_FROM_ENDPOINT(senderEndpoint));
if (find == _connections.end()) {
EXODIA_CORE_WARN("Network::Splitter() - Connection not found !");
Expand All @@ -367,7 +368,7 @@ namespace Exodia::Network {
senderConnection = _server_connection;
}
}
// if (senderConnection.GetLastId() >= (int)header.getId()) {
// if (senderConnection->GetLastId() >= (int)header.getId()) {
// return;
// }
EXODIA_CORE_INFO("Receive packet {0}", header.toString());
Expand All @@ -380,14 +381,14 @@ namespace Exodia::Network {
buffer.Write(&id, sizeof(uint64_t));
std::vector<char> buffer_vector(buffer.ToVector());
ack->SetContent(buffer_vector);
senderConnection.SendPacket(_socket, ack);
senderConnection->SendPacket(_socket, ack);
}

senderConnection.AddReceivedPacket();
senderConnection.AddKyloByteReceived(packet);
senderConnection.SetLastPacketReceived(packet);
senderConnection->AddReceivedPacket();
senderConnection->AddKyloByteReceived(packet);
senderConnection->SetLastPacketReceived(packet);
commands[header.getCommand()](content, header.getSize(), senderConnection, header);
senderConnection.SetLastId(header.getId());
senderConnection->SetLastId(header.getId());

if (_connections.size() > 0) {
auto find = _connections.find(STRING_FROM_ENDPOINT(senderEndpoint));
Expand Down
30 changes: 15 additions & 15 deletions GameEngine/src/Network/NetworkSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Exodia::Network {
void Network::SendPacket(std::shared_ptr<Packet> packet) {
if (_networkType == NetworkType::SERVER) {
for (auto &connection : _connections)
connection.second.SendPacket(_socket, packet);
connection.second->SendPacket(_socket, packet);
} else {
_server_connection.SendPacket(_socket, packet);
_server_connection->SendPacket(_socket, packet);
}
}

Expand All @@ -46,7 +46,7 @@ namespace Exodia::Network {
Buffer buffer(0);

packet->SetContent(buffer);
_server_connection.SendPacket(_socket, packet);
_server_connection->SendPacket(_socket, packet);
}

/**
Expand All @@ -60,26 +60,26 @@ namespace Exodia::Network {

if (_networkType == NetworkType::SERVER) {
for (auto &connection : _connections) {
int32_t received = connection.second.GetReceivedPacket();
int32_t sent = connection.second.GetSendPacket();
int32_t received = connection.second->GetReceivedPacket();
int32_t sent = connection.second->GetSendPacket();

buffer.Write(&received, sizeof(int));
buffer.Write(&sent, sizeof(int));
packet->SetContent(buffer);
connection.second.SetReceivedPacket(0);
connection.second.SetSendPacket(0);
connection.second.SendPacket(_socket, packet);
connection.second->SetReceivedPacket(0);
connection.second->SetSendPacket(0);
connection.second->SendPacket(_socket, packet);
}
} else {
int32_t received = _server_connection.GetReceivedPacket();
int32_t sent = _server_connection.GetSendPacket();
int32_t received = _server_connection->GetReceivedPacket();
int32_t sent = _server_connection->GetSendPacket();

buffer.Write(&received, sizeof(int));
buffer.Write(&sent, sizeof(int));
packet->SetContent(buffer);
_server_connection.SetReceivedPacket(0);
_server_connection.SetSendPacket(0);
_server_connection.SendPacket(_socket, packet);
_server_connection->SetReceivedPacket(0);
_server_connection->SetSendPacket(0);
_server_connection->SendPacket(_socket, packet);
}
}

Expand Down Expand Up @@ -240,8 +240,8 @@ namespace Exodia::Network {
void Network::ResendNeedAck() {
if (_networkType == NetworkType::SERVER) {
for (auto &connection : _connections)
connection.second.ResendNeedAck(_socket);
connection.second->ResendNeedAck(_socket);
} else
_server_connection.ResendNeedAck(_socket);
_server_connection->ResendNeedAck(_socket);
}
}; // namespace Exodia::Network
16 changes: 8 additions & 8 deletions Server/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ namespace Exodia {
if (_Network.GetConnections().empty())
EXODIA_TRACE("\t-> No clients connected");

for (std::pair<const std::string, Connection> connection : _Network.GetConnections())
EXODIA_TRACE("\t-> IP '{0}' : Port '{1}'", connection.second.GetEndpoint().port(),
connection.second.GetEndpoint().address().to_string());
for (std::pair<const std::string, std::shared_ptr<Connection>> connection : _Network.GetConnections())
EXODIA_TRACE("\t-> IP '{0}' : Port '{1}'", connection.second->GetEndpoint().port(),
connection.second->GetEndpoint().address().to_string());
EXODIA_TRACE("\nEntities '{0}'", _WorldNetwork->GetCount());
} else if (command == "packet")
_Network.SendPacketInfo();
Expand Down Expand Up @@ -304,7 +304,7 @@ namespace Exodia {
*
* @return a boolean value, which indicates whether the client is new or not.
*/
bool Server::IsClientNew(std::pair<const std::string, Connection> connection) {
bool Server::IsClientNew(std::pair<const std::string, std::shared_ptr<Connection>> connection) {
bool newClient = true;

for (User user : _Users) {
Expand Down Expand Up @@ -348,11 +348,11 @@ namespace Exodia {
*/
void Server::CheckForNewClients() {
Entity *player = nullptr;
std::map<std::string, Connection> connections = _Network.GetConnections();
std::map<std::string, std::shared_ptr<Connection>> connections = _Network.GetConnections();

if (connections.empty())
return;
for (std::pair<const std::string, Connection> connection : connections) {
for (std::pair<const std::string, std::shared_ptr<Connection>> connection : connections) {
if (IsClientNew(connection)) {
uint32_t userID = (uint32_t)_Users.size();
Entity *entity = Scenes[GAME]->CreateEntity("Player_" + std::to_string(userID));
Expand Down Expand Up @@ -381,8 +381,8 @@ namespace Exodia {
for (auto connection : _Network.GetConnections()) {
for (auto user : _Users) {
if (user.GetConnection() == connection.second) {
if (connection.second.GetNetworkInfo().lastPacketReceived->GetHeader().getTimestamp() <
connection.second.GetNetworkInfo().lastPacketSent->GetHeader().getTimestamp() - 10000) {
if (connection.second->GetNetworkInfo().lastPacketReceived->GetHeader().getTimestamp() <
connection.second->GetNetworkInfo().lastPacketSent->GetHeader().getTimestamp() - 10000) {
EXODIA_CORE_INFO("Client disconnected");
_Network.SendDisconnect();
_Network.Disconnect(connection.second);
Expand Down
2 changes: 1 addition & 1 deletion Server/src/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Exodia {
void InitEntities();
void WaitForClient();
void HandleEvent(std::pair<std::pair<uint32_t, bool>, asio::ip::udp::endpoint> event);
bool IsClientNew(std::pair<const std::string, Connection> connection);
bool IsClientNew(std::pair<const std::string, std::shared_ptr<Connection>> connection);
void SendComponents(SceneType scene);

////////////////
Expand Down
Loading

0 comments on commit 391550f

Please sign in to comment.