From 994d4260a831a3b976418b19073a2936d0119525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eckl=2C=20M=C3=A1t=C3=A9?= <ecklm94@gmail.com> Date: Thu, 26 May 2016 22:43:30 +0200 Subject: [PATCH] Timeout dolog van MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kicsit rugalmasabbá kell tenni, de az alap koncepció működik. --- include/udp_packet_wp.h | 2 +- main.cpp | 2 -- src/IPC_US_communicator.cpp | 4 ++++ src/udp_packet_wp.cpp | 11 +++++++++-- src/udp_socket_wp.cpp | 23 ++++++++++++++++++++--- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/udp_packet_wp.h b/include/udp_packet_wp.h index fd217f2..acd7520 100644 --- a/include/udp_packet_wp.h +++ b/include/udp_packet_wp.h @@ -38,7 +38,7 @@ public: * @param new_buf New content of the buffer * @param new_buf_len Length of the new conten in bytes */ - void set_buf(const char *new_buf, const size_t new_buf_len); + void set_buf(const char *new_buf, const ssize_t new_buf_len); /** * @return The length of the current content in the buffer diff --git a/main.cpp b/main.cpp index 3b6681a..38de168 100644 --- a/main.cpp +++ b/main.cpp @@ -169,8 +169,6 @@ int main(int argc, char *argv[]) { comm_link.send_data("go to hell!"); stop = true; - log_on_demand(LOG_DEBUG, "Raising SIGPIPE"); - raise(SIGPIPE); } else comm_link.send_data("nothing has happened"); diff --git a/src/IPC_US_communicator.cpp b/src/IPC_US_communicator.cpp index 6a9e653..e4e2fa0 100644 --- a/src/IPC_US_communicator.cpp +++ b/src/IPC_US_communicator.cpp @@ -41,6 +41,10 @@ IPC_US_communicator::~IPC_US_communicator() { std::string IPC_US_communicator::recv_data() { udp_packet_wp data = comm_sock.recvfrom_wp(NULL, NULL); + if(data.get_buf() == 0) + { + // May throw some exception... + } return data.get_buf(); } diff --git a/src/udp_packet_wp.cpp b/src/udp_packet_wp.cpp index 4f134c0..52f877c 100644 --- a/src/udp_packet_wp.cpp +++ b/src/udp_packet_wp.cpp @@ -32,7 +32,14 @@ void udp_packet_wp::set_buf_len(const size_t new_buf_len) { buf_len = new_buf_len; } -void udp_packet_wp::set_buf(const char *new_buf, const size_t new_buf_len) { +void udp_packet_wp::set_buf(const char *new_buf, const ssize_t new_buf_len) { set_buf_len(new_buf_len); - memcpy(buf, new_buf, new_buf_len); + if(new_buf_len <= 0 || new_buf == NULL) + { + memset(buf, 0, MAX_UDP_PACKET_SIZE_BYTES); + } + else + { + memcpy(buf, new_buf, new_buf_len); + } } diff --git a/src/udp_socket_wp.cpp b/src/udp_socket_wp.cpp index 24f78dc..051f57f 100644 --- a/src/udp_socket_wp.cpp +++ b/src/udp_socket_wp.cpp @@ -1,9 +1,14 @@ #include <sys/socket.h> #include <unistd.h> #include <stdio.h> +#include <sys/time.h> #include "../include/udp_socket_wp.h" #include "../include/dns_message.h" +#include "../include/logger.h" + +extern logger log_on_demand; +extern bool verbose; void udp_socket_wp::create_socket(int __domain) { if(domain < 0 || fd < 0) @@ -17,11 +22,16 @@ void udp_socket_wp::create_socket(int __domain) { { // May throw some exception... } + timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + setsockopt_wp(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); } void udp_socket_wp::setsockopt_wp(int __level, int __optname, const void* __optval, socklen_t __optlen) { if (setsockopt(fd, __level, __optname, __optval, __optlen) < 0) { // May throw some exception + PERROR_ON_DEMAND("setsockopt"); } } @@ -38,9 +48,16 @@ void udp_socket_wp::bind_wp(const general_sockaddr_wp &wrapped_sockaddr) { udp_packet_wp udp_socket_wp::recvfrom_wp(sockaddr *sender_address, socklen_t* sender_address_len) { udp_packet_wp msg; static char buf[MAX_UDP_PACKET_SIZE_BYTES]; - size_t len = recvfrom(fd, buf, MAX_UDP_PACKET_SIZE_BYTES, MSG_WAITFORONE, - sender_address, sender_address_len); - + ssize_t len; + if((len = recvfrom(fd, buf, MAX_UDP_PACKET_SIZE_BYTES, MSG_WAITFORONE, + sender_address, sender_address_len)) < 0) + { + if(verbose) + PERROR_ON_DEMAND("recvfrom"); + msg.set_buf(NULL, 0); + } +// else +// log_on_demand(LOG_DEBUG, "len: %d", len); msg.set_buf(buf, len); msg.address.set_sockaddr(sender_address, (sender_address_len != NULL) ? *sender_address_len : 0); return msg; -- GitLab