mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 01:58:00 +02:00
zmqip now uint
This commit is contained in:
@ -3,10 +3,13 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
namespace sls {
|
||||
|
||||
/* Base class for TCP socket, this is used to send data between detector, client
|
||||
and receiver. Specific protocols inherit from this class.*/
|
||||
|
||||
class DataSocket {
|
||||
public:
|
||||
DataSocket(int socketId);
|
||||
@ -14,27 +17,28 @@ class DataSocket {
|
||||
virtual ~DataSocket();
|
||||
DataSocket &operator=(DataSocket &&move) noexcept;
|
||||
void swap(DataSocket &other) noexcept;
|
||||
|
||||
//No copy since the class manage the underlying socket
|
||||
DataSocket(const DataSocket &) = delete;
|
||||
DataSocket &operator=(DataSocket const &) = delete;
|
||||
int getSocketId() const { return socketId_; }
|
||||
|
||||
|
||||
int Send(const void *buffer, size_t size);
|
||||
template <typename T> int Send(T &&data) {
|
||||
return Send(&data, sizeof(data));
|
||||
}
|
||||
|
||||
// Trick to send all
|
||||
// Variadic template to send all arguments
|
||||
template <class... Args> int SendAll(Args &&... args) {
|
||||
auto l = std::initializer_list<int>{Send(args)...};
|
||||
auto sum = std::accumulate(begin(l), end(l), 0);
|
||||
return sum;
|
||||
}
|
||||
|
||||
int Receive(void *buffer, size_t size);
|
||||
|
||||
template <typename T> int Receive(T &arg) {
|
||||
return Receive(&arg, sizeof(arg));
|
||||
}
|
||||
|
||||
template <typename T> T Receive() {
|
||||
T arg;
|
||||
Receive(&arg, sizeof(arg));
|
||||
@ -52,12 +56,4 @@ class DataSocket {
|
||||
int socketId_ = -1;
|
||||
};
|
||||
|
||||
int ConvertHostnameToInternetAddress(const char *const hostname,
|
||||
struct ::addrinfo **res);
|
||||
int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip,
|
||||
const int ipsize);
|
||||
|
||||
struct ::sockaddr_in
|
||||
ConvertHostnameToInternetAddress(const std::string &hostname);
|
||||
|
||||
}; // namespace sls
|
||||
|
@ -56,7 +56,7 @@ class MacAddr {
|
||||
constexpr uint64_t uint64() const noexcept { return addr_; }
|
||||
};
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
IpAddr HostnameToIp(const char *hostname);
|
||||
std::string IpToInterfaceName(const std::string& ip);
|
||||
MacAddr InterfaceNameToMac(std::string inf);
|
||||
|
||||
|
@ -27,7 +27,6 @@ DataSocket::~DataSocket() {
|
||||
try {
|
||||
close();
|
||||
} catch (...) {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -73,7 +72,7 @@ int DataSocket::Send(const void *buffer, size_t size) {
|
||||
break;
|
||||
bytes_sent += this_send;
|
||||
}
|
||||
if (bytes_sent != data_size){
|
||||
if (bytes_sent != data_size) {
|
||||
std::ostringstream ss;
|
||||
ss << "TCP socket sent " << bytes_sent << " bytes instead of "
|
||||
<< data_size << " bytes";
|
||||
@ -127,7 +126,6 @@ void DataSocket::close() {
|
||||
throw SocketError("could not close socket");
|
||||
}
|
||||
socketId_ = -1;
|
||||
|
||||
} else {
|
||||
throw std::runtime_error("Socket ERROR: close called on bad socket\n");
|
||||
}
|
||||
@ -138,73 +136,4 @@ void DataSocket::shutDownSocket() {
|
||||
close();
|
||||
}
|
||||
|
||||
struct sockaddr_in
|
||||
ConvertHostnameToInternetAddress(const std::string &hostname) {
|
||||
struct addrinfo hints, *result;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags |= AI_CANONNAME;
|
||||
|
||||
struct sockaddr_in serverAddr {};
|
||||
if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result) != 0) {
|
||||
freeaddrinfo(result);
|
||||
std::string msg = "ClientSocket cannot decode host:" + hostname + "\n";
|
||||
throw SocketError(msg);
|
||||
}
|
||||
serverAddr.sin_family = AF_INET;
|
||||
memcpy((char *)&serverAddr.sin_addr.s_addr,
|
||||
&((struct sockaddr_in *)result->ai_addr)->sin_addr,
|
||||
sizeof(in_addr_t));
|
||||
freeaddrinfo(result);
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
int ConvertHostnameToInternetAddress(const char *const hostname,
|
||||
struct ::addrinfo **res) {
|
||||
// criteria in selecting socket address structures returned by res
|
||||
struct ::addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
// get host info into res
|
||||
int errcode = getaddrinfo(hostname, nullptr, &hints, res);
|
||||
if (errcode != 0) {
|
||||
FILE_LOG(logERROR) << "Could not convert hostname (" << hostname
|
||||
<< ") to internet address (zmq):"
|
||||
<< gai_strerror(errcode);
|
||||
} else {
|
||||
if (*res == nullptr) {
|
||||
FILE_LOG(logERROR) << "Could not converthostname (" << hostname
|
||||
<< ") to internet address (zmq):"
|
||||
"gettaddrinfo returned null";
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
return 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert Internet Address structure pointer to ip string (char*)
|
||||
* Clears the internet address structure as well
|
||||
* @param res pointer to internet address structure
|
||||
* @param ip pointer to char array to store result in
|
||||
* @param ipsize size available in ip buffer
|
||||
* @return 1 for fail, 0 for success
|
||||
*/
|
||||
// Do not make this static (for multi threading environment)
|
||||
int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip,
|
||||
const int ipsize) {
|
||||
if (inet_ntop(res->ai_family,
|
||||
&((struct sockaddr_in *)res->ai_addr)->sin_addr, ip,
|
||||
ipsize) != nullptr) {
|
||||
::freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
@ -75,7 +75,7 @@ std::ostream &operator<<(std::ostream &out, const MacAddr &addr) {
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname) {
|
||||
IpAddr HostnameToIp(const char *hostname) {
|
||||
addrinfo hints;
|
||||
addrinfo *result = nullptr;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
@ -87,7 +87,7 @@ uint32_t HostnameToIp(const char *hostname) {
|
||||
}
|
||||
uint32_t ip = ((sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
|
||||
freeaddrinfo(result);
|
||||
return ip;
|
||||
return IpAddr(ip);
|
||||
}
|
||||
|
||||
std::string IpToInterfaceName(const std::string &ip) {
|
||||
|
Reference in New Issue
Block a user