![]() |
R-Type
2
Doom but in better
|
A class that manages the network communication through UDP sockets. More...
#include <NetworkManager.hpp>
Public Member Functions | |
NetworkManager (const std::uint32_t entityId=0) | |
Constructs a NetworkManager instance with an optional entity ID. | |
~NetworkManager () | |
Destroy the Network Manager object. | |
void | initialize () |
Initializes the UDP socket and binds it to a local endpoint. This function opens the socket and binds it to the specified port. If initialization fails, an error message will be printed. | |
void | handleMessages () |
Handles incoming messages on the UDP socket. | |
void | sendMessage (const MessageNode &message) |
Sends a message to the server. | |
void | startGame () |
starts a game by sending CONNECT TO server. | |
void | setPort (const unsigned int port) |
Sets the port for communication. | |
void | setIp (const std::string &ip) |
Sets the IP address for communication. | |
void | setPlayerName (const std::string &name) |
Sets the player's name for identification. | |
void | setAddress (const std::string &ip, const unsigned int port) |
Sets both the IP address and port for communication. | |
const bool | isConnected () const |
Checks if the connection is active. | |
float | bytesToFloat (const uint8_t *bytes) |
Converts a byte array to a floating-point value. | |
std::string | bytesToHex (const std::vector< uint8_t > &bytes) |
Converts a byte array to a hexadecimal string representation. | |
MessageNode | translateMessage (const std::vector< uint8_t > &message) |
Translates a message into a MessageNode. | |
std::string | convertMessageToString (const MessageNode &message) |
Translates a MessageNode into a string. | |
void | receiveMessage () |
Receives messages continuously from the server. | |
void | startReceivingMessages () |
void | stopReceivingMessages () |
std::vector< GUI::Network::MessageNode > | getBufferedMessages () |
![]() | |
EntityNode (const size_t entityId=0) | |
virtual | ~EntityNode ()=default |
virtual size_t | getEntityNodeId () const |
Additional Inherited Members | |
![]() | |
size_t | _entityID |
A class that manages the network communication through UDP sockets.
This class is responsible for initializing the UDP socket, sending and receiving messages, and interpreting messages between the client and server. It can send and receive messages in various formats and manage the connection state.
Definition at line 47 of file NetworkManager.hpp.
GUI::Network::NetworkManager::NetworkManager | ( | const std::uint32_t | entityId = 0 | ) |
Constructs a NetworkManager instance with an optional entity ID.
entityId | The unique identifier for the entity (default is 0). |
Definition at line 16 of file NetworkManager.cpp.
GUI::Network::NetworkManager::~NetworkManager | ( | ) |
Destroy the Network Manager object.
Definition at line 18 of file NetworkManager.cpp.
float GUI::Network::NetworkManager::bytesToFloat | ( | const uint8_t * | bytes | ) |
Converts a byte array to a floating-point value.
bytes | Pointer to the byte array representing a float. |
Definition at line 100 of file NetworkManager.cpp.
std::string GUI::Network::NetworkManager::bytesToHex | ( | const std::vector< uint8_t > & | bytes | ) |
Converts a byte array to a hexadecimal string representation.
bytes | The byte array to convert. |
Definition at line 107 of file NetworkManager.cpp.
std::string GUI::Network::NetworkManager::convertMessageToString | ( | const MessageNode & | message | ) |
Translates a MessageNode into a string.
message | The MessageNode to be converted. |
Definition at line 222 of file NetworkManager.cpp.
std::vector< GUI::Network::MessageNode > GUI::Network::NetworkManager::getBufferedMessages | ( | ) |
Definition at line 356 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::handleMessages | ( | ) |
Handles incoming messages on the UDP socket.
This function listens for messages from the server, deserializes them, and processes the received data.
Definition at line 36 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::initialize | ( | ) |
Initializes the UDP socket and binds it to a local endpoint. This function opens the socket and binds it to the specified port. If initialization fails, an error message will be printed.
Definition at line 25 of file NetworkManager.cpp.
const bool GUI::Network::NetworkManager::isConnected | ( | ) | const |
Checks if the connection is active.
Definition at line 93 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::receiveMessage | ( | ) |
Receives messages continuously from the server.
This function waits for incoming messages and processes them by calling translateMessage()
to display them in a human-readable format.
Definition at line 310 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::sendMessage | ( | const MessageNode & | message | ) |
Sends a message to the server.
message | The message to send. |
This function will check if the socket is connected, validate the message size, and send the message to the server.
Definition at line 56 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::setAddress | ( | const std::string & | ip, |
const unsigned int | port ) |
Sets both the IP address and port for communication.
ip | The IP address to set. |
port | The port number to set. |
Definition at line 299 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::setIp | ( | const std::string & | ip | ) |
Sets the IP address for communication.
ip | The IP address to set. |
Definition at line 279 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::setPlayerName | ( | const std::string & | name | ) |
Sets the player's name for identification.
name | The player's name. |
Definition at line 290 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::setPort | ( | const unsigned int | port | ) |
Sets the port for communication.
port | The port number to set. |
Definition at line 268 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::startGame | ( | ) |
starts a game by sending CONNECT TO server.
Definition at line 82 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::startReceivingMessages | ( | ) |
Definition at line 346 of file NetworkManager.cpp.
void GUI::Network::NetworkManager::stopReceivingMessages | ( | ) |
Definition at line 351 of file NetworkManager.cpp.
GUI::Network::MessageNode GUI::Network::NetworkManager::translateMessage | ( | const std::vector< uint8_t > & | message | ) |
Translates a message into a MessageNode.
message | The byte array representing the message. |
This function decodes different message types such as connect, disconnect, move, shoot, etc. and returns a description of the message contents.
Definition at line 116 of file NetworkManager.cpp.