mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
kept the same client interface:tb changed when fixing error codes/exceptions, modified multi print, fixed roi bug in indexing to sls
This commit is contained in:
@ -14,22 +14,33 @@ void ClientInterface::SetSocket(MySocketTCP *socket) {
|
||||
}
|
||||
|
||||
|
||||
int ClientInterface::Client_GetMesage(char* mess) {
|
||||
bool created = false;
|
||||
int ret = OK;
|
||||
if (!mess){
|
||||
created = true;
|
||||
mess = new char[MAX_STR_LENGTH];
|
||||
memset(mess, 0, MAX_STR_LENGTH);
|
||||
}
|
||||
mySocket->ReceiveDataOnly(mess,MAX_STR_LENGTH);
|
||||
cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess);
|
||||
if(strstr(mess,"Unrecognized Function")!=NULL)
|
||||
ret = FAIL;
|
||||
void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int sizeOfRetval) {
|
||||
// get result of operation
|
||||
mySocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
|
||||
if (created)
|
||||
delete [] mess;
|
||||
return ret;
|
||||
bool unrecognizedFunction = false;
|
||||
if (ret == FAIL) {
|
||||
bool created = false;
|
||||
// allocate mess if null
|
||||
if (!mess){
|
||||
created = true;
|
||||
mess = new char[MAX_STR_LENGTH];
|
||||
memset(mess, 0, MAX_STR_LENGTH);
|
||||
}
|
||||
// get error message
|
||||
mySocket->ReceiveDataOnly(mess,MAX_STR_LENGTH);
|
||||
cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess);
|
||||
|
||||
// unrecognized function, do not ask for retval
|
||||
if(strstr(mess,"Unrecognized Function") != NULL)
|
||||
unrecognizedFunction = true;
|
||||
// delete allocated mess
|
||||
if (created)
|
||||
delete [] mess;
|
||||
}
|
||||
// get retval
|
||||
if (!unrecognizedFunction)
|
||||
mySocket->ReceiveDataOnly(retval, sizeOfRetval);
|
||||
}
|
||||
|
||||
|
||||
@ -37,18 +48,10 @@ int ClientInterface::Client_Send(int fnum,
|
||||
void* args, int sizeOfArgs,
|
||||
void* retval, int sizeOfRetval,
|
||||
char* mess) {
|
||||
|
||||
mySocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
mySocket->SendDataOnly(args, sizeOfArgs);
|
||||
|
||||
int ret = FAIL;
|
||||
mySocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
if (ret == FAIL) {
|
||||
if (Client_GetMesage(mess) == FAIL)
|
||||
return FAIL;
|
||||
}
|
||||
mySocket->ReceiveDataOnly(retval, sizeOfRetval);
|
||||
|
||||
int ret = FAIL;
|
||||
mySocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
mySocket->SendDataOnly(args, sizeOfArgs);
|
||||
Client_Receive(ret, mess, retval, sizeOfRetval);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -34,23 +34,23 @@ public:
|
||||
void SetSocket(MySocketTCP *socket);
|
||||
|
||||
/**
|
||||
* Get message from server
|
||||
* Print appropriate message
|
||||
* Check for Unrecognized function in message and return fail if it does
|
||||
* to prevent getting retval from the server afterwards
|
||||
* @param mess message
|
||||
* @returns FAIL if unrecognized function found in message, else OK
|
||||
* Receive ret, mess or retval from Server
|
||||
* @param ret result of operation
|
||||
* @param mess pointer to message
|
||||
* @param retval poitner to retval
|
||||
* @param sizeOfRetval size of retval
|
||||
*/
|
||||
int Client_GetMesage(char* mess = 0);
|
||||
void Client_Receive(int& ret, char* mess, void* retval, int sizeOfRetval);
|
||||
|
||||
/**
|
||||
* Send Arguments to server and get result back
|
||||
* Send Arguments to server and receives result back
|
||||
* @param fnum function enum to determine what parameter
|
||||
* @param args pointer to arguments
|
||||
* @param sizeOfArgs argument size
|
||||
* @param retval pointer to return value
|
||||
* @param sizeOfRetval return value size
|
||||
* @param mess pointer to message if message required externally
|
||||
* @returns sucess of operation
|
||||
*/
|
||||
int Client_Send(int fnum,
|
||||
void* args, int sizeOfArgs,
|
||||
|
@ -150,7 +150,7 @@ public:
|
||||
sockfd.fd = socket(AF_INET, getProtocol(),0); //tcp
|
||||
|
||||
if (sockfd.fd < 0) {
|
||||
cprintf(RED, "Can not create socket\n");
|
||||
FILE_LOG(logERROR) << "Can not create socket";
|
||||
sockfd.fd =-1;
|
||||
throw SocketException();
|
||||
}
|
||||
@ -173,7 +173,7 @@ public:
|
||||
int val=1;
|
||||
if (setsockopt(sockfd.fd,SOL_SOCKET,SO_REUSEADDR,
|
||||
&val,sizeof(int)) == -1) {
|
||||
cprintf(RED, "setsockopt REUSEADDR failed\n");
|
||||
FILE_LOG(logERROR) << "setsockopt REUSEADDR failed";
|
||||
sockfd.fd =-1;
|
||||
throw SocketException();
|
||||
}
|
||||
@ -192,10 +192,8 @@ public:
|
||||
"Could not get rx socket receive buffer size";
|
||||
} else if (ret_size >= real_size) {
|
||||
actual_udp_socket_buffer_size = ret_size;
|
||||
#ifdef VEBOSE
|
||||
FILE_LOG(logINFO) << "[Port " << port_number << "] "
|
||||
FILE_LOG(logDEBUG1) << "[Port " << port_number << "] "
|
||||
"UDP rx socket buffer size is sufficient (" << ret_size << ")";
|
||||
#endif
|
||||
}
|
||||
|
||||
// not sufficient, enhance size
|
||||
@ -244,7 +242,7 @@ public:
|
||||
|
||||
|
||||
if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
cprintf(RED, "Can not bind socket\n");
|
||||
FILE_LOG(logERROR) << "Can not bind socket";
|
||||
sockfd.fd =-1;
|
||||
throw SocketException();
|
||||
}
|
||||
@ -313,7 +311,7 @@ public:
|
||||
case UDP:
|
||||
return SOCK_DGRAM;
|
||||
default:
|
||||
cprintf(RED, "unknown protocol %d\n", p);
|
||||
FILE_LOG(logERROR) << "unknown protocol: " << p;
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
@ -365,74 +363,70 @@ public:
|
||||
if(is_a_server && protocol==TCP){ //server tcp; the server will wait for the clients connection
|
||||
if (sockfd.fd>0) {
|
||||
if ((sockfd.newfd = accept(sockfd.fd,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) {
|
||||
cprintf(RED, "Error: with server accept, connection refused\n");
|
||||
FILE_LOG(logERROR) << "with server accept, connection refused";
|
||||
switch(errno) {
|
||||
case EWOULDBLOCK:
|
||||
printf("ewouldblock eagain\n");
|
||||
FILE_LOG(logERROR) << "ewouldblock eagain";
|
||||
break;
|
||||
case EBADF:
|
||||
printf("ebadf\n");
|
||||
FILE_LOG(logERROR) << "ebadf";
|
||||
break;
|
||||
case ECONNABORTED:
|
||||
printf("econnaborted\n");
|
||||
FILE_LOG(logERROR) << "econnaborted";
|
||||
break;
|
||||
case EFAULT:
|
||||
printf("efault\n");
|
||||
FILE_LOG(logERROR) << "efault";
|
||||
break;
|
||||
case EINTR:
|
||||
printf("eintr\n");
|
||||
FILE_LOG(logERROR) << "eintr";
|
||||
break;
|
||||
case EINVAL:
|
||||
printf("einval\n");
|
||||
FILE_LOG(logERROR) << "einval";
|
||||
break;
|
||||
case EMFILE:
|
||||
printf("emfile\n");
|
||||
FILE_LOG(logERROR) << "emfile";
|
||||
break;
|
||||
case ENFILE:
|
||||
printf("enfile\n");
|
||||
FILE_LOG(logERROR) << "enfile";
|
||||
break;
|
||||
case ENOTSOCK:
|
||||
printf("enotsock\n");
|
||||
FILE_LOG(logERROR) << "enotsock";
|
||||
break;
|
||||
case EOPNOTSUPP:
|
||||
printf("eOPNOTSUPP\n");
|
||||
FILE_LOG(logERROR) << "eOPNOTSUPP";
|
||||
break;
|
||||
case ENOBUFS:
|
||||
printf("ENOBUFS\n");
|
||||
FILE_LOG(logERROR) << "ENOBUFS";
|
||||
break;
|
||||
case ENOMEM:
|
||||
printf("ENOMEM\n");
|
||||
FILE_LOG(logERROR) << "ENOMEM";
|
||||
break;
|
||||
case ENOSR:
|
||||
printf("ENOSR\n");
|
||||
FILE_LOG(logERROR) << "ENOSR";
|
||||
break;
|
||||
case EPROTO:
|
||||
printf("EPROTO\n");
|
||||
FILE_LOG(logERROR) << "EPROTO";
|
||||
break;
|
||||
default:
|
||||
printf("unknown error\n");
|
||||
FILE_LOG(logERROR) << "unknown error";
|
||||
}
|
||||
}
|
||||
else{
|
||||
inet_ntop(AF_INET, &(clientAddress.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "client connected "<< sockfd.newfd << endl;
|
||||
#endif
|
||||
FILE_LOG(logDEBUG1) << "client connected " << sockfd.newfd;
|
||||
}
|
||||
}
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "fd " << sockfd.newfd << endl;
|
||||
#endif
|
||||
FILE_LOG(logDEBUG1) << "fd " << sockfd.newfd;
|
||||
return sockfd.newfd;
|
||||
} else {
|
||||
if (sockfd.fd<=0)
|
||||
sockfd.fd = socket(AF_INET, getProtocol(),0);
|
||||
// SetTimeOut(10);
|
||||
if (sockfd.fd < 0){
|
||||
cprintf(RED, "Can not create socket\n");
|
||||
FILE_LOG(logERROR) << "Can not create socket";
|
||||
} else {
|
||||
if(connect(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
cprintf(RED, "Can not connect to socket\n");
|
||||
FILE_LOG(logERROR) << "Can not connect to socket";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -470,13 +464,13 @@ public:
|
||||
tout.tv_usec = 0;
|
||||
if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVTIMEO,
|
||||
&tout, sizeof(struct timeval)) <0) {
|
||||
cprintf(RED, "Error in setsockopt SO_RCVTIMEO %d\n", 0);
|
||||
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
|
||||
}
|
||||
tout.tv_sec = ts;
|
||||
tout.tv_usec = 0;
|
||||
if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_SNDTIMEO,
|
||||
&tout, sizeof(struct timeval)) < 0) {
|
||||
cprintf(RED, "Error in setsockopt SO_SNDTIMEO %d\n", ts);
|
||||
FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << ts;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
@ -508,7 +502,6 @@ public:
|
||||
sa = (struct sockaddr_in *)(iap->ifa_addr);
|
||||
inet_ntop(iap->ifa_addr->sa_family, (void *)&(sa->sin_addr), buf, buf_len);
|
||||
if (ip==std::string(buf)) {
|
||||
//printf("%s\n", iap->ifa_name);
|
||||
strcpy(buf,iap->ifa_name);
|
||||
break;
|
||||
}
|
||||
@ -615,17 +608,17 @@ public:
|
||||
// get host info into res
|
||||
int errcode = getaddrinfo (hostname, NULL, &hints, res);
|
||||
if (errcode != 0) {
|
||||
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq):"
|
||||
"%s\n", hostname, gai_strerror(errcode));
|
||||
FILE_LOG(logERROR) << "Could not convert hostname (" << hostname << ") to internet address (zmq):" <<
|
||||
gai_strerror(errcode);
|
||||
} else {
|
||||
if (*res == NULL) {
|
||||
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq): "
|
||||
"gettaddrinfo returned null\n", hostname);
|
||||
FILE_LOG(logERROR) << "Could not converthostname (" << hostname << ") to internet address (zmq):"
|
||||
"gettaddrinfo returned null";
|
||||
} else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
cprintf(RED, "Error: Could not convert hostname to internet address\n");
|
||||
FILE_LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
return 1;
|
||||
};
|
||||
|
||||
@ -643,7 +636,7 @@ public:
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
cprintf(RED, "Error: Could not convert internet address to ip string\n");
|
||||
FILE_LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -697,7 +690,7 @@ public:
|
||||
continue;
|
||||
if(nsent != nsending){
|
||||
if(nsent && (nsent != -1))
|
||||
cprintf(RED,"Incomplete Packet size %d\n",nsent);
|
||||
FILE_LOG(logERROR) << "Incomplete Packet size " << nsent;
|
||||
break;
|
||||
}
|
||||
length-=nsent;
|
||||
@ -709,16 +702,13 @@ public:
|
||||
//normal
|
||||
nsending=packet_size;
|
||||
while(1){
|
||||
#ifdef VERYVERBOSE
|
||||
cprintf(BLUE,"%d gonna listen\n", portno); fflush(stdout);
|
||||
#endif
|
||||
nsent = recvfrom(sockfd.fd,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, &clientAddress_length);
|
||||
//break out of loop only if read one packets size or read didnt work (cuz of shutdown)
|
||||
if(nsent<=0 || nsent == packet_size)
|
||||
break;
|
||||
//incomplete packets or header packets ignored and read buffer again
|
||||
if(nsent != packet_size && nsent != header_packet_size)
|
||||
cprintf(RED,"%d Incomplete Packet size %d\n", portno, nsent);
|
||||
FILE_LOG(logERROR) << portno << ": Incomplete Packet size " << nsent;
|
||||
}
|
||||
//nsent = 1040;
|
||||
if(nsent > 0)total_sent+=nsent;
|
||||
@ -727,9 +717,7 @@ public:
|
||||
default:
|
||||
;
|
||||
}
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "sent "<< total_sent << " Bytes" << endl;
|
||||
#endif
|
||||
FILE_LOG(logDEBUG1) << "sent " << total_sent << " Bytes";
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
@ -740,9 +728,7 @@ public:
|
||||
* @returns size of data sent
|
||||
*/
|
||||
int SendDataOnly(void *buf, int length) {
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "want to send "<< length << " Bytes" << endl;
|
||||
#endif
|
||||
FILE_LOG(logDEBUG1) << "want to send " << length << " Bytes";
|
||||
if (buf==NULL) return -1;
|
||||
|
||||
total_sent=0;
|
||||
@ -757,7 +743,7 @@ public:
|
||||
nsending = (length>packet_size) ? packet_size:length;
|
||||
nsent = write(tcpfd,(char*)buf+total_sent,nsending);
|
||||
if(is_a_server && nsent < 0) {
|
||||
cprintf(BG_RED, "Error writing to socket. Possible client socket crash\n");
|
||||
FILE_LOG(logERROR) << "Could not write to socket. Possible client socket crash";
|
||||
break;
|
||||
}
|
||||
if(!nsent) break;
|
||||
@ -779,9 +765,7 @@ public:
|
||||
default:
|
||||
;
|
||||
}
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "sent "<< total_sent << " Bytes" << endl;
|
||||
#endif
|
||||
FILE_LOG(logDEBUG1) << "sent "<< total_sent << " Bytes";
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user