From c8d4b77a09127bb9cf2da13d6ccd6a772c08c8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Sat, 7 Dec 2024 16:58:01 +0000 Subject: [PATCH 1/8] Added logs and linter on Ethernet Mock --- .../Communication/Ethernet/Ethernet.cpp | 115 +++++++++--------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp b/Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp index c1aeac284..510df6088 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp @@ -1,73 +1,72 @@ -/* - * Ethernet.cpp - * - * Created on: Nov 23, 2022 - * Author: stefa - */ - #include "HALALMock/Services/Communication/Ethernet/Ethernet.hpp" -#include + #include +#include in_addr_t ipaddr, netmask, gw; uint8_t IP_ADDRESS[4], NETMASK_ADDRESS[4], GATEWAY_ADDRESS[4]; bool Ethernet::is_ready = false; bool Ethernet::is_running = false; -void Ethernet::start(string local_ip, string subnet_mask, string gateway){ - start(IPV4(local_ip), IPV4(subnet_mask), IPV4(gateway)); +void Ethernet::start(string local_ip, string subnet_mask, string gateway) { + start(IPV4(local_ip), IPV4(subnet_mask), IPV4(gateway)); } -void Ethernet::start(IPV4 local_ip, IPV4 subnet_mask, IPV4 gateway){ - if(!is_running && is_ready){ - ipaddr = local_ip.address; - netmask = subnet_mask.address; - gw = gateway.address; - IP_ADDRESS[0] = ipaddr & 0xFF; - IP_ADDRESS[1] = (ipaddr >> 8) & 0xFF; - IP_ADDRESS[2] = (ipaddr >> 16) & 0xFF; - IP_ADDRESS[3] = (ipaddr >> 24) & 0xFF; - NETMASK_ADDRESS[0] = netmask & 0xFF; - NETMASK_ADDRESS[1] = (netmask >> 8) & 0xFF; - NETMASK_ADDRESS[2] = (netmask >> 16) & 0xFF; - NETMASK_ADDRESS[3] = (netmask >> 24) & 0xFF; - GATEWAY_ADDRESS[0] = gw & 0xFF; - GATEWAY_ADDRESS[1] = (gw >> 8) & 0xFF; - GATEWAY_ADDRESS[2] = (gw >> 16) & 0xFF; - GATEWAY_ADDRESS[3] = (gw >> 24) & 0xFF; - is_running = true; - }else{ - std::cout<<"Unable to start Ethernet!\n"; - } - - if (not is_ready) { - std::cout<<"Ethernet is not ready\n"; - return; - } +void Ethernet::start(IPV4 local_ip, IPV4 subnet_mask, IPV4 gateway) { + if (!is_running && is_ready) { + ipaddr = local_ip.address; + netmask = subnet_mask.address; + gw = gateway.address; + IP_ADDRESS[0] = ipaddr & 0xFF; + IP_ADDRESS[1] = (ipaddr >> 8) & 0xFF; + IP_ADDRESS[2] = (ipaddr >> 16) & 0xFF; + IP_ADDRESS[3] = (ipaddr >> 24) & 0xFF; + NETMASK_ADDRESS[0] = netmask & 0xFF; + NETMASK_ADDRESS[1] = (netmask >> 8) & 0xFF; + NETMASK_ADDRESS[2] = (netmask >> 16) & 0xFF; + NETMASK_ADDRESS[3] = (netmask >> 24) & 0xFF; + GATEWAY_ADDRESS[0] = gw & 0xFF; + GATEWAY_ADDRESS[1] = (gw >> 8) & 0xFF; + GATEWAY_ADDRESS[2] = (gw >> 16) & 0xFF; + GATEWAY_ADDRESS[3] = (gw >> 24) & 0xFF; + is_running = true; + LOG_INFO("Ethernet started succesfully"); + } else { + LOG_ERROR("Unable to start Ethernet!"); + } + if (not is_ready) { + LOG_ERROR("Ethernet is not ready"); + return; + } } -void Ethernet::inscribe(){ - constexpr static uint8_t number_pin_ethernet = 10; - const static Pin pin_list_ethernet[number_pin_ethernet] = {PA1,PA2,PA7,PB13,PC1,PC4,PC5,PG11,PG0,PG13}; - if(!is_ready){ - for(size_t i = 0; i < number_pin_ethernet; i++){ - EmulatedPin &pin_data = SharedMemory::get_pin(pin_list_ethernet[i]); - if(pin_data.type == PinType::NOT_USED){ - pin_data.type = PinType::Ethernet; - }else{ - std::cout<<"Error inscribing ethernet Pins, PA1,PA2,PA7,PB13,PC1,PC4,PC5,PG11,PG0,PG13 must be free\n"; - return; - } - } - }else{ - std::cout<<"Unable to inscribe Ethernet because is already ready!\n"; - } +void Ethernet::inscribe() { + constexpr static uint8_t number_pin_ethernet = 10; + const static Pin pin_list_ethernet[number_pin_ethernet] = { + PA1, PA2, PA7, PB13, PC1, PC4, PC5, PG11, PG0, PG13}; + if (!is_ready) { + for (size_t i = 0; i < number_pin_ethernet; i++) { + EmulatedPin &pin_data = SharedMemory::get_pin(pin_list_ethernet[i]); + if (pin_data.type == PinType::NOT_USED) { + pin_data.type = PinType::Ethernet; + LOG_DEBUG("Pin subscribed succcesfully to Ethernet"); + } else { + LOG_ERROR( + "Error inscribing ethernet Pins, " + "PA1,PA2,PA7,PB13,PC1,PC4,PC5,PG11,PG0,PG13 must be free"); + return; + } + } + } else { + LOG_ERROR("Unable to inscribe Ethernet because is already ready!"); + } } -void Ethernet::update(){ - //I'm going to leave the case is not running so it warn the user to check if you have done inscribed - if(not is_running) { - std::cout<<"Ethernet is not running, check if its been inscribed\n"; - return; - } +void Ethernet::update() { + // I'm going to leave the case is not running so it warn the user to check + // if you have done inscribed + if (not is_running) { + LOG_ERROR("Ethernet is not running, check if its been inscribed"); + return; + } } From 0b2b9c665107495e680729a306d0b4e97773424f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Sat, 7 Dec 2024 17:19:50 +0000 Subject: [PATCH 2/8] Added logs and linter in ServerSocket mock --- .../Ethernet/TCP/ServerSocket.hpp | 5 +- .../Ethernet/TCP/ServerSocket.cpp | 421 ++++++++++-------- 2 files changed, 224 insertions(+), 202 deletions(-) diff --git a/Inc/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.hpp b/Inc/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.hpp index 786c174d9..0f659e46c 100644 --- a/Inc/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.hpp +++ b/Inc/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.hpp @@ -1,6 +1,5 @@ #pragma once -#ifdef STLIB_ETH #include "HALALMock/Services/Communication/Ethernet/EthernetNode.hpp" #include "HALALMock/Services/Communication/Ethernet/Ethernet.hpp" #include "HALALMock/Models/Packets/Packet.hpp" @@ -139,6 +138,4 @@ class ServerSocket : public OrderProtocol{ std::jthread receive_thread; std::mutex mutex; -}; - -#endif //STLIB_ETH +}; \ No newline at end of file diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp index 2c6923120..bbc06faaf 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp @@ -1,56 +1,69 @@ -#ifdef STLIB_ETH #include "HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.hpp" +#include "HALALMock/Services/Logger/Logger.hpp" + #define MAX_SIZE_BUFFER 1024 uint8_t ServerSocket::priority = 1; -unordered_map ServerSocket::listening_sockets = {}; +unordered_map ServerSocket::listening_sockets = {}; ServerSocket::ServerSocket() = default; -ServerSocket::ServerSocket(IPV4 local_ip, uint32_t local_port) : local_ip(local_ip),local_port(local_port){ - if(not Ethernet::is_running) { - cout<<"Cannot declare UDP socket before Ethernet::start()\n"; - return; - } - tx_packet_buffer = {}; - rx_packet_buffer = {}; - state = INACTIVE; - configure_server_socket_and_listen(); +ServerSocket::ServerSocket(IPV4 local_ip, uint32_t local_port) + : local_ip(local_ip), local_port(local_port) { + if (not Ethernet::is_running) { + LOG_ERROR("Cannot declare UDP socket before Ethernet::start()"); + return; + } + tx_packet_buffer = {}; + rx_packet_buffer = {}; + state = INACTIVE; + configure_server_socket_and_listen(); } - -ServerSocket::ServerSocket(IPV4 local_ip, uint32_t local_port, uint32_t inactivity_time_until_keepalive, uint32_t space_between_tries, uint32_t tries_until_disconnection): ServerSocket(local_ip, local_port){ - keepalive_config.inactivity_time_until_keepalive = inactivity_time_until_keepalive; - keepalive_config.space_between_tries = space_between_tries; - keepalive_config.tries_until_disconnection = tries_until_disconnection; +ServerSocket::ServerSocket(IPV4 local_ip, uint32_t local_port, + uint32_t inactivity_time_until_keepalive, + uint32_t space_between_tries, + uint32_t tries_until_disconnection) + : ServerSocket(local_ip, local_port) { + keepalive_config.inactivity_time_until_keepalive = + inactivity_time_until_keepalive; + keepalive_config.space_between_tries = space_between_tries; + keepalive_config.tries_until_disconnection = tries_until_disconnection; } - -ServerSocket::ServerSocket(ServerSocket&& other) : local_ip(move(other.local_ip)), local_port(move(other.local_port)),state(other.state){ - listening_sockets[local_port] = this; - tx_packet_buffer = {}; - rx_packet_buffer = {}; +ServerSocket::ServerSocket(ServerSocket&& other) + : local_ip(move(other.local_ip)), + local_port(move(other.local_port)), + state(other.state) { + listening_sockets[local_port] = this; + tx_packet_buffer = {}; + rx_packet_buffer = {}; } -void ServerSocket::operator=(ServerSocket&& other){ - local_ip = move(other.local_ip); - local_port = move(other.local_port); - state = other.state; - listening_sockets[local_port] = this; - tx_packet_buffer = {}; - rx_packet_buffer = {}; - if(not (std::find(OrderProtocol::sockets.begin(), OrderProtocol::sockets.end(), this) != OrderProtocol::sockets.end())) - OrderProtocol::sockets.push_back(this); +void ServerSocket::operator=(ServerSocket&& other) { + local_ip = move(other.local_ip); + local_port = move(other.local_port); + state = other.state; + listening_sockets[local_port] = this; + tx_packet_buffer = {}; + rx_packet_buffer = {}; + if (not(std::find(OrderProtocol::sockets.begin(), + OrderProtocol::sockets.end(), + this) != OrderProtocol::sockets.end())) + OrderProtocol::sockets.push_back(this); } -ServerSocket::~ServerSocket(){ - //el destructor no destruye - auto it = std::find(OrderProtocol::sockets.begin(), OrderProtocol::sockets.end(), this); - if(it == OrderProtocol::sockets.end()) return; - else OrderProtocol::sockets.erase(it); - // Clean all descriptors - if (client_fd != -1) { +ServerSocket::~ServerSocket() { + // el destructor no destruye + auto it = std::find(OrderProtocol::sockets.begin(), + OrderProtocol::sockets.end(), this); + if (it == OrderProtocol::sockets.end()) + return; + else + OrderProtocol::sockets.erase(it); + // Clean all descriptors + if (client_fd != -1) { ::close(client_fd); client_fd = -1; } @@ -58,27 +71,27 @@ ServerSocket::~ServerSocket(){ ::close(server_socket_fd); server_socket_fd = -1; } - //clean the transmisions buffers - while (!tx_packet_buffer.empty()) { + // clean the transmisions buffers + while (!tx_packet_buffer.empty()) { tx_packet_buffer.pop(); } while (!rx_packet_buffer.empty()) { rx_packet_buffer.pop(); } - //eliminate the threads - if(state == LISTENING){ - ~listening_thread(); - }else if(state == ACCEPTED){ - ~receive_thread(); - } - + // eliminate the threads + if (state == LISTENING) { + ~listening_thread(); + } else if (state == ACCEPTED) { + ~receive_thread(); + } } -ServerSocket::ServerSocket(EthernetNode local_node) : ServerSocket(local_node.ip,local_node.port){}; +ServerSocket::ServerSocket(EthernetNode local_node) + : ServerSocket(local_node.ip, local_node.port) {}; -void ServerSocket::close(){ - // Clean all descriptors - if (client_fd != -1) { +void ServerSocket::close() { + // Clean all descriptors + if (client_fd != -1) { ::close(client_fd); client_fd = -1; } @@ -86,193 +99,205 @@ void ServerSocket::close(){ ::close(server_socket_fd); server_socket_fd = -1; } - //clean the transmisions buffers - while (!tx_packet_buffer.empty()) { + // clean the transmisions buffers + while (!tx_packet_buffer.empty()) { tx_packet_buffer.pop(); } while (!rx_packet_buffer.empty()) { rx_packet_buffer.pop(); } - //eliminate the threads - if(state == LISTENING){ - ~listening_thread(); - }else if(state == ACCEPTED){ - ~receive_thread(); - } - listening_sockets[local_port] = this; - state = CLOSED; + // eliminate the threads + if (state == LISTENING) { + ~listening_thread(); + } else if (state == ACCEPTED) { + ~receive_thread(); + } + listening_sockets[local_port] = this; + state = CLOSED; } -void ServerSocket::process_data(){ - while(!rx_packet_buffer.empty()){ - { - std::lock_guard lock(mtx); - Packet *packet = rx_packet_buffer.front(); - rx_packet_buffer.pop(); - } - uint8_t* new_data = (uint8_t*)(packet->build()); - Order::process_data(this, new_data); - } +void ServerSocket::process_data() { + while (!rx_packet_buffer.empty()) { + { + std::lock_guard lock(mtx); + Packet* packet = rx_packet_buffer.front(); + rx_packet_buffer.pop(); + } + uint8_t* new_data = (uint8_t*)(packet->build()); + Order::process_data(this, new_data); + } } -bool ServerSocket::add_order_to_queue(Order& order){ - if(state == ACCEPTED){ - return false; - } +bool ServerSocket::add_order_to_queue(Order& order) { + if (state == ACCEPTED) { + return false; + } if (order.get_size() == 0) { - std::cout << "Error: order is empty\n"; - return false; + LOG_ERROR("Order is empty"); + return false; } { - std::lock_guard lock(mutex); - tx_packet_buffer.push(order); + std::lock_guard lock(mutex); + tx_packet_buffer.push(order); } - return true; + return true; } -void ServerSocket::send(){ - std::lock_guard lock(mutex); - while (!tx_packet_buffer.empty()) { - Packet *packet = tx_packet_buffer.front(); - ssize_t sent_bytes = ::send(client_fd, packet->build(), packet->get_size(), 0); +void ServerSocket::send() { + std::lock_guard lock(mutex); + while (!tx_packet_buffer.empty()) { + Packet* packet = tx_packet_buffer.front(); + ssize_t sent_bytes = + ::send(client_fd, packet->build(), packet->get_size(), 0); if (sent_bytes < 0) { std::cerr << "Error sending packet\n"; - state = CLOSING; - close(); + state = CLOSING; + close(); return; } tx_packet_buffer.pop(); } } -bool ServerSocket::is_connected(){ - return state == ServerSocket::ServerState::ACCEPTED; +bool ServerSocket::is_connected() { + return state == ServerSocket::ServerState::ACCEPTED; } -void ServerSocket::create_server_socket(){ - server_socket_fd = socket(AF_INET, SOCK_STREAM, 0); - if(server_socket_fd == -1){ - std::cout<<"Socket creation failure\n"; - return; - } - //inset the local address and port - struct sockaddr_in server_socket_Address; - server_socket_Address.sin_family = AF_INET; - server_socket_Address.sin_addr.s_addr = local_ip.address; - server_socket_Address.sin_port = htons(local_port); - if(bind(server_socket_fd, (struct sockaddr*)&server_socket_Address, sizeof(server_socket_Address)) < 0){ - std::cout<<"Bind error\n"; - close(); - return; - } +void ServerSocket::create_server_socket() { + server_socket_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket_fd == -1) { + LOG_ERROR("Socket creation failure"); + return; + } + // inset the local address and port + struct sockaddr_in server_socket_Address; + server_socket_Address.sin_family = AF_INET; + server_socket_Address.sin_addr.s_addr = local_ip.address; + server_socket_Address.sin_port = htons(local_port); + if (bind(server_socket_fd, (struct sockaddr*)&server_socket_Address, + sizeof(server_socket_Address)) < 0) { + LOG_ERROR("Couldn't bind"); + close(); + return; + } } -bool ServerSocket::configure_server_socket(){ - //to reuse local address: - int opt = 1; - if (setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { - std::cerr << "Error setting SO_REUSEADDR\n"; - close(); - return false; - } - //disable naggle algorithm - int flag = 1; - if (setsockopt(server_socket_fd,IPPROTO_TCP,TCP_NODELAY,(char *) &flag, sizeof(int)) < 0){ - std::cout<<"It has been an error disabling Nagle's algorithm\n"; - return false; - } - //habilitate keepalives +bool ServerSocket::configure_server_socket() { + // to reuse local address: + int opt = 1; + if (setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, + sizeof(opt)) < 0) { + LOG_ERROR("Couldn't set SO_REUSEADDR"); + close(); + return false; + } + // disable naggle algorithm + int flag = 1; + if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, + sizeof(int)) < 0) { + LOG_ERROR("Can't disable Nagle's algorithm"); + return false; + } + // habilitate keepalives int optval = 1; - if (setsockopt(server_socket_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) { - std::cout << "ERROR configuring KEEPALIVES\n"; + if (setsockopt(server_socket_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, + sizeof(optval)) < 0) { + LOG_ERROR("Can't configure KEEPALIVES"); return false; } - // Configure TCP_KEEPIDLE it sets what time to wait to start sending keepalives - //different from lwip to linux - uint32_t tcp_keepidle_time = stkeepalive_config.inactivity_time_until_keepalive; - if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepidle_time, sizeof(tcp_keepidle_time)) < 0) { - std::cout << "Error configuring TCP_KEEPIDLE\n"; + // Configure TCP_KEEPIDLE it sets what time to wait to start sending + // keepalives + // different from lwip to linux + uint32_t tcp_keepidle_time = + stkeepalive_config.inactivity_time_until_keepalive; + if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPIDLE, + &tcp_keepidle_time, sizeof(tcp_keepidle_time)) < 0) { + LOG_ERROR("Can't configure TCP_KEEPIDLE"); return false; } - //interval between keepalives + // interval between keepalives uint32_t keep_interval_time = keepalive_config.space_between_tries; - if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPINTVL, &keep_interval_time, sizeof(keep_interval_time)) < 0) { - std::cout << "Error configuring TCP_KEEPINTVL\n"; + if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPINTVL, + &keep_interval_time, sizeof(keep_interval_time)) < 0) { + LOG_ERROR("Can't configure TCP_KEEPINTVL"); return false; } - // Configure TCP_KEEPCNT (number keepalives are send before considering the connection down) - uint32_t keep_cnt = keepalive_config.tries_until_disconnection; - if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_cnt, sizeof(keep_cnt)) < 0) { - std::cout << "Error to configure TCP_KEEPCNT\n"; + // Configure TCP_KEEPCNT (number keepalives are send before considering the + // connection down) + uint32_t keep_cnt = keepalive_config.tries_until_disconnection; + if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_cnt, + sizeof(keep_cnt)) < 0) { + LOG_ERROR("Can't configure TCP_KEEPCNT"); return false; } - return true; + return true; } -void ServerSocket::configure_server_socket_and_listen(){ - create_server_socket(); - if(!configure_server_socket()){ - cout<<"Error configuring ServerSocket\n"; - close(); - return; - } - if (listen(server_socket_fd, SOMAXCONN) < 0) { - std::cout"Error listening\n"; +void ServerSocket::configure_server_socket_and_listen() { + create_server_socket(); + if (!configure_server_socket()) { + LOG_ERROR("Can't configure ServerSocket"); close(); return; } - state = LISTENING; - listening_sockets[local_port] = this; - //create a thread to listen - listening_thread = std::jthread [&](){ - //solo aceptamos una conexion - struct sockaddr_in client_addr; - socklen_t client_len = sizeof(client_addr); - client_fd = accept(server_socket_fd,(struct sockaddr*)&client_addr, &client_len); - if(client_fd > 0){ - if(!accept_callback(client_fd,client_addr)){ - cout<<"Something went wrong in accept_callback\n"; - }else{ - OrderProtocol::sockets.push_back(this); - } - - }else{ - cout<< "Error accepting\n"; - close(); - return; - } - } -} -bool ServerSocket::accept_callback(int client_fd, sockaddr_in client_address){ - if(listening_sockets.contains(local_port) && state == LISTENING){ - state = ACCEPTED; - remote_ip = IPV4(client_address.sin_addr.s_addr); - rx_packet_buffer = {}; - handle_receive_from_client(client_fd); - return true; - }else{ - return false; - } - -} -void ServerSocket::handle_receive_from_client(int client_fd){ - receive_thread = std::jthread([client_fd]() { - uint8_t buffer[BUFFER_SIZE]; // Buffer for the data - ssize_t bytes_received; - while ((bytes_received = recv(client_fd, buffer, sizeof(buffer), 0)) > 0 && state == ACCEPTED){ - Packet* packet; - packet->parse(*buffer); - { - std::lock_guard lock(mtx); - rx_packet_buffer.push(&packet); - process_data(); - } + if (listen(server_socket_fd, SOMAXCONN) < 0) { + LOG_ERROR("Can't listen"); + close(); + return; } - //if receive a 0 means that the client has finished the connection so we will close this server_socket - if (bytes_received == 0) { - std::cout << "Client disconnected\n"; - }else if (bytes_received < 0) { - std::cout << "Error receiving data\n"; + state = LISTENING; + listening_sockets[local_port] = this; + // create a thread to listen + listening_thread = std::jthread[&]() { + // solo aceptamos una conexion + struct sockaddr_in client_addr; + socklen_t client_len = sizeof(client_addr); + client_fd = accept(server_socket_fd, (struct sockaddr*)&client_addr, + &client_len); + if (client_fd > 0) { + if (!accept_callback(client_fd, client_addr)) { + LOG_ERROR("Something went wrong in accept_callback"); + } else { + OrderProtocol::sockets.push_back(this); + } + + } else { + LOG_ERROR("Can't accept"); + close(); + return; + } } - close(); - }); } - -#endif //STLIB_ETH +bool ServerSocket::accept_callback(int client_fd, sockaddr_in client_address) { + if (listening_sockets.contains(local_port) && state == LISTENING) { + state = ACCEPTED; + remote_ip = IPV4(client_address.sin_addr.s_addr); + rx_packet_buffer = {}; + handle_receive_from_client(client_fd); + return true; + } else { + return false; + } +} +void ServerSocket::handle_receive_from_client(int client_fd) { + receive_thread = std::jthread([client_fd]() { + uint8_t buffer[BUFFER_SIZE]; // Buffer for the data + ssize_t bytes_received; + while ((bytes_received = recv(client_fd, buffer, sizeof(buffer), 0)) > + 0 && + state == ACCEPTED) { + Packet* packet; + packet->parse(*buffer); + { + std::lock_guard lock(mtx); + rx_packet_buffer.push(&packet); + process_data(); + } + } + // if receive a 0 means that the client has finished the connection so + // we will close this server_socket + if (bytes_received == 0) { + LOG_WARNING("Client disconnected"); + } else if (bytes_received < 0) { + LOG_ERROR("Error receiving data"); + } + close(); + }); +} \ No newline at end of file From 4b55bd5696667fa4aa5272062277ca69753cdf38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Mon, 9 Dec 2024 10:58:47 +0000 Subject: [PATCH 3/8] Minor refactor in logs in ServerSocket --- .../Ethernet/TCP/ServerSocket.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp index bbc06faaf..db772d19f 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp @@ -150,7 +150,7 @@ void ServerSocket::send() { ssize_t sent_bytes = ::send(client_fd, packet->build(), packet->get_size(), 0); if (sent_bytes < 0) { - std::cerr << "Error sending packet\n"; + LOG_ERROR(std::format("Error sending packet {}", packet->get_id())); state = CLOSING; close(); return; @@ -175,7 +175,8 @@ void ServerSocket::create_server_socket() { server_socket_Address.sin_port = htons(local_port); if (bind(server_socket_fd, (struct sockaddr*)&server_socket_Address, sizeof(server_socket_Address)) < 0) { - LOG_ERROR("Couldn't bind"); + LOG_ERROR(std::format("Couldn't bind to address {} in port {}", + local_ip.string_address, local_port)); close(); return; } @@ -225,7 +226,7 @@ bool ServerSocket::configure_server_socket() { uint32_t keep_cnt = keepalive_config.tries_until_disconnection; if (setsockopt(server_socket_fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_cnt, sizeof(keep_cnt)) < 0) { - LOG_ERROR("Can't configure TCP_KEEPCNT"); + LOG_ERROR("Can't configure TCP_KEEPCNT"); return false; } return true; @@ -233,12 +234,12 @@ bool ServerSocket::configure_server_socket() { void ServerSocket::configure_server_socket_and_listen() { create_server_socket(); if (!configure_server_socket()) { - LOG_ERROR("Can't configure ServerSocket"); + LOG_ERROR("Can't configure ServerSocket"); close(); return; } if (listen(server_socket_fd, SOMAXCONN) < 0) { - LOG_ERROR("Can't listen"); + LOG_ERROR("Can't listen"); close(); return; } @@ -253,13 +254,13 @@ void ServerSocket::configure_server_socket_and_listen() { &client_len); if (client_fd > 0) { if (!accept_callback(client_fd, client_addr)) { - LOG_ERROR("Something went wrong in accept_callback"); + LOG_ERROR("Something went wrong in accept_callback"); } else { OrderProtocol::sockets.push_back(this); } } else { - LOG_ERROR("Can't accept"); + LOG_ERROR("Can't accept"); close(); return; } @@ -294,9 +295,9 @@ void ServerSocket::handle_receive_from_client(int client_fd) { // if receive a 0 means that the client has finished the connection so // we will close this server_socket if (bytes_received == 0) { - LOG_WARNING("Client disconnected"); + LOG_WARNING("Client disconnected"); } else if (bytes_received < 0) { - LOG_ERROR("Error receiving data"); + LOG_ERROR("Error receiving data"); } close(); }); From 49331f6869029eb8c9fadab6a1f0cadad873a0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Mon, 9 Dec 2024 11:09:16 +0000 Subject: [PATCH 4/8] Modified Socket mock to use logs and applied linter --- .../Communication/Ethernet/TCP/Socket.cpp | 438 +++++++++--------- 1 file changed, 227 insertions(+), 211 deletions(-) diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp index 60c20139a..60d7281eb 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp @@ -1,280 +1,296 @@ -#ifdef STLIB_ETH #include "HALALMock/Services/Communication/Ethernet/TCP/Socket.hpp" #define BUFFER_SIZE 1024 -unordered_map Socket::connecting_sockets = {}; - +unordered_map Socket::connecting_sockets = {}; Socket::Socket() = default; -Socket::Socket(Socket&& other):remote_port(move(remote_port)),state(other.state){ - EthernetNode remote_node(other.remote_ip, other.remote_port); - connecting_sockets[remote_node] = this; +Socket::Socket(Socket&& other) + : remote_port(move(remote_port)), state(other.state) { + EthernetNode remote_node(other.remote_ip, other.remote_port); + connecting_sockets[remote_node] = this; } -void Socket::operator=(Socket&& other){ - remote_port = move(other.remote_port); - state = other.state; - EthernetNode remote_node(other.remote_ip, other.remote_port); - connecting_sockets[remote_node] = this; - if(std::find(OrderProtocol::sockets.begin(), OrderProtocol::sockets.end(), this) == OrderProtocol::sockets.end()) - OrderProtocol::sockets.push_back(this); +void Socket::operator=(Socket&& other) { + remote_port = move(other.remote_port); + state = other.state; + EthernetNode remote_node(other.remote_ip, other.remote_port); + connecting_sockets[remote_node] = this; + if (std::find(OrderProtocol::sockets.begin(), OrderProtocol::sockets.end(), + this) == OrderProtocol::sockets.end()) + OrderProtocol::sockets.push_back(this); } -Socket::~Socket(){ - auto it = std::find(OrderProtocol::sockets.begin(), OrderProtocol::sockets.end(), this); - if(it == OrderProtocol::sockets.end()) return; - else OrderProtocol::sockets.erase(it); - close(); +Socket::~Socket() { + auto it = std::find(OrderProtocol::sockets.begin(), + OrderProtocol::sockets.end(), this); + if (it == OrderProtocol::sockets.end()) + return; + else + OrderProtocol::sockets.erase(it); + close(); } -Socket::Socket(IPV4 local_ip, uint32_t local_port, IPV4 remote_ip, uint32_t remote_port,bool use_keep_alive): - local_ip(local_ip), local_port(local_port),remote_ip(remote_ip), remote_port(remote_port),use_keep_alives{use_keep_alive} -{ - if(not Ethernet::is_running) { - std::cout<<"Cannot declare TCP socket before Ethernet::start()"; - return; - } - state = INACTIVE; - tx_packet_buffer = {}; - rx_packet_buffer = {}; - EthernetNode remote_node(remote_ip, remote_port); - configure_socket_and_connect(); - OrderProtocol::sockets.push_back(this); +Socket::Socket(IPV4 local_ip, uint32_t local_port, IPV4 remote_ip, + uint32_t remote_port, bool use_keep_alive) + : local_ip(local_ip), + local_port(local_port), + remote_ip(remote_ip), + remote_port(remote_port), + use_keep_alives{use_keep_alive} { + if (not Ethernet::is_running) { + LOG_ERROR("Could not declare TCP socket before Ethernet::start()"); + return; + } + state = INACTIVE; + tx_packet_buffer = {}; + rx_packet_buffer = {}; + EthernetNode remote_node(remote_ip, remote_port); + configure_socket_and_connect(); + OrderProtocol::sockets.push_back(this); } -void Socket::create_socket(){ - //create socket not blocking - socket_fd = ::socket(AF_INET,SOCK_STREAM | SOCK_NONBLOCK,0); - //inset the local address and port - struct sockaddr_in socket_Address; - socket_Address.sin_family = AF_INET; - socket_Address.sin_addr.s_addr = local_ip.address; - socket_Address.sin_port = htons(local_port); - if(bind(socket_fd, (struct sockaddr*)&socket_Address, sizeof(socket_Address)) < 0){ - std::cout<<"Bind error\n"; - ::close(socket_fd); - return; - } +void Socket::create_socket() { + // create socket not blocking + socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + // inset the local address and port + struct sockaddr_in socket_Address; + socket_Address.sin_family = AF_INET; + socket_Address.sin_addr.s_addr = local_ip.address; + socket_Address.sin_port = htons(local_port); + if (bind(socket_fd, (struct sockaddr*)&socket_Address, + sizeof(socket_Address)) < 0) { + LOG_ERROR("Could not bind socket"); + ::close(socket_fd); + return; + } } -bool Socket::configure_socket(){ - //disable naggle algorithm - int flag = 1; - if (setsockopt(socket_fd,IPPROTO_TCP,TCP_NODELAY,(char *) &flag, sizeof(int)) < 0){ - std::cout<<"It has been an error disabling Nagle's algorithm\n"; - ::close(socket_fd); - return false; - } - //habilitate keepalives +bool Socket::configure_socket() { + // disable naggle algorithm + int flag = 1; + if (setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, + sizeof(int)) < 0) { + LOG_ERROR("Could not be disabled Nagle's algorithm"); + ::close(socket_fd); + return false; + } + // habilitate keepalives int optval = 1; - if (setsockopt(socket_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) { - std::cout << "ERROR configuring KEEPALIVES\n"; + if (setsockopt(socket_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, + sizeof(optval)) < 0) { + LOG_ERROR("Unable to configure KEEPALIVES"); ::close(socket_fd); return false; } - // Configure TCP_KEEPIDLE it sets what time to wait to start sending keepalives - // Using the minimum linux keepalives time - uint32_t tcp_keepidle_time = keepalive_config.inactivity_time_until_keepalive; - if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepidle_time, sizeof(tcp_keepidle_time)) < 0) { - std::cout << "Error configuring TCP_KEEPIDLE\n"; - ::close(socket_fd); + // Configure TCP_KEEPIDLE it sets what time to wait to start sending + // keepalives + // Using the minimum linux keepalives time + uint32_t tcp_keepidle_time = + keepalive_config.inactivity_time_until_keepalive; + if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepidle_time, + sizeof(tcp_keepidle_time)) < 0) { + LOG_ERROR("Unable to configure TCP_KEEPIDLE"); + ::close(socket_fd); return false; } - //interval between keepalives + // interval between keepalives uint32_t keep_interval_time = keepalive_config.space_between_tries; - if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPINTVL, &keep_interval_time, sizeof(keep_interval_time)) < 0) { - std::cout << "Error configuring TCP_KEEPINTVL\n"; + if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPINTVL, &keep_interval_time, + sizeof(keep_interval_time)) < 0) { + LOG_ERROR("Unable to configure TCP_KEEPINTVL"); ::close(socket_fd); return false; } - // Configure TCP_KEEPCNT (number keepalives are send before considering the connection down) - uint32_t keep_cnt = keepalive_config.tries_until_disconnection; - if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_cnt, sizeof(keep_cnt)) < 0) { - std::cout << "Error to configure TCP_KEEPCNT\n"; + // Configure TCP_KEEPCNT (number keepalives are send before considering the + // connection down) + uint32_t keep_cnt = keepalive_config.tries_until_disconnection; + if (setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_cnt, + sizeof(keep_cnt)) < 0) { + LOG_ERROR("Unable to configure TCP_KEEPCNT"); ::close(socket_fd); return false; } - return true; + return true; } -void Socket::connection_callback(){ - if(connecting_sockets.contains(remote_node)){ - connecting_sockets.erase(remote_node); - state = CONNECTED; - } - start_receiving(); - is_connecting = false; +void Socket::connection_callback() { + if (connecting_sockets.contains(remote_node)) { + connecting_sockets.erase(remote_node); + state = CONNECTED; + } + start_receiving(); + is_connecting = false; } -void Socket::connect_attempt(){ - //insert the remote address and port and connect - struct sockaddr_in remote_addr; - remote_addr.sin_family = AF_INET; - remote_addr.sin_addr.s_addr = inet_addr(remote_ip); // IP remota en formato adecuado - remote_addr.sin_port = htons(remote_port); - connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr)); +void Socket::connect_attempt() { + // insert the remote address and port and connect + struct sockaddr_in remote_addr; + remote_addr.sin_family = AF_INET; + remote_addr.sin_addr.s_addr = + inet_addr(remote_ip); // IP remota en formato adecuado + remote_addr.sin_port = htons(remote_port); + connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr)); } -void Socket::configure_socket_and_connect(){ - create_socket(); +void Socket::configure_socket_and_connect() { + create_socket(); - if(!configure_socket()){ - cout<<"error configuring socket\n"; - } - connecting_sockets[remote_node] = this; - //create thread that will be block while waiting the connection - connect_attempt() - is_connecting = true; - //thread to wait for connection - wait_for_connection_thread = std::jthread [&](){ - pollfd socket_event; - socket_event.fd = socket_fd; - socket_event.events = POLLIN; - int result = poll(socket_event, 1, -1); // -1 means to never timeout - if(result > 0){ //Connection succesfully - connection_callback(); - }else{ - ::close(socket_fd); - state = INACTIVE; - } - - } + if (!configure_socket()) { + LOG_ERROR("Unable to configure socket"); + } + connecting_sockets[remote_node] = this; + // create thread that will be block while waiting the connection + connect_attempt() is_connecting = true; + // thread to wait for connection + wait_for_connection_thread = std::jthread[&]() { + pollfd socket_event; + socket_event.fd = socket_fd; + socket_event.events = POLLIN; + int result = poll(socket_event, 1, -1); // -1 means to never timeout + if (result > 0) { // Connection succesfully + connection_callback(); + } else { + ::close(socket_fd); + state = INACTIVE; + } + } } -Socket::Socket(IPV4 local_ip, uint32_t local_port, IPV4 remote_ip, uint32_t remote_port, uint32_t inactivity_time_until_keepalive, uint32_t space_between_tries, uint32_t tries_until_disconnection): Socket(local_ip, local_port, remote_ip, remote_port){ - keepalive_config.inactivity_time_until_keepalive = inactivity_time_until_keepalive; - keepalive_config.space_between_tries = space_between_tries; - keepalive_config.tries_until_disconnection = tries_until_disconnection; +Socket::Socket(IPV4 local_ip, uint32_t local_port, IPV4 remote_ip, + uint32_t remote_port, uint32_t inactivity_time_until_keepalive, + uint32_t space_between_tries, uint32_t tries_until_disconnection) + : Socket(local_ip, local_port, remote_ip, remote_port) { + keepalive_config.inactivity_time_until_keepalive = + inactivity_time_until_keepalive; + keepalive_config.space_between_tries = space_between_tries; + keepalive_config.tries_until_disconnection = tries_until_disconnection; } -Socket::Socket(EthernetNode local_node, EthernetNode remote_node):Socket(local_node.ip, local_node.port, remote_node.ip, remote_node.port){} - -void Socket::close(){ - if(is_connecting){ - is_connecting = false; - ~wait_for_connection_thread(); - } - if(is_receiving){ - is_receiving = false; - ~receiving_thread(); - } - while(!tx_packet_buffer.empty()){ - tx_packet_buffer.pop(); - } - while(!rx_packet_buffer.empty()){ - rx_packet_buffer.pop(); - } - state = CLOSING; - ::close(socket_fd); - +Socket::Socket(EthernetNode local_node, EthernetNode remote_node) + : Socket(local_node.ip, local_node.port, remote_node.ip, remote_node.port) { } -void Socket::reconnect(){ - EthernetNode remote_node(remote_ip, remote_port); - if(!connecting_sockets.contains(remote_node)){ - connecting_sockets[remote_node] = this; - } - if(is_connecting){ - is_connecting = false; - ~wait_for_connection_thread(); - } - connect_attempt() - is_connecting = true; - //thread to wait for connection - wait_for_connection_thread = std::jthread [&](){ - pollfd socket_event; - socket_event.fd = socket_fd; - socket_event.events = POLLIN; - int result = poll(socket_event, 1, -1); // -1 means to never timeout - if(result > 0){ //Connection succesfully - connection_callback(); - }else{ - ::close(socket_fd); - state = INACTIVE; - } - } +void Socket::close() { + if (is_connecting) { + is_connecting = false; + ~wait_for_connection_thread(); + } + if (is_receiving) { + is_receiving = false; + ~receiving_thread(); + } + while (!tx_packet_buffer.empty()) { + tx_packet_buffer.pop(); + } + while (!rx_packet_buffer.empty()) { + rx_packet_buffer.pop(); + } + state = CLOSING; + ::close(socket_fd); } -void Socket::reset(){ - EthernetNode remote_node(remote_ip, remote_port); - if(!connecting_sockets.contains(remote_node)){ - connecting_sockets[remote_node] = this; - } - state = INACTIVE; - close(); - configure_socket_and_connect(); +void Socket::reconnect() { + EthernetNode remote_node(remote_ip, remote_port); + if (!connecting_sockets.contains(remote_node)) { + connecting_sockets[remote_node] = this; + } + if (is_connecting) { + is_connecting = false; + ~wait_for_connection_thread(); + } + connect_attempt() is_connecting = true; + // thread to wait for connection + wait_for_connection_thread = std::jthread[&]() { + pollfd socket_event; + socket_event.fd = socket_fd; + socket_event.events = POLLIN; + int result = poll(socket_event, 1, -1); // -1 means to never timeout + if (result > 0) { // Connection succesfully + connection_callback(); + } else { + ::close(socket_fd); + state = INACTIVE; + } + } } +void Socket::reset() { + EthernetNode remote_node(remote_ip, remote_port); + if (!connecting_sockets.contains(remote_node)) { + connecting_sockets[remote_node] = this; + } + state = INACTIVE; + close(); + configure_socket_and_connect(); +} -void Socket::send(){ - std::lock_guard lock(mutex); - while (!tx_packet_buffer.empty()) { - Packet *packet = tx_packet_buffer.front(); - ssize_t sent_bytes = ::send(socket_fd, packet->build(), packet->get_size(), 0); +void Socket::send() { + std::lock_guard lock(mutex); + while (!tx_packet_buffer.empty()) { + Packet* packet = tx_packet_buffer.front(); + ssize_t sent_bytes = + ::send(socket_fd, packet->build(), packet->get_size(), 0); if (sent_bytes < 0) { - std::cerr << "Error sending packet\n"; + LOG_ERROR( + std::format("Unable to send packet {}", packet->get_id())); return; } tx_packet_buffer.pop(); } } -void Socket::start_receiving(){ - is_receiving = true; - receiving_thread = std::jthread(&Socket::receive, this); +void Socket::start_receiving() { + is_receiving = true; + receiving_thread = std::jthread(&Socket::receive, this); } void Socket::receive() { while (is_receiving) { - uint8_t buffer[BUFFER_SIZE]; // Buffer for the data + uint8_t buffer[BUFFER_SIZE]; // Buffer for the data ssize_t received_bytes = ::recv(socket_fd, buffer, sizeof(buffer), 0); if (received_bytes > 0) { - HeapPacket *packet; - packet->parse(*buffer); + HeapPacket* packet; + packet->parse(*buffer); { - std::lock_guard lock(mtx); + std::lock_guard lock(mtx); rx_packet_buffer.push(std::move(packet)); - process_data(); + process_data(); } } else if (received_bytes < 0) { - std::cout << "Error receiving data\n"; - state = CLOSING; - while(!tx_packet_buffer.empty()){ - tx_packet_buffer.pop(); - } - while(!rx_packet_buffer.empty()){ - rx_packet_buffer.pop(); - } - ::close(socket_fd); - return; + LOG_ERROR("Unable to receive data"); + state = CLOSING; + while (!tx_packet_buffer.empty()) { + tx_packet_buffer.pop(); + } + while (!rx_packet_buffer.empty()) { + rx_packet_buffer.pop(); + } + ::close(socket_fd); + return; } } } -void Socket::process_data(){ - while(!rx_packet_buffer.empty()){ - HeapPacket *packet; - { - std::lock_guard lock(mtx); - packet = rx_packet_buffer.front(); - rx_packet_buffer.pop(); - } - uint8_t* new_data = (uint8_t*)(packet->build()); - Order::process_data(this, new_data); - } +void Socket::process_data() { + while (!rx_packet_buffer.empty()) { + HeapPacket* packet; + { + std::lock_guard lock(mtx); + packet = rx_packet_buffer.front(); + rx_packet_buffer.pop(); + } + uint8_t* new_data = (uint8_t*)(packet->build()); + Order::process_data(this, new_data); + } } -bool Socket::add_order_to_queue(Order& order){ - if(state == Socket::SocketState::CONNECTED){ - return false; - } +bool Socket::add_order_to_queue(Order& order) { + if (state == Socket::SocketState::CONNECTED) { + return false; + } if (order.get_size() == 0) { - std::cout << "Error: order empty\n"; - return false; + LOG_ERROR("Order is empty"); + return false; } { - std::lock_guard lock(mutex); - tx_packet_buffer.push(move(order)); + std::lock_guard lock(mutex); + tx_packet_buffer.push(move(order)); } - return true; -} - -bool Socket::is_connected(){ - return state == Socket::SocketState::CONNECTED; + return true; } -#endif //STLIB_ETH +bool Socket::is_connected() { return state == Socket::SocketState::CONNECTED; } From e280e7c5e38b324e44b346ee897055da9407861c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Mon, 9 Dec 2024 11:16:55 +0000 Subject: [PATCH 5/8] Linter and logs on DatagramSocket mock --- .../Communication/Ethernet/TCP/Socket.cpp | 3 + .../Ethernet/UDP/DatagramSocket.cpp | 153 +++++++++--------- 2 files changed, 84 insertions(+), 72 deletions(-) diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp index 60d7281eb..c90b6fa00 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp @@ -1,5 +1,8 @@ #include "HALALMock/Services/Communication/Ethernet/TCP/Socket.hpp" + +#include "HALALMock/Services/Logger/Logger.hpp" + #define BUFFER_SIZE 1024 unordered_map Socket::connecting_sockets = {}; diff --git a/Src/HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.cpp b/Src/HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.cpp index a46e35212..945f1beb2 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.cpp @@ -1,87 +1,96 @@ -#ifdef STLIB_ETH - #include "HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.hpp" + +#include "HALALMock/Services/Logger/Logger.hpp" #define MAX_SIZE_PACKET 1024 DatagramSocket::DatagramSocket() = default; -DatagramSocket::DatagramSocket(DatagramSocket&& other):udp_socket(move(other.udp_socket)), local_ip(move(other.local_ip)) , local_port(move(other.local_port)) ,remote_ip(move(other.remote_ip)), - remote_port(move(remote_port)) - {} +DatagramSocket::DatagramSocket(DatagramSocket&& other) + : udp_socket(move(other.udp_socket)), + local_ip(move(other.local_ip)), + local_port(move(other.local_port)), + remote_ip(move(other.remote_ip)), + remote_port(move(remote_port)) {} -DatagramSocket::DatagramSocket(IPV4 local_ip, uint32_t local_port, IPV4 remote_ip, uint32_t remote_port): local_ip(local_ip), -local_port(local_port), remote_ip(remote_ip), remote_port(remote_port){ - if(not Ethernet::is_running) { - std::cout<<"Cannot declare UDP socket before Ethernet::start()\n"; - return; - } - create_udp_socket(); - } +DatagramSocket::DatagramSocket(IPV4 local_ip, uint32_t local_port, + IPV4 remote_ip, uint32_t remote_port) + : local_ip(local_ip), + local_port(local_port), + remote_ip(remote_ip), + remote_port(remote_port) { + if (not Ethernet::is_running) { + LOG_ERROR("Could not declare UDP socket before Ethernet::start()"); + return; + } + create_udp_socket(); +} -DatagramSocket::DatagramSocket(EthernetNode local_node, EthernetNode remote_node): DatagramSocket(local_node.ip, local_node.port, remote_node.ip, remote_node.port){} +DatagramSocket::DatagramSocket(EthernetNode local_node, + EthernetNode remote_node) + : DatagramSocket(local_node.ip, local_node.port, remote_node.ip, + remote_node.port) {} -DatagramSocket::~DatagramSocket(){ - if(not is_disconnected) - close(); +DatagramSocket::~DatagramSocket() { + if (not is_disconnected) close(); } -void DatagramSocket::create_udp_socket(){ - udp_socket = socket(AF_INET,SOCK_DGRAM,0); - if(udp_socket < 0){ - std::cout<<"Socket creation failed\n"; - } - struct sockaddr_in servaddr; - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(local_port); - servaddr.sin_addr.s_addr = local_ip.address; - if(bind(udp_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ - std::cout<<"Bind error\n"; - close(udp_socket); - is_disconnected = true; - return; - } - is_disconnected = false; - //receiving callback - receiving_udp_thread = std::jthread([&](){ - is_receiving = true; - while(true){ - uint8_t received_data[1024]; - struct sockaddr_in src_addr; - socklen_t addr_len = sizeof(src_addr); - ssize_t size = recvfrom(udp_socket,(uint8_t*)received_data,MAX_SIZE_PACKET,0,(struct sockaddr *)&src_addr, &addr_len); - if(size < 0){ - std::cout<<"Error in function recvfrom\n"; - is_receiving = false; - return; - } - //receive callback - Packet::parse_data(received_data); - } - - }); - Ethernet::update(); +void DatagramSocket::create_udp_socket() { + udp_socket = socket(AF_INET, SOCK_DGRAM, 0); + if (udp_socket < 0) { + LOG_ERROR("Unable to create socket"); + } + struct sockaddr_in servaddr; + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(local_port); + servaddr.sin_addr.s_addr = local_ip.address; + if (bind(udp_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { + LOG_ERROR(std::format("Unable to bind to address {} in port {}", + local_ip->string_address, local_port)); + close(udp_socket); + is_disconnected = true; + return; + } + is_disconnected = false; + // receiving callback + receiving_udp_thread = std::jthread([&]() { + is_receiving = true; + while (true) { + uint8_t received_data[1024]; + struct sockaddr_in src_addr; + socklen_t addr_len = sizeof(src_addr); + ssize_t size = + recvfrom(udp_socket, (uint8_t*)received_data, MAX_SIZE_PACKET, + 0, (struct sockaddr*)&src_addr, &addr_len); + if (size < 0) { + LOG_ERROR("Unable to receive data"); + is_receiving = false; + return; + } + // receive callback + Packet::parse_data(received_data); + } + }); + Ethernet::update(); } -void DatagramSocket::operator=(DatagramSocket&& other){ - udp_socket = move(other.udp_socket); - local_ip = move(other.local_ip); - local_port = move(other.local_port); - remote_ip = other.remote_ip; - remote_port = other.remote_port; - other.is_disconnected = true; +void DatagramSocket::operator=(DatagramSocket&& other) { + udp_socket = move(other.udp_socket); + local_ip = move(other.local_ip); + local_port = move(other.local_port); + remote_ip = other.remote_ip; + remote_port = other.remote_port; + other.is_disconnected = true; } -void DatagramSocket::reconnect(){ - is_disconnected = true; - close(); - create_udp_socket(); +void DatagramSocket::reconnect() { + is_disconnected = true; + close(); + create_udp_socket(); } -void DatagramSocket::close(){ - //check if receiving thread is on and delete it - if(is_receiving){ - receiving_udp_thread.~jthread(); - } - ::close(udp_socket); - is_disconnected = true; +void DatagramSocket::close() { + // check if receiving thread is on and delete it + if (is_receiving) { + receiving_udp_thread.~jthread(); + } + ::close(udp_socket); + is_disconnected = true; } -#endif //STLIB_ETH - From e950ab08bd07dc4cb4a1e8ccf80c9bcc9c4683f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20S=C3=A1ez?= Date: Mon, 9 Dec 2024 11:27:14 +0000 Subject: [PATCH 6/8] Added logs and applied linter on Shared Memory --- .../Services/SharedMemory/SharedMemory.cpp | 239 +++++++++--------- 1 file changed, 125 insertions(+), 114 deletions(-) diff --git a/Src/HALALMock/Services/SharedMemory/SharedMemory.cpp b/Src/HALALMock/Services/SharedMemory/SharedMemory.cpp index fd08d9e26..b42bcae0b 100644 --- a/Src/HALALMock/Services/SharedMemory/SharedMemory.cpp +++ b/Src/HALALMock/Services/SharedMemory/SharedMemory.cpp @@ -1,14 +1,17 @@ #include "HALALMock/Services/SharedMemory/SharedMemory.hpp" -//includes to create the shared Memory in Posix + +#include "HALALMock/Services/Logger/Logger.hpp" +// includes to create the shared Memory in Posix #include +#include #include #include -#include #include + #include // initialize the static variables -EmulatedPin *SharedMemory::gpio_memory{}; +EmulatedPin* SharedMemory::gpio_memory{}; uint8_t* SharedMemory::state_machine_memory = {}; char* SharedMemory::gpio_memory_name = {}; char* SharedMemory::state_machine_memory_name = {}; @@ -16,136 +19,144 @@ uint8_t* SharedMemory::state_machine_count{}; int SharedMemory::shm_gpio_fd{}; int SharedMemory::shm_state_machine_fd{}; - void SharedMemory::start() { - SharedMemory::gpio_memory_name = const_cast(SHM::gpio_memory_name); - SharedMemory::state_machine_memory_name = const_cast(SHM::state_machine_memory_name); - start_state_machine_memory(); // initialize the state machine shared memory - start_gpio_shared_memory(); // initialize the gpio_shared_memory + SharedMemory::gpio_memory_name = const_cast(SHM::gpio_memory_name); + SharedMemory::state_machine_memory_name = + const_cast(SHM::state_machine_memory_name); + start_state_machine_memory(); // initialize the state machine shared memory + start_gpio_shared_memory(); // initialize the gpio_shared_memory } -void SharedMemory::start(const char* gpio_memory_name, const char* state_machine_memory_name) { - SharedMemory::gpio_memory_name=const_cast(gpio_memory_name); - SharedMemory::state_machine_memory_name=const_cast(state_machine_memory_name); - start_state_machine_memory(); // initialize the state machine shared memory - start_gpio_shared_memory(); // initialize the gpio_shared_memory +void SharedMemory::start(const char* gpio_memory_name, + const char* state_machine_memory_name) { + SharedMemory::gpio_memory_name = const_cast(gpio_memory_name); + SharedMemory::state_machine_memory_name = + const_cast(state_machine_memory_name); + start_state_machine_memory(); // initialize the state machine shared memory + start_gpio_shared_memory(); // initialize the gpio_shared_memory } -void SharedMemory::start_gpio_shared_memory(){ - - //create shared memory object - shm_gpio_fd = shm_open(gpio_memory_name, O_CREAT | O_RDWR,0660); - if(shm_gpio_fd == -1){ - std::cout<<"Error to Open de Shared Memory"; - return; - } - //configure the size of the shared memory object - if(ftruncate(shm_gpio_fd,gpio_memory_size) == -1){ - std::cout<<"Error to asssign memory to the Shared Memory"; - ::close(shm_gpio_fd); - return; - } - //point gpio_memory to the beginning of shared_memory - gpio_memory = static_cast(mmap(0, gpio_memory_size, PROT_WRITE | PROT_READ, MAP_SHARED, shm_gpio_fd, 0)); - if(gpio_memory == MAP_FAILED){ - std::cout<<"Error mapping Shared Memory"; - ::close(shm_gpio_fd); // Close the descriptor if there is a problem with the mapping +void SharedMemory::start_gpio_shared_memory() { + // create shared memory object + shm_gpio_fd = shm_open(gpio_memory_name, O_CREAT | O_RDWR, 0660); + if (shm_gpio_fd == -1) { + LOG_ERROR("Unable to open Shared Memory"); + return; + } + // configure the size of the shared memory object + if (ftruncate(shm_gpio_fd, gpio_memory_size) == -1) { + LOG_ERROR("Unable to assign memory to Shared Memory"); + ::close(shm_gpio_fd); + return; + } + // point gpio_memory to the beginning of shared_memory + gpio_memory = static_cast(mmap(0, gpio_memory_size, + PROT_WRITE | PROT_READ, + MAP_SHARED, shm_gpio_fd, 0)); + if (gpio_memory == MAP_FAILED) { + LOG_ERROR("Unable to map Shared Memory"); + ::close(shm_gpio_fd); // Close the descriptor if there is a problem + // with the mapping return; - } - - // clean the shared memory in case it has info from the previous execution - memset(static_cast(gpio_memory),0,gpio_memory_size); + } + + // clean the shared memory in case it has info from the previous execution + memset(static_cast(gpio_memory), 0, gpio_memory_size); } -void SharedMemory::start_state_machine_memory(){ - - // create the shared memory object - shm_state_machine_fd=shm_open(state_machine_memory_name,O_CREAT | O_RDWR, 0660); - if(shm_state_machine_fd==-1){ - std::cout<<"Error creating the shared memory object\n"; - std::terminate(); - } - // configure the size of the shared memory object - if(ftruncate(shm_state_machine_fd,state_machine_memory_size)==-1){ - std::cout<<"Error configuring the size of the shared memory object\n"; - ::close(shm_state_machine_fd); - std::terminate(); - } - // memory map the shared memory object - state_machine_memory=static_cast(mmap(NULL,state_machine_memory_size,PROT_WRITE | PROT_READ,MAP_SHARED,shm_state_machine_fd,0)); - if(state_machine_memory==MAP_FAILED){ - std::cout<<"Error mapping the shared memory object\n"; - ::close(shm_state_machine_fd); - std::terminate(); - } - - state_machine_count=&state_machine_memory[0]; - *state_machine_count=0; - - // clean the shared memory in case it has info from the previous execution - memset(static_cast(state_machine_memory),0,state_machine_memory_size); +void SharedMemory::start_state_machine_memory() { + // create the shared memory object + shm_state_machine_fd = + shm_open(state_machine_memory_name, O_CREAT | O_RDWR, 0660); + if (shm_state_machine_fd == -1) { + LOG_ERROR("Unable to create the Shared Memory object"); + std::terminate(); + } + // configure the size of the shared memory object + if (ftruncate(shm_state_machine_fd, state_machine_memory_size) == -1) { + LOG_ERROR("Unable to configure the size of the Shared Memory object"); + ::close(shm_state_machine_fd); + std::terminate(); + } + // memory map the shared memory object + state_machine_memory = static_cast( + mmap(NULL, state_machine_memory_size, PROT_WRITE | PROT_READ, + MAP_SHARED, shm_state_machine_fd, 0)); + if (state_machine_memory == MAP_FAILED) { + LOG_ERROR("Unable to map the Shared Memory object"); + ::close(shm_state_machine_fd); + std::terminate(); + } + + state_machine_count = &state_machine_memory[0]; + *state_machine_count = 0; + + // clean the shared memory in case it has info from the previous execution + memset(static_cast(state_machine_memory), 0, + state_machine_memory_size); } -void SharedMemory::close(){ - close_gpio_shared_memory(); - close_state_machine_memory(); +void SharedMemory::close() { + close_gpio_shared_memory(); + close_state_machine_memory(); } -void SharedMemory::close_gpio_shared_memory(){ - if (gpio_memory != nullptr){ - //unmap shared memory - if(munmap(gpio_memory,gpio_memory_size) == -1){ - std::cout<<"Error unmapping the gpio shared_memory\n"; - std::terminate(); - } - //put the pointer to null - gpio_memory = nullptr; - } - int delete_shared_gpio_memory = shm_unlink(gpio_memory_name); - if(delete_shared_gpio_memory == -1){ - std::cout<<"Error unlinking the shared memory object\n"; - std::terminate(); - } - if(shm_gpio_fd!=-1 && ::close(shm_gpio_fd) == -1){ - std::cout<<"Error closing the file descriptor\n"; - std::terminate(); - } +void SharedMemory::close_gpio_shared_memory() { + if (gpio_memory != nullptr) { + // unmap shared memory + if (munmap(gpio_memory, gpio_memory_size) == -1) { + std::cout << "Error unmapping the gpio shared_memory\n"; + LOG_ERROR("Unable to unmap the gpio shared_memory"); + std::terminate(); + } + // put the pointer to null + gpio_memory = nullptr; + } + int delete_shared_gpio_memory = shm_unlink(gpio_memory_name); + if (delete_shared_gpio_memory == -1) { + LOG_ERROR("Unable to unlink the Shared Memory object"); + std::terminate(); + } + if (shm_gpio_fd != -1 && ::close(shm_gpio_fd) == -1) { + LOG_ERROR("Unable to close the file descriptor"); + std::terminate(); + } } -void SharedMemory::close_state_machine_memory(){ - if (state_machine_memory!=nullptr){ - // unmap the shared memory object - if(munmap(state_machine_memory,state_machine_memory_size)==-1){ - std::cout<<"Error unmapping the shared memory object\n"; - std::terminate(); - } - - // point the shared memory object to NULL - state_machine_memory=nullptr; - } - - if(shm_unlink(state_machine_memory_name)==-1){ - std::cout<<"Error unlinking the shared memory object\n"; - std::terminate(); - } - - if(shm_state_machine_fd !=-1 && ::close(shm_state_machine_fd)==-1){ - std::cout<<"Error closing the shared memory file descriptor\n"; - - std::terminate(); - } +void SharedMemory::close_state_machine_memory() { + if (state_machine_memory != nullptr) { + // unmap the shared memory object + if (munmap(state_machine_memory, state_machine_memory_size) == -1) { + LOG_ERROR("Unable to unmap the Shared Memory object"); + std::terminate(); + } + + // point the shared memory object to NULL + state_machine_memory = nullptr; + } + + if (shm_unlink(state_machine_memory_name) == -1) { + LOG_ERROR("Unable to unlink the Shared Memory object"); + std::terminate(); + } + + if (shm_state_machine_fd != -1 && ::close(shm_state_machine_fd) == -1) { + LOG_ERROR("Unable to close the Shared Memory file descriptor"); + + std::terminate(); + } } -void SharedMemory::update_current_state(uint8_t index, uint8_t state){ - state_machine_memory[index]=state; +void SharedMemory::update_current_state(uint8_t index, uint8_t state) { + state_machine_memory[index] = state; } -EmulatedPin& SharedMemory::get_pin(Pin pin){ +EmulatedPin& SharedMemory::get_pin(Pin pin) { auto it = SHM::pin_offsets.find(pin); - if(it == SHM::pin_offsets.end()){ - std::cout<<"Error: Pin " << pin.to_string() << " doesn't exist.\n"; + if (it == SHM::pin_offsets.end()) { + LOG_ERROR(std::format("Pin {} does not exist", pin.to_string())); std::terminate(); } size_t offset = it->second; - if (offset >= gpio_memory_size){ - std::cout<<"Error: Offset " << offset << " is out of scope\n"; + if (offset >= gpio_memory_size) { + LOG_ERROR(std::format("Offset {} is out of scope", offset)); std::terminate(); } From 8279f9847628d9987ffafb09494e2babf0ee575d Mon Sep 17 00:00:00 2001 From: Gonzalo <58850783+g0nz4I0@users.noreply.github.com> Date: Sun, 19 Jan 2025 01:38:58 +0100 Subject: [PATCH 7/8] log when socked is closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Óscar Gandía Iglesias <145703854+oganigl@users.noreply.github.com> --- Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp index 6b9ff4e1d..e55eef9f1 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp @@ -187,6 +187,7 @@ void Socket::close() { tx_packet_buffer.pop(); } state = CLOSING; + LOG_INFO("Socket has been closed correctly") } void Socket::reconnect() { // I'm going to do in reconnect a total reset due to From ebe8a96f0b635d56c36629fdb933ef96645b2b38 Mon Sep 17 00:00:00 2001 From: Gonzalo <58850783+g0nz4I0@users.noreply.github.com> Date: Sun, 19 Jan 2025 01:41:17 +0100 Subject: [PATCH 8/8] Fix multiple definition on connecting_sockets --- Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp index e55eef9f1..8042d84d0 100644 --- a/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp +++ b/Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp @@ -5,7 +5,6 @@ std::unordered_map Socket::connecting_sockets = {}; #include "HALALMock/Services/Logger/Logger.hpp" #define BUFFER_SIZE 1024 -unordered_map Socket::connecting_sockets = {}; Socket::Socket() = default;