mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-21 03:10:02 +02:00
more tests
This commit is contained in:
parent
d5df63ce49
commit
f08006db46
@ -45,7 +45,8 @@ class UdpRxSocket {
|
|||||||
|
|
||||||
const std::string portname = std::to_string(port);
|
const std::string portname = std::to_string(port);
|
||||||
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)) {
|
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)) {
|
||||||
throw RuntimeError("Failed at getaddrinfo with " + std::string(hostname));
|
throw RuntimeError("Failed at getaddrinfo with " +
|
||||||
|
std::string(hostname));
|
||||||
}
|
}
|
||||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
@ -83,23 +84,23 @@ class UdpRxSocket {
|
|||||||
: UdpRxSocket(port_number, ps, InterfaceNameToIp(eth).str().c_str(),
|
: UdpRxSocket(port_number, ps, InterfaceNameToIp(eth).str().c_str(),
|
||||||
buf_size) {}
|
buf_size) {}
|
||||||
|
|
||||||
const char *LastPacket() const { return buff; }
|
|
||||||
|
|
||||||
~UdpRxSocket() {
|
~UdpRxSocket() {
|
||||||
delete[] buff;
|
delete[] buff;
|
||||||
Shutdown();
|
Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive one packet to the internal buffer of the socket class, preferred
|
const char *LastPacket() const noexcept { return buff; }
|
||||||
// method?
|
constexpr ssize_t getPacketSize() const noexcept { return packet_size; }
|
||||||
bool ReceivePacket() {
|
|
||||||
auto bytes_received = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
|
bool ReceivePacket() noexcept {
|
||||||
|
auto bytes_received =
|
||||||
|
recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
|
||||||
return bytes_received == packet_size;
|
return bytes_received == packet_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive to an external buffer, do we need it?
|
bool ReceivePacket(char *dst) noexcept {
|
||||||
bool ReceivePacket(char *dst) {
|
auto bytes_received =
|
||||||
auto bytes_received = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
|
recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
|
||||||
return bytes_received == packet_size;
|
return bytes_received == packet_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +108,8 @@ class UdpRxSocket {
|
|||||||
// refactoring of the receiver
|
// refactoring of the receiver
|
||||||
ssize_t ReceiveDataOnly(char *dst) {
|
ssize_t ReceiveDataOnly(char *dst) {
|
||||||
auto r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
auto r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
||||||
constexpr ssize_t eiger_header_packet = 40; //only detector that has this
|
constexpr ssize_t eiger_header_packet =
|
||||||
|
40; // only detector that has this
|
||||||
if (r == eiger_header_packet) {
|
if (r == eiger_header_packet) {
|
||||||
FILE_LOG(logWARNING) << "Got header pkg";
|
FILE_LOG(logWARNING) << "Got header pkg";
|
||||||
r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
||||||
@ -115,10 +117,6 @@ class UdpRxSocket {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t getPacketSize() const{
|
|
||||||
return packet_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t getBufferSize() const {
|
ssize_t getBufferSize() const {
|
||||||
uint64_t ret_size = 0;
|
uint64_t ret_size = 0;
|
||||||
socklen_t optlen = sizeof(uint64_t);
|
socklen_t optlen = sizeof(uint64_t);
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
TEST_CASE("Receive a packet on localhost") {
|
int open_socket(int port) {
|
||||||
constexpr int port = 50001;
|
|
||||||
const char *host = nullptr; // localhost
|
const char *host = nullptr; // localhost
|
||||||
|
|
||||||
// Create a socket for sending
|
// Create a socket for sending
|
||||||
@ -28,17 +27,30 @@ TEST_CASE("Receive a packet on localhost") {
|
|||||||
throw sls::RuntimeError("Failed to create UDP RX socket");
|
throw sls::RuntimeError("Failed to create UDP RX socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (connect(fd, res->ai_addr, res->ai_addrlen)){
|
||||||
|
throw sls::RuntimeError("Failed to connect socket");
|
||||||
|
}
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Receive a packet on localhost") {
|
||||||
|
constexpr int port = 50001;
|
||||||
|
|
||||||
std::vector<int> data_to_send{4, 5, 3, 2, 5, 7, 2, 3};
|
std::vector<int> data_to_send{4, 5, 3, 2, 5, 7, 2, 3};
|
||||||
ssize_t packet_size =
|
ssize_t packet_size =
|
||||||
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
|
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
|
||||||
sls::UdpRxSocket udpsock{port, packet_size, host};
|
sls::UdpRxSocket udpsock{port, packet_size};
|
||||||
|
|
||||||
int n = sendto(fd, data_to_send.data(), packet_size, 0, res->ai_addr,
|
|
||||||
res->ai_addrlen);
|
|
||||||
|
|
||||||
|
int fd = open_socket(port);
|
||||||
|
// int n = sendto(fd, data_to_send.data(), packet_size, 0, res->ai_addr,
|
||||||
|
// res->ai_addrlen);
|
||||||
|
|
||||||
|
auto n = write(fd, data_to_send.data(), packet_size);
|
||||||
CHECK(n == packet_size);
|
CHECK(n == packet_size);
|
||||||
CHECK(udpsock.ReceivePacket());
|
CHECK(udpsock.ReceivePacket());
|
||||||
|
close(fd);
|
||||||
// Copy data from buffer and compare values
|
// Copy data from buffer and compare values
|
||||||
std::vector<int> data_received(data_to_send.size());
|
std::vector<int> data_received(data_to_send.size());
|
||||||
memcpy(data_received.data(), udpsock.LastPacket(), udpsock.getPacketSize());
|
memcpy(data_received.data(), udpsock.LastPacket(), udpsock.getPacketSize());
|
||||||
@ -65,3 +77,13 @@ TEST_CASE("Shutdown socket without hanging") {
|
|||||||
|
|
||||||
CHECK(r == false); // since we didn't get the packet
|
CHECK(r == false); // since we didn't get the packet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Too small packet"){
|
||||||
|
constexpr int port = 50001;
|
||||||
|
sls::UdpRxSocket s(port, 2*sizeof(uint32_t));
|
||||||
|
auto fd = open_socket(port);
|
||||||
|
uint32_t val = 10;
|
||||||
|
write(fd, &val, sizeof(val));
|
||||||
|
CHECK(s.ReceivePacket() == false);
|
||||||
|
close(fd);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user