mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-04-24 07:50:44 +02:00
First UDP socket recv implementation
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#ifndef UDPRECEIVER_H
|
||||
#define UDPRECEIVER_H
|
||||
|
||||
#include "config.hpp"
|
||||
#include "RingBuffer.hpp"
|
||||
|
||||
class UdpReceiver {
|
||||
|
||||
int socket_fd_ = -1;
|
||||
|
||||
public:
|
||||
UdpReceiver();
|
||||
virtual ~UdpReceiver();
|
||||
|
||||
bool receive(void* buffer, size_t buffer_n_bytes);
|
||||
void bind(
|
||||
const uint16_t port,
|
||||
const size_t usec_timeout=config::udp_usec_timeout);
|
||||
void close();
|
||||
};
|
||||
|
||||
|
||||
#endif //LIB_CPP_H5_WRITER_UDPRECEIVER_H
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <iostream>
|
||||
#include "UdpReceiver.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void UdpReceiver::bind(const uint16_t port, const size_t usec_timeout)
|
||||
{
|
||||
if (socket_fd_ > -1) {
|
||||
throw runtime_error("Socket already bound.");
|
||||
}
|
||||
|
||||
socket_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (socket_fd_ < 0) {
|
||||
throw runtime_error("Cannot open socket.");
|
||||
}
|
||||
|
||||
sockaddr_in server_address = {0};
|
||||
server_address.sin_family = AF_INET;
|
||||
server_address.sin_addr.s_addr = INADDR_ANY;
|
||||
server_address.sin_port = htons(port);
|
||||
|
||||
auto bind_result = ::bind(
|
||||
socket_fd_,
|
||||
reinterpret_cast<const sockaddr *>(&server_address),
|
||||
sizeof(server_address));
|
||||
|
||||
if (bind_result < 0) {
|
||||
throw runtime_error("Cannot bind socket.");
|
||||
}
|
||||
|
||||
struct timeval udp_socket_timeout;
|
||||
udp_socket_timeout.tv_sec = 0;
|
||||
udp_socket_timeout.tv_usec = usec_timeout;
|
||||
|
||||
setsockopt(
|
||||
socket_fd_,
|
||||
SOL_SOCKET,
|
||||
SO_RCVTIMEO,
|
||||
(const char*)&udp_socket_timeout,
|
||||
sizeof(struct timeval));
|
||||
}
|
||||
|
||||
bool UdpReceiver::receive(void* buffer, size_t buffer_n_bytes)
|
||||
{
|
||||
auto data_len = recv(socket_fd_, buffer, buffer_n_bytes, 0);
|
||||
|
||||
if (data_len < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data_len != buffer_n_bytes) {
|
||||
cout << "Invalid packet length " << data_len << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user