R-Type  2
Doom but in better
Loading...
Searching...
No Matches
NetworkManager.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** NetworkManager.hpp
4** File description:
5** Handle UDP sockets & Send/Receive messages between server and client
6*/
7
15#pragma once
16
17#include <string>
18#include <vector>
19#include <asio.hpp>
20#include <sstream>
21#include <array>
22#include <iostream>
23#include <vector>
24#include <iomanip>
25#include <cstdint>
26#include <cstring>
27
28#include "ProtocolHandler.hpp"
29#include "Log.hpp"
30#include "LogMacros.hpp"
31#include "Utilities.hpp"
34
35namespace GUI
36{
37 namespace Network
38 {
48 public:
53 NetworkManager(const std::uint32_t entityId = 0);
54
64 void initialize();
70 void handleMessages();
79 void sendMessage(const MessageNode &message);
80
84 void startGame();
85
91 void setPort(const unsigned int port);
97 void setIp(const std::string &ip);
103 void setPlayerName(const std::string &name);
110 void setAddress(const std::string &ip, const unsigned int port);
111
117 const bool isConnected() const;
118
126 float bytesToFloat(const uint8_t *bytes);
134 std::string bytesToHex(const std::vector<uint8_t> &bytes);
145 MessageNode translateMessage(const std::vector<uint8_t> &message);
153 std::string convertMessageToString(const MessageNode &message);
160 void receiveMessage();
161
163
165
166 std::vector<GUI::Network::MessageNode> getBufferedMessages();
167
168 private:
174 void _connect();
180 void _disconnect();
181
182 bool _connectionActive = false;
183 bool _continueListening = true;
184 unsigned int _port = 9000;
185 std::vector<MessageNode> _bufferedMessages;
186 std::string _ip = "127.0.0.1";
187 std::string _playerName = "Player";
188 asio::io_context _ioContext;
189 asio::ip::udp::socket _socket{ _ioContext };
190 };
191 }
192}
This is the file that links the EntityNode children.
Macro definitions for logging messages with varying levels of detail and formatting.
This is the file in charge of containing the Log class (the one in charge of outputing info only when...
This file defines the Packet class responsible for serializing and deserializing a custom binary UDP ...
A class that manages the network communication through UDP sockets.
const bool isConnected() const
Checks if the connection is active.
void setAddress(const std::string &ip, const unsigned int port)
Sets both the IP address and port for communication.
~NetworkManager()
Destroy the Network Manager object.
std::vector< GUI::Network::MessageNode > getBufferedMessages()
NetworkManager(const std::uint32_t entityId=0)
Constructs a NetworkManager instance with an optional entity ID.
void startGame()
starts a game by sending CONNECT TO server.
void sendMessage(const MessageNode &message)
Sends a message to the server.
void initialize()
Initializes the UDP socket and binds it to a local endpoint. This function opens the socket and binds...
std::string bytesToHex(const std::vector< uint8_t > &bytes)
Converts a byte array to a hexadecimal string representation.
void setPlayerName(const std::string &name)
Sets the player's name for identification.
void handleMessages()
Handles incoming messages on the UDP socket.
void setIp(const std::string &ip)
Sets the IP address for communication.
void receiveMessage()
Receives messages continuously from the server.
std::string convertMessageToString(const MessageNode &message)
Translates a MessageNode into a string.
void setPort(const unsigned int port)
Sets the port for communication.
MessageNode translateMessage(const std::vector< uint8_t > &message)
Translates a message into a MessageNode.
float bytesToFloat(const uint8_t *bytes)
Converts a byte array to a floating-point value.