Cleanup settings in UdpReceiver

This commit is contained in:
2020-05-11 17:32:49 +02:00
parent 88364592bf
commit e15b285a3e
3 changed files with 20 additions and 12 deletions
+1 -4
View File
@@ -2,7 +2,6 @@
#define UDPRECEIVER_H
#include <sys/socket.h>
#include "buffer_config.hpp"
class UdpReceiver {
@@ -15,9 +14,7 @@ public:
bool receive(void* buffer, size_t buffer_n_bytes);
int receive_many(mmsghdr* msgs, const size_t n_msgs);
void bind(
const uint16_t port,
const size_t usec_timeout=core_buffer::BUFFER_UDP_US_TIMEOUT);
void bind(const uint16_t port);
void disconnect();
};
+3
View File
@@ -34,6 +34,9 @@ namespace core_buffer {
// Time to sleep before retrying to read the queue.
const size_t BUFFER_QUEUE_RETRY_MS = 5;
// Size of UDP recv buffer.
const int BUFFER_UDP_RCVBUF = MODULE_N_BYTES * 10;
// Microseconds timeout for UDP recv.
const int BUFFER_UDP_US_TIMEOUT = 5 * 1000;
+16 -8
View File
@@ -4,8 +4,11 @@
#include "jungfrau.hpp"
#include "date.h"
#include <unistd.h>
#include <cstring>
#include "buffer_config.hpp"
using namespace std;
using namespace core_buffer;
UdpReceiver::UdpReceiver() :
socket_fd_(-1)
@@ -17,7 +20,7 @@ UdpReceiver::~UdpReceiver()
disconnect();
}
void UdpReceiver::bind(const uint16_t port, const size_t usec_timeout)
void UdpReceiver::bind(const uint16_t port)
{
if (socket_fd_ > -1) {
throw runtime_error("Socket already bound.");
@@ -44,14 +47,19 @@ void UdpReceiver::bind(const uint16_t port, const size_t usec_timeout)
struct timeval udp_socket_timeout;
udp_socket_timeout.tv_sec = 0;
udp_socket_timeout.tv_usec = usec_timeout;
udp_socket_timeout.tv_usec = BUFFER_UDP_US_TIMEOUT;
setsockopt(
socket_fd_,
SOL_SOCKET,
SO_RCVTIMEO,
(const char*)&udp_socket_timeout,
sizeof(struct timeval));
if (setsockopt(socket_fd_, SOL_SOCKET, SO_RCVTIMEO,
(const char*)&udp_socket_timeout, sizeof(struct timeval) == -1)) {
throw runtime_error(
"Cannot set SO_RCVTIMEO. " + string(strerror(errno)));
}
if (setsockopt(socket_fd_, SOL_SOCKET, SO_RCVBUF,
&BUFFER_UDP_RCVBUF, sizeof(BUFFER_UDP_RCVBUF)) == -1) {
throw runtime_error(
"Cannot set SO_RCVBUF. " + string(strerror(errno)));
};
//TODO: try to set SO_RCVLOWAT
}