Babel  1
The voip software that only works on your local network
Loading...
Searching...
No Matches
UDP.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** send_receive
4** File description:
5** UDP.cpp
6*/
7
14#include "Network/UDP.hpp"
15
16namespace Network
17{
26 UDP::UDP(asio::io_context &io_context, const std::string &ip, int port, bool is_sender)
27 : _socket(io_context), _is_sender(is_sender)
28 {
29 if (_is_sender) {
30 _socket.open(asio::ip::udp::v4());
31 } else {
32 asio::ip::udp::endpoint endpoint(asio::ip::address::from_string(ip), port);
33 _socket.open(endpoint.protocol());
34 _socket.bind(endpoint);
35 }
36 _socket.non_blocking(true);
37 }
38
43 {
44 _socket.close();
45 }
46
54 void UDP::sendTo(const std::string &message, const std::string &address, int port)
55 {
56 asio::ip::udp::endpoint endpoint(asio::ip::address::from_string(address), port);
57 _socket.send_to(asio::buffer(message), endpoint);
58 }
59
68 void UDP::sendRaw(const char *data, std::size_t size, const std::string &address, int port)
69 {
70 asio::ip::udp::endpoint endpoint(asio::ip::address::from_string(address), port);
71 _socket.send_to(asio::buffer(data, size), endpoint);
72 }
73
81 std::string UDP::receiveFrom(std::string &address, int &port)
82 {
83 char buffer[1024];
84 asio::ip::udp::endpoint sender_endpoint;
85 std::error_code ec; // Error code object
86
87 size_t len = _socket.receive_from(asio::buffer(buffer), sender_endpoint, 0, ec);
88
89 if (ec == asio::error::would_block) {
90 PRETTY_WARNING << "Would block" << std::endl;
91 return "";
92 }
93
94 if (ec) {
95 PRETTY_WARNING << "Receive failed: " << ec.message() << std::endl;
96 throw std::runtime_error("Receive failed: " + ec.message());
97 }
98
99 address = sender_endpoint.address().to_string();
100 port = sender_endpoint.port();
101
102 std::string received_data(buffer, len);
103 _received_packets.push(received_data);
104 return received_data;
105 }
106
113 {
114 return _socket.is_open();
115 }
116
122 std::string UDP::fetchPacket()
123 {
124 if (_received_packets.empty()) {
125 return "";
126 }
127 std::string packet = _received_packets.front();
128 _received_packets.pop();
129 return packet;
130 }
131}
#define PRETTY_WARNING
Warning log with details and colour.
This file contains the definition of the UDP class for handling UDP network communication.
std::string fetchPacket()
Fetch a packet from the received packets queue.
Definition UDP.cpp:122
void sendRaw(const char *data, std::size_t size, const std::string &address, int port)
Send raw data to a specified address and port.
Definition UDP.cpp:68
UDP(asio::io_context &io_context, const std::string &ip, int port, bool is_sender)
Construct a new UDP object.
Definition UDP.cpp:26
~UDP()
Destroy the UDP object.
Definition UDP.cpp:42
void sendTo(const std::string &message, const std::string &address, int port)
Send a message to a specified address and port.
Definition UDP.cpp:54
bool isConnectionAlive()
Check if the connection is alive.
Definition UDP.cpp:112
std::string receiveFrom(std::string &address, int &port)
Receive a message from a specified address and port.
Definition UDP.cpp:81