mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-21 11:20:04 +02:00
test socket
This commit is contained in:
parent
64d59b1dff
commit
d5df63ce49
@ -5,8 +5,7 @@ data on a udp socket.
|
||||
|
||||
It provides a drop in replacement for
|
||||
genericSocket. But please be careful since
|
||||
this might be deprecated in the future.
|
||||
|
||||
this might be deprecated in the future
|
||||
|
||||
*/
|
||||
|
||||
@ -108,15 +107,18 @@ class UdpRxSocket {
|
||||
// refactoring of the receiver
|
||||
ssize_t ReceiveDataOnly(char *dst) {
|
||||
auto r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
||||
// if we read an eiger header pkg read again, with new firmware
|
||||
// this check can be removed
|
||||
if (r == 40) {
|
||||
constexpr ssize_t eiger_header_packet = 40; //only detector that has this
|
||||
if (r == eiger_header_packet) {
|
||||
FILE_LOG(logWARNING) << "Got header pkg";
|
||||
r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t getPacketSize() const{
|
||||
return packet_size;
|
||||
}
|
||||
|
||||
ssize_t getBufferSize() const {
|
||||
uint64_t ret_size = 0;
|
||||
socklen_t optlen = sizeof(uint64_t);
|
||||
|
@ -121,7 +121,7 @@ std::string IpToInterfaceName(const std::string &ip) {
|
||||
|
||||
IpAddr InterfaceNameToIp(const std::string &ifn) {
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int family, s;
|
||||
// int family, s;
|
||||
char host[NI_MAXHOST];
|
||||
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
@ -132,7 +132,7 @@ IpAddr InterfaceNameToIp(const std::string &ifn) {
|
||||
if (ifa->ifa_addr == NULL)
|
||||
continue;
|
||||
|
||||
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
|
||||
auto s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
|
||||
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
||||
|
||||
if ((strcmp(ifa->ifa_name, ifn.c_str()) == 0) &&
|
||||
|
@ -8,4 +8,5 @@ target_sources(tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-FixedCapacityContainer.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-ToString.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-TypeTraits.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-UdpRxSocket.cpp
|
||||
)
|
67
slsSupportLib/tests/test-UdpRxSocket.cpp
Normal file
67
slsSupportLib/tests/test-UdpRxSocket.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "UdpRxSocket.h"
|
||||
#include "catch.hpp"
|
||||
#include "sls_detector_exceptions.h"
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE("Receive a packet on localhost") {
|
||||
constexpr int port = 50001;
|
||||
const char *host = nullptr; // localhost
|
||||
|
||||
// Create a socket for sending
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_protocol = 0;
|
||||
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
||||
struct addrinfo *res = 0;
|
||||
|
||||
const std::string portname = std::to_string(port);
|
||||
if (getaddrinfo(host, portname.c_str(), &hints, &res)) {
|
||||
throw sls::RuntimeError("Failed at getaddrinfo with " +
|
||||
std::string(host));
|
||||
}
|
||||
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if (fd == -1) {
|
||||
throw sls::RuntimeError("Failed to create UDP RX socket");
|
||||
}
|
||||
|
||||
std::vector<int> data_to_send{4, 5, 3, 2, 5, 7, 2, 3};
|
||||
ssize_t packet_size =
|
||||
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
|
||||
sls::UdpRxSocket udpsock{port, packet_size, host};
|
||||
|
||||
int n = sendto(fd, data_to_send.data(), packet_size, 0, res->ai_addr,
|
||||
res->ai_addrlen);
|
||||
|
||||
CHECK(n == packet_size);
|
||||
CHECK(udpsock.ReceivePacket());
|
||||
|
||||
// Copy data from buffer and compare values
|
||||
std::vector<int> data_received(data_to_send.size());
|
||||
memcpy(data_received.data(), udpsock.LastPacket(), udpsock.getPacketSize());
|
||||
CHECK(data_received.size() == data_to_send.size()); // sanity check
|
||||
for (size_t i = 0; i != data_to_send.size(); ++i) {
|
||||
CHECK(data_to_send[i] == data_received[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Shutdown socket without hanging") {
|
||||
constexpr int port = 50001;
|
||||
constexpr ssize_t packet_size = 8000;
|
||||
sls::UdpRxSocket s{port, packet_size};
|
||||
|
||||
// Start a thread and wait for package
|
||||
// if the socket is left open we would block
|
||||
std::future<bool> ret =
|
||||
std::async(static_cast<bool (sls::UdpRxSocket::*)()>(
|
||||
&sls::UdpRxSocket::ReceivePacket),
|
||||
&s);
|
||||
|
||||
s.Shutdown();
|
||||
auto r = ret.get();
|
||||
|
||||
CHECK(r == false); // since we didn't get the packet
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user