mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 05:17:13 +02:00
template for sending to detector
This commit is contained in:
@ -10,7 +10,8 @@
|
||||
#include <unistd.h>
|
||||
namespace sls {
|
||||
|
||||
ClientSocket::ClientSocket(std::string stype, const std::string &host, uint16_t port)
|
||||
ClientSocket::ClientSocket(std::string stype, const std::string &host,
|
||||
uint16_t port)
|
||||
: DataSocket(socket(AF_INET, SOCK_STREAM, 0)), socketType(stype) {
|
||||
|
||||
struct addrinfo hints, *result;
|
||||
@ -20,22 +21,25 @@ ClientSocket::ClientSocket(std::string stype, const std::string &host, uint16_t
|
||||
hints.ai_flags |= AI_CANONNAME;
|
||||
|
||||
if (getaddrinfo(host.c_str(), NULL, &hints, &result) != 0) {
|
||||
std::string msg =
|
||||
"ClientSocket cannot decode host:" + host + " on port " + std::to_string(port) + "\n";
|
||||
std::string msg = "ClientSocket cannot decode host:" + host +
|
||||
" on port " + std::to_string(port) + "\n";
|
||||
throw SocketError(msg);
|
||||
}
|
||||
|
||||
// TODO! Erik, results could have multiple entries do we need to loop through them?
|
||||
// struct sockaddr_in serverAddr {};
|
||||
// TODO! Erik, results could have multiple entries do we need to loop
|
||||
// through them? struct sockaddr_in serverAddr {};
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_port = htons(port);
|
||||
memcpy((char *)&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr,
|
||||
memcpy((char *)&serverAddr.sin_addr.s_addr,
|
||||
&((struct sockaddr_in *)result->ai_addr)->sin_addr,
|
||||
sizeof(in_addr_t));
|
||||
|
||||
if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
|
||||
if (::connect(getSocketId(), (struct sockaddr *)&serverAddr,
|
||||
sizeof(serverAddr)) != 0) {
|
||||
freeaddrinfo(result);
|
||||
std::string msg = "ClientSocket: Cannot connect to " + socketType + ":" +
|
||||
host + " on port " + std::to_string(port) + "\n";
|
||||
std::string msg = "ClientSocket: Cannot connect to " + socketType +
|
||||
":" + host + " on port " + std::to_string(port) +
|
||||
"\n";
|
||||
throw SocketError(msg);
|
||||
}
|
||||
freeaddrinfo(result);
|
||||
@ -47,13 +51,15 @@ ClientSocket::ClientSocket(std::string sType, struct sockaddr_in addr)
|
||||
if (::connect(getSocketId(), (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
char address[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &addr.sin_addr, address, INET_ADDRSTRLEN);
|
||||
std::string msg = "ClientSocket: Cannot connect to " + socketType + ":" + address + " on port " +
|
||||
std::string msg = "ClientSocket: Cannot connect to " + socketType +
|
||||
":" + address + " on port " +
|
||||
std::to_string(addr.sin_port) + "\n";
|
||||
throw SocketError(msg);
|
||||
}
|
||||
}
|
||||
|
||||
int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval,
|
||||
int ClientSocket::sendCommandThenRead(int fnum, const void *args,
|
||||
size_t args_size, void *retval,
|
||||
size_t retval_size) {
|
||||
int ret = slsDetectorDefs::FAIL;
|
||||
sendData(&fnum, sizeof(fnum));
|
||||
@ -69,7 +75,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
|
||||
char mess[MAX_STR_LENGTH]{};
|
||||
// get error message
|
||||
receiveData(mess, sizeof(mess));
|
||||
FILE_LOG(logERROR) << socketType << " returned error: " << mess;
|
||||
FILE_LOG(logERROR) << socketType << " returned error: " << mess;
|
||||
std::cout << "\n"; // needed to reset the color.
|
||||
|
||||
// Do we need to know hostname here?
|
||||
|
Reference in New Issue
Block a user