improved program startup stability

This commit is contained in:
study-faraphel 2024-11-10 14:45:27 +01:00
parent 0745b725a7
commit ec72e8888b
4 changed files with 18 additions and 55 deletions

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.28)
cmake_minimum_required(VERSION 3.25)
project(M2-PT-DRP LANGUAGES CXX)

View file

@ -22,7 +22,7 @@
#include "tasks/undefined/UndefinedTask.hpp"
EventManager::EventManager() {
EventManager::EventManager(const std::string& address, const std::string& port, const bool useIpv6) {
// register the different events type
this->eventRegistry = {
{drp::event::EventType::PONG, std::make_shared<drp::event::PongEvent>()},
@ -40,7 +40,8 @@ EventManager::EventManager() {
// hints for the communication
addrinfo addressHints {};
addressHints.ai_family = AF_INET6;
addressHints.ai_family = useIpv6 ? AF_INET6 : AF_INET;
// addressHints.ai_flags = AI_PASSIVE;
addressHints.ai_socktype = SOCK_DGRAM;
addressHints.ai_protocol = IPPROTO_UDP;
@ -52,17 +53,25 @@ EventManager::EventManager() {
);
if (this->context.socket < 0)
throw std::runtime_error("[Receiver] Could not create the socket: " + std::string(strerror(errno)));
throw std::runtime_error("Could not create the socket: " + std::string(strerror(errno)));
// get the information for the broadcast local-link address
// TODO(Faraphel): ip / port as argument ?
if(const int error = getaddrinfo(
"ff02::1",
"5650",
address.c_str(),
port.c_str(),
&addressHints,
&context.broadcastAddressInfo
) != 0)
throw std::runtime_error("[Sender] Could not get the address: " + std::string(gai_strerror(error)));
throw std::runtime_error("Could not get the address: " + std::string(gai_strerror(error)));
// bind the socket to the address
if (bind(
this->context.socket,
this->context.broadcastAddressInfo->ai_addr,
this->context.broadcastAddressInfo->ai_addrlen
) < 0)
throw std::runtime_error("Could not bind to the address: " + std::string(strerror(errno)));
// generate a random identifier for ourselves
std::random_device randomDevice;
@ -95,23 +104,6 @@ void EventManager::loop() {
void EventManager::loopSender() {
addrinfo* destinationInfo;
addrinfo addressHints {};
addressHints.ai_family = AF_INET; // TODO: AF_INET6
addressHints.ai_socktype = SOCK_DGRAM;
addressHints.ai_protocol = IPPROTO_UDP;
// get the information for the broadcast local-link address
// TODO(Faraphel): ip / port as argument ?
if(const int error = getaddrinfo(
"ff02::1",
"5650",
&addressHints,
&destinationInfo
) != 0)
throw std::runtime_error("[Sender] Could not get the address: " + std::string(gai_strerror(error)));
while (true) {
std::cout << "[Sender] Status: " + std::to_string(static_cast<int>(this->context.me.status)) << std::endl;
@ -127,9 +119,6 @@ void EventManager::loopSender() {
// ask the task class to handle the task
task->handle(this->context);
}
// free the address
freeaddrinfo(destinationInfo);
}
@ -140,32 +129,6 @@ void EventManager::loopReceiver() {
drp::packet::GenericPacket packet {};
drp::packet::GenericPacketContent packetContent {};
addrinfo addressHints {};
addressHints.ai_family = AF_INET; // TODO: AF_INET6
addressHints.ai_socktype = SOCK_DGRAM;
addressHints.ai_protocol = IPPROTO_UDP;
// TODO(Faraphel): port as argument
addrinfo* senderInfo;
if(getaddrinfo(
"ff02::1", // hostname
"5650", // port
&addressHints,
&senderInfo
) != 0)
throw std::runtime_error("[Receiver] Could not get the address: " + std::string(gai_strerror(errno)));
// bind the socket to the address
if (bind(
this->context.socket,
senderInfo->ai_addr,
senderInfo->ai_addrlen
) < 0)
throw std::runtime_error("[Receiver] Could not bind to the address: " + std::string(strerror(errno)));
// free the sender address
freeaddrinfo(senderInfo);
// client loop
while (true) {
// receive new data

View file

@ -13,7 +13,7 @@
class EventManager {
public:
EventManager();
EventManager(const std::string& address, const std::string& port, bool useIpv6 = false);
void loop();
void loopSender();

View file

@ -13,7 +13,7 @@ int main(int argc, char* argv[]) {
if (Pa_Initialize() != paNoError)
throw std::runtime_error("Could not initialize PortAudio.");
EventManager eventManager;
EventManager eventManager = EventManager("127.0.0.1", "15650", false);
eventManager.loop();
// terminate the libraries