66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
// Copyright (2019-2022) Paul Scherrer Institute
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "UdpReceiver.h"
|
|
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include "../common/JFJochException.h"
|
|
|
|
UdpReceiver::UdpReceiver(uint16_t udp_port_number, ProcessJFPacket &process) {
|
|
receiver = std::async(std::launch::async, &UdpReceiver::Run, this, udp_port_number, &process);
|
|
}
|
|
|
|
UdpReceiver::~UdpReceiver() {
|
|
cancel = true;
|
|
if (receiver.valid())
|
|
receiver.get();
|
|
}
|
|
|
|
uint64_t UdpReceiver::Finalize() {
|
|
cancel = true;
|
|
if (receiver.valid())
|
|
return receiver.get();
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
uint64_t UdpReceiver::Run(uint16_t udp_port_number, ProcessJFPacket *process) {
|
|
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (fd < 0)
|
|
throw JFJochException(JFJochExceptionCategory::UDPError, "Cannot create UDP socket");
|
|
|
|
timeval timeout{
|
|
.tv_sec = 0,
|
|
.tv_usec = 10
|
|
};
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) <= 0)
|
|
throw JFJochException(JFJochExceptionCategory::UDPError, "Cannot set socket timeout");
|
|
|
|
sockaddr_in server_addr {
|
|
.sin_family = AF_INET,
|
|
.sin_port = htons(udp_port_number),
|
|
.sin_addr = {
|
|
.s_addr = INADDR_ANY
|
|
}
|
|
}, client_addr{};
|
|
|
|
if (bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) <= 0)
|
|
throw JFJochException(JFJochExceptionCategory::UDPError, "Cannot bind to UDP port");
|
|
|
|
char buffer[9000];
|
|
socklen_t src_addr_len=sizeof(client_addr);
|
|
|
|
while (!cancel) {
|
|
auto count = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &client_addr, &src_addr_len);
|
|
|
|
if (count == sizeof(jf_payload))
|
|
process->ProcessPacket((jf_payload *) buffer, client_addr.sin_addr.s_addr);
|
|
else if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (count == -1))
|
|
throw JFJochException(JFJochExceptionCategory::UDPError, "Cannot bind to UDP port");
|
|
}
|
|
|
|
close(fd);
|
|
} |