From ec72e8888ba2f88d6e4370235c70379c33ed986e Mon Sep 17 00:00:00 2001 From: study-faraphel Date: Sun, 10 Nov 2024 14:45:27 +0100 Subject: [PATCH] improved program startup stability --- CMakeLists.txt | 2 +- source/EventManager.cpp | 67 +++++++++-------------------------------- source/EventManager.hpp | 2 +- source/main.cpp | 2 +- 4 files changed, 18 insertions(+), 55 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 848c1bc..4a4439e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.28) +cmake_minimum_required(VERSION 3.25) project(M2-PT-DRP LANGUAGES CXX) diff --git a/source/EventManager.cpp b/source/EventManager.cpp index 389d43c..e206ce3 100644 --- a/source/EventManager.cpp +++ b/source/EventManager.cpp @@ -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()}, @@ -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(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 diff --git a/source/EventManager.hpp b/source/EventManager.hpp index cfe4479..6d4263d 100644 --- a/source/EventManager.hpp +++ b/source/EventManager.hpp @@ -13,7 +13,7 @@ class EventManager { public: - EventManager(); + EventManager(const std::string& address, const std::string& port, bool useIpv6 = false); void loop(); void loopSender(); diff --git a/source/main.cpp b/source/main.cpp index 371aa9f..fb5a201 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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