mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
Dev/fix port size (#805)
* port datatype changing from int to uint16_t * throwing for -1 given for uint16_t ports
This commit is contained in:
@ -316,6 +316,7 @@ template <> defs::vetoAlgorithm StringTo(const std::string &s);
|
||||
template <> defs::gainMode StringTo(const std::string &s);
|
||||
template <> defs::polarity StringTo(const std::string &s);
|
||||
|
||||
template <> uint16_t StringTo(const std::string &s);
|
||||
template <> uint32_t StringTo(const std::string &s);
|
||||
template <> uint64_t StringTo(const std::string &s);
|
||||
template <> int StringTo(const std::string &s);
|
||||
|
@ -7,6 +7,7 @@ UDP socket class to receive data. The intended use is in the
|
||||
receiver listener loop. Should be used RAII style...
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h> //ssize_t
|
||||
namespace sls {
|
||||
|
||||
@ -15,8 +16,8 @@ class UdpRxSocket {
|
||||
int sockfd_{-1};
|
||||
|
||||
public:
|
||||
UdpRxSocket(int port, ssize_t packet_size, const char *hostname = nullptr,
|
||||
int kernel_buffer_size = 0);
|
||||
UdpRxSocket(uint16_t port, ssize_t packet_size,
|
||||
const char *hostname = nullptr, int kernel_buffer_size = 0);
|
||||
~UdpRxSocket();
|
||||
bool ReceivePacket(char *dst) noexcept;
|
||||
int getBufferSize() const;
|
||||
|
@ -103,7 +103,7 @@ class ZmqSocket {
|
||||
* @param hostname_or_ip hostname or ip of server
|
||||
* @param portnumber port number
|
||||
*/
|
||||
ZmqSocket(const char *const hostname_or_ip, const uint32_t portnumber);
|
||||
ZmqSocket(const char *const hostname_or_ip, const uint16_t portnumber);
|
||||
|
||||
/**
|
||||
* Constructor for a server
|
||||
@ -111,7 +111,7 @@ class ZmqSocket {
|
||||
* @param portnumber port number
|
||||
* @param ethip is the ip of the ethernet interface to stream zmq from
|
||||
*/
|
||||
ZmqSocket(const uint32_t portnumber, const char *ethip);
|
||||
ZmqSocket(const uint16_t portnumber, const char *ethip);
|
||||
|
||||
/** Returns high water mark for outbound messages */
|
||||
int GetSendHighWaterMark();
|
||||
@ -143,7 +143,7 @@ class ZmqSocket {
|
||||
* Returns Port Number
|
||||
* @returns Port Number
|
||||
*/
|
||||
uint32_t GetPortNumber() { return portno; }
|
||||
uint16_t GetPortNumber() { return portno; }
|
||||
|
||||
/**
|
||||
* Returns Server Address
|
||||
@ -251,7 +251,7 @@ class ZmqSocket {
|
||||
};
|
||||
|
||||
/** Port Number */
|
||||
uint32_t portno;
|
||||
uint16_t portno;
|
||||
|
||||
/** Socket descriptor */
|
||||
mySocketDescriptors sockfd;
|
||||
|
@ -64,8 +64,8 @@ class MacAddr {
|
||||
|
||||
struct UdpDestination {
|
||||
uint32_t entry{};
|
||||
uint32_t port{};
|
||||
uint32_t port2{};
|
||||
uint16_t port{};
|
||||
uint16_t port2{};
|
||||
IpAddr ip;
|
||||
IpAddr ip2;
|
||||
MacAddr mac;
|
||||
@ -88,5 +88,6 @@ IpAddr HostnameToIp(const char *hostname);
|
||||
std::string IpToInterfaceName(const std::string &ip);
|
||||
MacAddr InterfaceNameToMac(const std::string &inf);
|
||||
IpAddr InterfaceNameToIp(const std::string &ifn);
|
||||
|
||||
void validatePortNumber(uint16_t port);
|
||||
void validatePortRange(uint16_t startPort, int numPorts);
|
||||
} // namespace sls
|
||||
|
@ -30,8 +30,8 @@
|
||||
// C includes
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
//Need macros for C compatibility
|
||||
//NOLINTBEGIN(cppcoreguidelines-macro-usage)
|
||||
// Need macros for C compatibility
|
||||
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
|
||||
#define BIT32_MASK 0xFFFFFFFF
|
||||
#define MAX_RX_DBIT 64
|
||||
|
||||
@ -80,7 +80,7 @@
|
||||
#define DEFAULT_STREAMING_TIMER_IN_MS 500
|
||||
|
||||
#define NUM_RX_THREAD_IDS 9
|
||||
//NOLINTEND(cppcoreguidelines-macro-usage)
|
||||
// NOLINTEND(cppcoreguidelines-macro-usage)
|
||||
#ifdef __cplusplus
|
||||
class slsDetectorDefs {
|
||||
public:
|
||||
@ -556,10 +556,10 @@ enum streamingInterface {
|
||||
int moduleIndex{0};
|
||||
char hostname[MAX_STR_LENGTH];
|
||||
int udpInterfaces{1};
|
||||
int udp_dstport{0};
|
||||
uint16_t udp_dstport{0};
|
||||
uint32_t udp_dstip{0U};
|
||||
uint64_t udp_dstmac{0LU};
|
||||
int udp_dstport2{0};
|
||||
uint16_t udp_dstport2{0};
|
||||
uint32_t udp_dstip2{0U};
|
||||
uint64_t udp_dstmac2{0LU};
|
||||
int64_t frames{0};
|
||||
|
@ -60,6 +60,6 @@ bool is_int(const std::string &s);
|
||||
bool replace_first(std::string *s, const std::string &substr,
|
||||
const std::string &repl);
|
||||
|
||||
std::pair<std::string, int> ParseHostPort(const std::string &s);
|
||||
std::pair<std::string, uint16_t> ParseHostPort(const std::string &s);
|
||||
|
||||
} // namespace sls
|
||||
|
@ -4,10 +4,10 @@
|
||||
#define RELEASE "developer"
|
||||
#define APILIB "developer 0x230224"
|
||||
#define APIRECEIVER "developer 0x230224"
|
||||
#define APICTB "developer 0x230829"
|
||||
#define APIGOTTHARD "developer 0x230829"
|
||||
#define APIGOTTHARD2 "developer 0x230829"
|
||||
#define APIJUNGFRAU "developer 0x230829"
|
||||
#define APIMYTHEN3 "developer 0x230829"
|
||||
#define APIMOENCH "developer 0x230829"
|
||||
#define APIEIGER "developer 0x230829"
|
||||
#define APICTB "developer 0x230922"
|
||||
#define APIGOTTHARD "developer 0x230922"
|
||||
#define APIGOTTHARD2 "developer 0x230922"
|
||||
#define APIJUNGFRAU "developer 0x230922"
|
||||
#define APIMYTHEN3 "developer 0x230922"
|
||||
#define APIMOENCH "developer 0x230922"
|
||||
#define APIEIGER "developer 0x230922"
|
||||
|
@ -1083,6 +1083,17 @@ template <> defs::polarity StringTo(const std::string &s) {
|
||||
throw RuntimeError("Unknown polarity mode " + s);
|
||||
}
|
||||
|
||||
template <> uint16_t StringTo(const std::string &s) {
|
||||
int base = s.find("0x") != std::string::npos ? 16 : 10;
|
||||
int value = std::stoi(s, nullptr, base);
|
||||
if (value < std::numeric_limits<uint16_t>::min() ||
|
||||
value > std::numeric_limits<uint16_t>::max()) {
|
||||
throw RuntimeError("Cannot scan uint16_t from string '" + s +
|
||||
"'. Value must be in range 0 - 65535.");
|
||||
}
|
||||
return static_cast<uint16_t>(value);
|
||||
}
|
||||
|
||||
template <> uint32_t StringTo(const std::string &s) {
|
||||
int base = s.find("0x") != std::string::npos ? 16 : 10;
|
||||
return std::stoul(s, nullptr, base);
|
||||
|
@ -15,8 +15,8 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
UdpRxSocket::UdpRxSocket(int port, ssize_t packet_size, const char *hostname,
|
||||
int kernel_buffer_size)
|
||||
UdpRxSocket::UdpRxSocket(uint16_t port, ssize_t packet_size,
|
||||
const char *hostname, int kernel_buffer_size)
|
||||
: packet_size_(packet_size) {
|
||||
struct addrinfo hints {};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
|
@ -15,7 +15,7 @@ namespace sls {
|
||||
|
||||
using namespace rapidjson;
|
||||
ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
|
||||
const uint32_t portnumber)
|
||||
const uint16_t portnumber)
|
||||
: portno(portnumber), sockfd(false) {
|
||||
// Extra check that throws if conversion fails, could be removed
|
||||
auto ipstr = HostnameToIp(hostname_or_ip).str();
|
||||
@ -55,7 +55,7 @@ ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
|
||||
<< GetReceiveHighWaterMark();
|
||||
}
|
||||
|
||||
ZmqSocket::ZmqSocket(const uint32_t portnumber, const char *ethip)
|
||||
ZmqSocket::ZmqSocket(const uint16_t portnumber, const char *ethip)
|
||||
: portno(portnumber), sockfd(true) {
|
||||
// create context
|
||||
sockfd.contextDescriptor = zmq_ctx_new();
|
||||
@ -289,24 +289,24 @@ int ZmqSocket::ReceiveHeader(const int index, zmqHeader &zHeader,
|
||||
header_buffer.get(), MAX_STR_LENGTH, 0);
|
||||
if (bytes_received > 0) {
|
||||
#ifdef ZMQ_DETAIL
|
||||
cprintf(BLUE, "Header %d [%d] Length: %d Header:%s \n", index, portno,
|
||||
cprintf(BLUE, "Header %d [%hu] Length: %d Header:%s \n", index, portno,
|
||||
bytes_received, header_buffer.get());
|
||||
#endif
|
||||
if (ParseHeader(index, bytes_received, header_buffer.get(), zHeader,
|
||||
version)) {
|
||||
#ifdef ZMQ_DETAIL
|
||||
cprintf(RED, "Parsed Header %d [%d] Length: %d Header:%s \n", index,
|
||||
portno, bytes_received, header_buffer.get());
|
||||
cprintf(RED, "Parsed Header %d [%hu] Length: %d Header:%s \n",
|
||||
index, portno, bytes_received, header_buffer.get());
|
||||
#endif
|
||||
if (!zHeader.data) {
|
||||
#ifdef ZMQ_DETAIL
|
||||
cprintf(RED, "%d [%d] Received end of acquisition\n", index,
|
||||
cprintf(RED, "%d [%hu] Received end of acquisition\n", index,
|
||||
portno);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#ifdef ZMQ_DETAIL
|
||||
cprintf(GREEN, "%d [%d] data\n", index, portno);
|
||||
cprintf(GREEN, "%d [%hu] data\n", index, portno);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <cstring>
|
||||
#include <ifaddrs.h>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <net/if.h>
|
||||
#include <netdb.h>
|
||||
#include <sstream>
|
||||
@ -203,4 +204,18 @@ MacAddr InterfaceNameToMac(const std::string &inf) {
|
||||
return MacAddr(mac);
|
||||
}
|
||||
|
||||
void validatePortNumber(uint16_t port) {
|
||||
// random local port. might work if internal = bad practise
|
||||
if (port == 0) {
|
||||
throw RuntimeError("Invalid port number. Must be between 1 - 65535.");
|
||||
}
|
||||
}
|
||||
|
||||
void validatePortRange(uint16_t startPort, int numPorts) {
|
||||
validatePortNumber(startPort);
|
||||
if ((startPort + numPorts) > std::numeric_limits<uint16_t>::max()) {
|
||||
throw RuntimeError("Invalid port range. Must be between 1 - 65535.");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
@ -4,9 +4,12 @@
|
||||
#include "sls/string_utils.h"
|
||||
#include "sls/container_utils.h"
|
||||
#include "sls/network_utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <sls/ToString.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::vector<std::string> split(const std::string &strToSplit, char delimeter) {
|
||||
@ -50,15 +53,15 @@ bool replace_first(std::string *s, const std::string &substr,
|
||||
return false;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> ParseHostPort(const std::string &s) {
|
||||
std::pair<std::string, uint16_t> ParseHostPort(const std::string &s) {
|
||||
// TODO deal with to many :, port not there?
|
||||
// no port return hostname as is and port as 0
|
||||
std::string host;
|
||||
int port{0};
|
||||
uint16_t port{0};
|
||||
auto res = split(s, ':');
|
||||
host = res[0];
|
||||
if (res.size() > 1) {
|
||||
port = std::stoi(res[1]);
|
||||
port = StringTo<uint16_t>(res[1]);
|
||||
}
|
||||
return std::make_pair(host, port);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
namespace sls {
|
||||
|
||||
TEST_CASE("Throws when cannot create socket") {
|
||||
REQUIRE_THROWS(ZmqSocket("sdiasodjajpvv", 5076001));
|
||||
REQUIRE_THROWS(ZmqSocket("sdiasodjajpvv", 50001));
|
||||
}
|
||||
|
||||
TEST_CASE("Get port number for sub") {
|
||||
|
@ -115,7 +115,7 @@ TEST_CASE("Copy construct a MacAddr") {
|
||||
}
|
||||
|
||||
TEST_CASE("udp dst struct basic properties") {
|
||||
static_assert(sizeof(UdpDestination) == 36,
|
||||
static_assert(sizeof(UdpDestination) == 32,
|
||||
"udpDestination struct size does not match");
|
||||
UdpDestination dst{};
|
||||
REQUIRE(dst.entry == 0);
|
||||
|
Reference in New Issue
Block a user