R-Type  2
Doom but in better
Loading...
Searching...
No Matches
GUI::Network::NetworkManager Class Reference

A class that manages the network communication through UDP sockets. More...

#include <NetworkManager.hpp>

Inheritance diagram for GUI::Network::NetworkManager:
Inheritance graph
Collaboration diagram for GUI::Network::NetworkManager:
Collaboration graph

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::MessageNodegetBufferedMessages ()
 
- Public Member Functions inherited from GUI::ECS::EntityNode
 EntityNode (const size_t entityId=0)
 
virtual ~EntityNode ()=default
 
virtual size_t getEntityNodeId () const
 

Additional Inherited Members

- Protected Attributes inherited from GUI::ECS::EntityNode
size_t _entityID
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ NetworkManager()

GUI::Network::NetworkManager::NetworkManager ( const std::uint32_t entityId = 0)

Constructs a NetworkManager instance with an optional entity ID.

Parameters
entityIdThe unique identifier for the entity (default is 0).

Definition at line 16 of file NetworkManager.cpp.

◆ ~NetworkManager()

GUI::Network::NetworkManager::~NetworkManager ( )

Destroy the Network Manager object.

Definition at line 18 of file NetworkManager.cpp.

Member Function Documentation

◆ bytesToFloat()

float GUI::Network::NetworkManager::bytesToFloat ( const uint8_t * bytes)

Converts a byte array to a floating-point value.

Parameters
bytesPointer to the byte array representing a float.
Returns
The corresponding floating-point value.

Definition at line 100 of file NetworkManager.cpp.

◆ bytesToHex()

std::string GUI::Network::NetworkManager::bytesToHex ( const std::vector< uint8_t > & bytes)

Converts a byte array to a hexadecimal string representation.

Parameters
bytesThe byte array to convert.
Returns
The hexadecimal string representing the byte array.

Definition at line 107 of file NetworkManager.cpp.

◆ convertMessageToString()

std::string GUI::Network::NetworkManager::convertMessageToString ( const MessageNode & message)

Translates a MessageNode into a string.

Parameters
messageThe MessageNode to be converted.
Returns
A string with bytes that corresponds to the message to be sent to server.

Definition at line 222 of file NetworkManager.cpp.

◆ getBufferedMessages()

std::vector< GUI::Network::MessageNode > GUI::Network::NetworkManager::getBufferedMessages ( )

Definition at line 356 of file NetworkManager.cpp.

◆ handleMessages()

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.

◆ initialize()

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.

◆ isConnected()

const bool GUI::Network::NetworkManager::isConnected ( ) const

Checks if the connection is active.

Returns
True if the socket is open and connected, false otherwise.

Definition at line 93 of file NetworkManager.cpp.

◆ receiveMessage()

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.

◆ sendMessage()

void GUI::Network::NetworkManager::sendMessage ( const MessageNode & message)

Sends a message to the server.

Parameters
messageThe 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.

◆ setAddress()

void GUI::Network::NetworkManager::setAddress ( const std::string & ip,
const unsigned int port )

Sets both the IP address and port for communication.

Parameters
ipThe IP address to set.
portThe port number to set.

Definition at line 299 of file NetworkManager.cpp.

◆ setIp()

void GUI::Network::NetworkManager::setIp ( const std::string & ip)

Sets the IP address for communication.

Parameters
ipThe IP address to set.

Definition at line 279 of file NetworkManager.cpp.

◆ setPlayerName()

void GUI::Network::NetworkManager::setPlayerName ( const std::string & name)

Sets the player's name for identification.

Parameters
nameThe player's name.

Definition at line 290 of file NetworkManager.cpp.

◆ setPort()

void GUI::Network::NetworkManager::setPort ( const unsigned int port)

Sets the port for communication.

Parameters
portThe port number to set.

Definition at line 268 of file NetworkManager.cpp.

◆ startGame()

void GUI::Network::NetworkManager::startGame ( )

starts a game by sending CONNECT TO server.

Definition at line 82 of file NetworkManager.cpp.

◆ startReceivingMessages()

void GUI::Network::NetworkManager::startReceivingMessages ( )

Definition at line 346 of file NetworkManager.cpp.

◆ stopReceivingMessages()

void GUI::Network::NetworkManager::stopReceivingMessages ( )

Definition at line 351 of file NetworkManager.cpp.

◆ translateMessage()

GUI::Network::MessageNode GUI::Network::NetworkManager::translateMessage ( const std::vector< uint8_t > & message)

Translates a message into a MessageNode.

Parameters
messageThe byte array representing the message.
Returns
A MessageNode representation of 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.


The documentation for this class was generated from the following files: