Exceptions handling in constructor for genericSocket, created object to handle both socket descriptors upon throwing exception in constructor (as raw pointers wont get destructed automatically)

This commit is contained in:
2018-08-09 15:43:56 +02:00
parent ab7e63c20f
commit 1102153d2b
14 changed files with 309 additions and 357 deletions

View File

@ -21,34 +21,44 @@ using namespace std;
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
qClient* cl = 0;
qClient *cl =new qClient(argv[1]); try {
qClient *c = new qClient(argv[1]);
cl = c;
} catch(...) {
return 0;
}
cl->executeLine(argc-2, argv+2); cl->executeLine(argc-2, argv+2);
delete cl; delete cl;
return 0;
} }
//------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------
qClient::qClient(char* hostname){ qClient::qClient(char* hostname):
//create socket mySocket(0),
mySocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO); myStopSocket(0){
if (mySocket->getErrorStatus()){
cout << "Error: could not connect to host:" << hostname << " with port " << DEFAULT_GUI_PORTNO << endl;
delete mySocket;
exit(-1);
}
//create socket to connect to stop server
myStopSocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO+1);
if (myStopSocket->getErrorStatus()){
cout << "Error: could not connect to host:" << hostname << " with port " << DEFAULT_GUI_PORTNO + 1 << endl;
delete myStopSocket;
exit(-1);
}
try {
// control socket
MySocketTCP* s = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO);
mySocket = s;
// stop socket
s = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO+1);
myStopSocket = s;
} catch(...) {
if (mySocket == 0)
cout << "Error: could not connect to control server:" <<
hostname << " with port " << DEFAULT_GUI_PORTNO << endl;
else
cout << "Error: could not connect to stop server:" <<
hostname << " with port " << DEFAULT_GUI_PORTNO + 1 << endl;
throw;
}
} }
@ -93,7 +103,7 @@ int qClient::executeLine(int narg, char *args[]){
else if (argument == "stop") else if (argument == "stop")
stopAcquisition(); stopAcquisition();
else{ else{
cout << "Error: could not parse arguments: " << argument << endl; cprintf(RED,"Error: could not parse arguments: %s\n", argument.c_str());
printCommands(); printCommands();
return FAIL; return FAIL;
} }

View File

@ -27,7 +27,7 @@ int qServer::gui_server_thread_running(0);
qServer::qServer(qDetectorMain *t): qServer::qServer(qDetectorMain *t):
myMainTab(t), mySocket(NULL),myStopSocket(NULL),port_no(DEFAULT_GUI_PORTNO),lockStatus(0),checkStarted(0),checkStopStarted(0){ myMainTab(t), mySocket(0),myStopSocket(0),port_no(DEFAULT_GUI_PORTNO),lockStatus(0),checkStarted(0),checkStopStarted(0){
strcpy(mess,""); strcpy(mess,"");
FunctionTable(); FunctionTable();
@ -196,7 +196,7 @@ int qServer::StartStopServer(int start){
pthread_join(gui_server_thread,NULL); pthread_join(gui_server_thread,NULL);
if(mySocket){ if(mySocket){
delete mySocket; delete mySocket;
mySocket = NULL; mySocket = 0;
} }
if(myStopSocket) if(myStopSocket)
@ -204,7 +204,7 @@ int qServer::StartStopServer(int start){
pthread_join(gui_stop_server_thread,NULL); pthread_join(gui_stop_server_thread,NULL);
if(myStopSocket){ if(myStopSocket){
delete myStopSocket; delete myStopSocket;
myStopSocket = NULL; myStopSocket = 0;
} }
} }
#ifdef VERBOSE #ifdef VERBOSE
@ -235,8 +235,10 @@ int qServer::StopServer(){
#endif #endif
int ret = qDefs::OK; int ret = qDefs::OK;
myStopSocket = new MySocketTCP(port_no+1); try {
if (myStopSocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(port_no+1);
myStopSocket = s;
} catch(...) {
gui_server_thread_running = 0; gui_server_thread_running = 0;
qDefs::Message(qDefs::WARNING,"Could not start gui stop server socket","qServer::StopServer"); qDefs::Message(qDefs::WARNING,"Could not start gui stop server socket","qServer::StopServer");
} }
@ -270,7 +272,7 @@ int qServer::StopServer(){
//delete socket(via exit server) //delete socket(via exit server)
if(myStopSocket){ if(myStopSocket){
delete myStopSocket; delete myStopSocket;
myStopSocket = NULL; myStopSocket = 0;
} }
if(!gui_server_thread_running) if(!gui_server_thread_running)
@ -300,8 +302,10 @@ int qServer::StartServer(){
#endif #endif
int ret = qDefs::OK; int ret = qDefs::OK;
mySocket = new MySocketTCP(port_no); try {
if (mySocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(port_no);
mySocket = s;
} catch(...) {
gui_server_thread_running = 0; gui_server_thread_running = 0;
qDefs::Message(qDefs::WARNING,"Could not start gui server socket","qServer::StartServer"); qDefs::Message(qDefs::WARNING,"Could not start gui server socket","qServer::StartServer");
} }
@ -335,7 +339,7 @@ int qServer::StartServer(){
//delete socket(via exit server) //delete socket(via exit server)
if(mySocket){ if(mySocket){
delete mySocket; delete mySocket;
mySocket = NULL; mySocket = 0;
} }
if(!gui_server_thread_running) if(!gui_server_thread_running)

View File

@ -88,24 +88,27 @@ int main(int argc, char* argv[])
int ret=slsReceiverDefs::FAIL; int ret=slsReceiverDefs::FAIL;
int arg[2]={idx,0}; int arg[2]={idx,0};
MySocketTCP* mySocket = 0;
MySocketTCP* tempSocket=new MySocketTCP(argv[1],1952); try {
if (tempSocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(argv[1],1952);
mySocket = s;
} catch (...) {
cerr << "could not create socket with " << argv[1] << endl; cerr << "could not create socket with " << argv[1] << endl;
help(); help();
} }
if (tempSocket->Connect()) { if (mySocket->Connect()) {
tempSocket->SendDataOnly(&fnum, sizeof(fnum)); mySocket->SendDataOnly(&fnum, sizeof(fnum));
tempSocket->SendDataOnly(arg,sizeof(arg)); mySocket->SendDataOnly(arg,sizeof(arg));
tempSocket->ReceiveDataOnly(&ret, sizeof(ret)); mySocket->ReceiveDataOnly(&ret, sizeof(ret));
if (ret != slsReceiverDefs::FAIL) { if (ret != slsReceiverDefs::FAIL) {
tempSocket->ReceiveDataOnly(&retval, sizeof(retval)); mySocket->ReceiveDataOnly(&retval, sizeof(retval));
} else { } else {
tempSocket->ReceiveDataOnly(mess,sizeof(mess)); mySocket->ReceiveDataOnly(mess,sizeof(mess));
printf("Detector returned Error: %s",mess); printf("Detector returned Error: %s",mess);
} }
tempSocket->Disconnect(); mySocket->Disconnect();
} else } else
cerr << "could not connect to " << argv[1] << endl; cerr << "could not connect to " << argv[1] << endl;

View File

@ -1287,29 +1287,39 @@ slsReceiverDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
slsDetectorDefs::detectorType slsDetector::getDetectorType(const char *name, int cport) { slsDetectorDefs::detectorType slsDetector::getDetectorType(const char *name, int cport) {
int fnum=F_GET_DETECTOR_TYPE; int fnum=F_GET_DETECTOR_TYPE;
int retval = FAIL; int retval = FAIL;
detectorType t=GENERIC; detectorType t = GENERIC;
MySocketTCP *s= new MySocketTCP(name, cport); MySocketTCP* mySocket = 0;
try {
MySocketTCP* s= new MySocketTCP(name, cport);
mySocket = s;
} catch(...) {
cout << "Cannot create socket to server " << name << " over port " << cport << endl;
return t;
}
char m[MAX_STR_LENGTH]; char m[MAX_STR_LENGTH];
#ifdef VERBOSE #ifdef VERBOSE
cout << "Getting detector type " << endl; cout << "Getting detector type " << endl;
#endif #endif
if (s->Connect() >= 0) { if (mySocket->Connect() >= 0) {
s->SendDataOnly(&fnum,sizeof(fnum)); mySocket->SendDataOnly(&fnum,sizeof(fnum));
s->ReceiveDataOnly(&retval,sizeof(retval)); mySocket->ReceiveDataOnly(&retval,sizeof(retval));
if (retval!=FAIL) { if (retval!=FAIL) {
s->ReceiveDataOnly(&t,sizeof(t)); mySocket->ReceiveDataOnly(&t,sizeof(t));
#ifdef VERBOSE #ifdef VERBOSE
cout << "Detector type is "<< t << endl; cout << "Detector type is "<< t << endl;
#endif #endif
} else { } else {
s->ReceiveDataOnly(m,sizeof(m)); mySocket->ReceiveDataOnly(m,sizeof(m));
std::cout<< "Detector returned error: " << m << std::endl; std::cout<< "Detector returned error: " << m << std::endl;
} }
s->Disconnect(); mySocket->Disconnect();
} else { } else {
cout << "Cannot connect to server " << name << " over port " << cport << endl; cout << "Cannot connect to server " << name << " over port " << cport << endl;
} }
delete s; delete mySocket;
return t; return t;
} }
@ -1834,40 +1844,46 @@ int slsDetector::setTCPSocket(string const name, int const control_port, int con
thisSP=thisDetector->stopPort; thisSP=thisDetector->stopPort;
// create control socket
if (!controlSocket) { if (!controlSocket) {
controlSocket= new MySocketTCP(thisName, thisCP); try {
if (controlSocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(thisName, thisCP);
#ifdef VERBOSE controlSocket = s;
std::cout<< "Could not connect Control socket " << thisName << " "
<< thisCP << std::endl;
#endif
delete controlSocket;
controlSocket=NULL;
retval=FAIL;
}
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
else std::cout<< "Control socket connected " <<
std::cout<< "Control socket connected " <<thisName << " " << thisCP thisName << " " << thisCP << std::endl;
<< std::endl;
#endif #endif
} catch(...) {
#ifdef VERBOSE
std::cout<< "Could not connect Control socket " <<
thisName << " " << thisCP << std::endl;
#endif
controlSocket = NULL;
retval = FAIL;
}
} }
// create stop socket
if (!stopSocket) { if (!stopSocket) {
stopSocket=new MySocketTCP(thisName, thisSP); try {
if (stopSocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(thisName, thisSP);
#ifdef VERBOSE stopSocket = s;
std::cout<< "Could not connect Stop socket "<<thisName << " " << thisSP
<< std::endl;
#endif
delete stopSocket;
stopSocket=NULL;
retval=FAIL;
}
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
else std::cout<< "Stop socket connected " <<
std::cout<< "Stop socket connected " << thisName << " " << thisSP thisName << " " << thisSP << std::endl;
<< std::endl;
#endif #endif
} catch(...) {
#ifdef VERBOSE
std::cout<< "Could not connect Stop socket " <<
thisName << " " << thisSP << std::endl;
#endif
stopSocket = NULL;
retval = FAIL;
}
} }
if (retval!=FAIL) { if (retval!=FAIL) {
checkOnline(); checkOnline();
@ -8313,21 +8329,24 @@ int slsDetector::setReceiverTCPSocket(string const name, int const receiver_port
//create data socket //create data socket
if (!dataSocket) { if (!dataSocket) {
dataSocket=new MySocketTCP(thisName, thisRP); try {
if (dataSocket->getErrorStatus()){ MySocketTCP* s = new MySocketTCP(thisName, thisRP);
#ifdef VERBOSE dataSocket = s;
std::cout<< "Could not connect Data socket "<<thisName << " " <<
thisRP << std::endl;
#endif
delete dataSocket;
dataSocket=NULL;
retval=FAIL;
}
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
else std::cout<< "Data socket connected " <<
std::cout<< "Data socket connected "<< thisName << " " << thisRP << std::endl; thisName << " " << thisRP << std::endl;
#endif #endif
} catch(...) {
#ifdef VERBOSE
std::cout<< "Could not connect Data socket " <<
thisName << " " << thisRP << std::endl;
#endif
dataSocket = NULL;
retval = FAIL;
}
} }
//check if it connects //check if it connects
if (retval!=FAIL) { if (retval!=FAIL) {
checkReceiverOnline(); checkReceiverOnline();

View File

@ -7,28 +7,9 @@
* @author Anna Bergamaschi * @author Anna Bergamaschi
* @version 0.0 * @version 0.0
*/ */
//version 1.0, base development, Ian 19/01/09
/* Modified by anna on 19.01.2009 */
/*
canceled SetupParameters() and varaibles intialized in the constructors' headers;
defined SEND_REC_MAX_SIZE (for compatibilty with mythen (and possibly other) pure C servers (i would move it to the common header file)
added #ifndef C_ONLY... to cutout class definition when including in pure C servers (can be removed if SEND_REC_MAX_SIZE is moved to the common header file)
defined private variables char hostname[1000] and int portno to store connection informations;
defined public functions int getHostname(char *name) and int getPortNumber() to retrieve connection informations
added public function int getErrorStatus() returning 1 if socketDescriptor<0
remove exits in the constructors and replace them with socketDescriptor=-1
replaced the argument of send/receive data with void (to avoid too much casting or compiler errors/warnings)
added a function which really does not close the socket between send/receive (senddataonly, receivedataonly)
*/
#include "ansi.h" #include "ansi.h"
#include "sls_receiver_exceptions.h"
#ifdef __CINT__ #ifdef __CINT__
//class sockaddr_in; //class sockaddr_in;
@ -69,6 +50,31 @@ using namespace std;
#define DEFAULT_BACKLOG 5 #define DEFAULT_BACKLOG 5
/**
* Class to close socket descriptors automatically
* upon encountering exceptions in the constructor
*/
class mySocketDescriptors {
public:
mySocketDescriptors():fd(-1), newfd(-1){};
~mySocketDescriptors() {
// close TCP server new socket descriptor from accept
if (newfd >= 0) {
close(newfd);
}
// close socket descriptor
if (fd >= 0) {
close(fd);
}
}
/** socket descriptor */
int fd;
/** new socket descriptor in TCP server from accept */
int newfd;
};
class genericSocket{ class genericSocket{
public: public:
@ -81,6 +87,7 @@ public:
/** /**
* The constructor for a client * The constructor for a client
* throws an exception if the hostname/ip could not be converted to an internet address
* @param host_ip_or_name hostname or ip of the client * @param host_ip_or_name hostname or ip of the client
* @param port_number port number to connect to * @param port_number port number to connect to
* @param p TCP or UDP * @param p TCP or UDP
@ -92,8 +99,6 @@ public:
portno(port_number), portno(port_number),
protocol(p), protocol(p),
is_a_server(0), is_a_server(0),
socketDescriptor(-1),
file_des(-1),
packet_size(ps), packet_size(ps),
nsending(0), nsending(0),
nsent(0), nsent(0),
@ -108,20 +113,23 @@ public:
differentClients = 0; differentClients = 0;
struct addrinfo *result; struct addrinfo *result;
if (!ConvertHostnameToInternetAddress(host_ip_or_name, &result)) { if (ConvertHostnameToInternetAddress(host_ip_or_name, &result)) {
serverAddress.sin_family = result->ai_family; sockfd.fd = -1;
memcpy((char *) &serverAddress.sin_addr.s_addr, throw SocketException();
&((struct sockaddr_in *) result->ai_addr)->sin_addr, sizeof(in_addr_t));
freeaddrinfo(result);
serverAddress.sin_port = htons(port_number);
socketDescriptor=0;
} }
sockfd.fd = 0;
serverAddress.sin_family = result->ai_family;
memcpy((char *) &serverAddress.sin_addr.s_addr,
&((struct sockaddr_in *) result->ai_addr)->sin_addr, sizeof(in_addr_t));
freeaddrinfo(result);
serverAddress.sin_port = htons(port_number);
clientAddress_length=sizeof(clientAddress); clientAddress_length=sizeof(clientAddress);
}; };
/** /**
* The constructor for a server * The constructor for a server
* throws an exception if * throws an exception if socket could not be created, closes descriptor before throwing
* @param port_number port number to connect to * @param port_number port number to connect to
* @param p TCP or UDP * @param p TCP or UDP
* @param ps a single packet size * @param ps a single packet size
@ -133,8 +141,6 @@ public:
portno(port_number), portno(port_number),
protocol(p), protocol(p),
is_a_server(1), is_a_server(1),
socketDescriptor(-1),
file_des(-1),
packet_size(ps), packet_size(ps),
nsending(0), nsending(0),
nsent(0), nsent(0),
@ -152,8 +158,8 @@ public:
// same port // same port
if(serverAddress.sin_port == htons(port_number)){ if(serverAddress.sin_port == htons(port_number)){
socketDescriptor = -10; sockfd.fd = -10;
return; throw SamePortSocketException();
} }
char ip[20]; char ip[20];
@ -166,11 +172,12 @@ public:
strcpy(ip,eth); strcpy(ip,eth);
} }
socketDescriptor = socket(AF_INET, getProtocol(),0); //tcp sockfd.fd = socket(AF_INET, getProtocol(),0); //tcp
if (socketDescriptor < 0) { if (sockfd.fd < 0) {
cprintf(RED, "Can not create socket\n"); cprintf(RED, "Can not create socket\n");
return; sockfd.fd =-1;
throw SocketException();
} }
// Set some fields in the serverAddress structure. // Set some fields in the serverAddress structure.
@ -189,15 +196,15 @@ public:
// reuse port // reuse port
{ {
int val=1; int val=1;
if (setsockopt(socketDescriptor,SOL_SOCKET,SO_REUSEADDR, if (setsockopt(sockfd.fd,SOL_SOCKET,SO_REUSEADDR,
&val,sizeof(int)) == -1) { &val,sizeof(int)) == -1) {
cprintf(RED, "setsockopt REUSEADDR failed\n"); cprintf(RED, "setsockopt REUSEADDR failed\n");
socketDescriptor=-1; sockfd.fd =-1;
return; throw SocketException();
} }
} }
//increase buffer size if its udp //increase socket buffer size if its udp
if (p == UDP) { if (p == UDP) {
uint32_t desired_size = buf_size; uint32_t desired_size = buf_size;
uint32_t real_size = desired_size * 2; // kernel doubles this value for bookkeeping overhead uint32_t real_size = desired_size * 2; // kernel doubles this value for bookkeeping overhead
@ -205,7 +212,7 @@ public:
socklen_t optlen = sizeof(int); socklen_t optlen = sizeof(int);
// confirm if sufficient // confirm if sufficient
if (getsockopt(socketDescriptor, SOL_SOCKET, SO_RCVBUF, &ret_size, &optlen) == -1) { if (getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF, &ret_size, &optlen) == -1) {
FILE_LOG(logWARNING) << "[Port " << port_number << "] " FILE_LOG(logWARNING) << "[Port " << port_number << "] "
"Could not get rx socket receive buffer size"; "Could not get rx socket receive buffer size";
} else if (ret_size >= real_size) { } else if (ret_size >= real_size) {
@ -219,14 +226,14 @@ public:
// not sufficient, enhance size // not sufficient, enhance size
else { else {
// set buffer size (could not set) // set buffer size (could not set)
if (setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVBUF, if (setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
&desired_size, optlen) == -1) { &desired_size, optlen) == -1) {
FILE_LOG(logWARNING) << "[Port " << port_number << "] " FILE_LOG(logWARNING) << "[Port " << port_number << "] "
"Could not set rx socket buffer size to " "Could not set rx socket buffer size to "
<< desired_size << ". (No Root Privileges?)"; << desired_size << ". (No Root Privileges?)";
} }
// confirm size // confirm size
else if (getsockopt(socketDescriptor, SOL_SOCKET, SO_RCVBUF, else if (getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
&ret_size, &optlen) == -1) { &ret_size, &optlen) == -1) {
FILE_LOG(logWARNING) << "[Port " << port_number << "] " FILE_LOG(logWARNING) << "[Port " << port_number << "] "
"Could not get rx socket buffer size"; "Could not get rx socket buffer size";
@ -241,9 +248,9 @@ public:
actual_udp_socket_buffer_size = ret_size; actual_udp_socket_buffer_size = ret_size;
// force a value larger than system limit // force a value larger than system limit
// (if run in a privileged context (capability CAP_NET_ADMIN set)) // (if run in a privileged context (capability CAP_NET_ADMIN set))
int ret = setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVBUFFORCE, int ret = setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUFFORCE,
&desired_size, optlen); &desired_size, optlen);
getsockopt(socketDescriptor, SOL_SOCKET, SO_RCVBUF, getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
&ret_size, &optlen); &ret_size, &optlen);
if (ret == -1) { if (ret == -1) {
FILE_LOG(logWARNING) << "[Port " << port_number << "] " FILE_LOG(logWARNING) << "[Port " << port_number << "] "
@ -261,15 +268,15 @@ public:
} }
if(bind(socketDescriptor,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){ if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
cprintf(RED, "Can not bind socket\n"); cprintf(RED, "Can not bind socket\n");
socketDescriptor=-1; sockfd.fd =-1;
return; throw SocketException();
} }
if (getProtocol()==SOCK_STREAM) if (getProtocol()==SOCK_STREAM)
listen(socketDescriptor, DEFAULT_BACKLOG); listen(sockfd.fd, DEFAULT_BACKLOG);
} }
@ -277,8 +284,7 @@ public:
* The destructor: disconnects and close the socket * The destructor: disconnects and close the socket
*/ */
~genericSocket() { ~genericSocket() {
Disconnect(); //mySocketDescriptor destructor also gets called
CloseServerTCPSocketDescriptor();
serverAddress.sin_port=-1; serverAddress.sin_port=-1;
}; };
@ -305,13 +311,13 @@ public:
* Get TCP Server File Descriptor * Get TCP Server File Descriptor
* @returns TCP Server file descriptor * @returns TCP Server file descriptor
*/ */
int getFileDes(){return file_des;}; int getFileDes(){return sockfd.newfd;};
/** /**
* Get socket descriptor * Get socket descriptor
* @returns socket descriptor * @returns socket descriptor
*/ */
int getsocketDescriptor(){return socketDescriptor;}; int getsocketDescriptor(){return sockfd.fd;};
/** /**
* Get total bytes sent/received * Get total bytes sent/received
@ -343,21 +349,14 @@ public:
*/ */
int getProtocol() {return getProtocol(protocol);}; int getProtocol() {return getProtocol(protocol);};
/**
* Get error status
* @returns 1 if error
*/
int getErrorStatus(){if (socketDescriptor==-10) return -10;
else if (socketDescriptor<0) return 1; else return 0;};
/** /**
* Close TCP Server socket descriptor * Close TCP Server socket descriptor
*/ */
void CloseServerTCPSocketDescriptor() { void CloseServerTCPSocketDescriptor() {
if (getProtocol() == TCP && is_a_server) { if (protocol == TCP && is_a_server) {
if (socketDescriptor >= 0) { if (sockfd.fd >= 0) {
close(socketDescriptor); close(sockfd.fd);
socketDescriptor = -1; sockfd.fd = -1;
} }
} }
}; };
@ -367,15 +366,15 @@ public:
*/ */
void Disconnect(){ void Disconnect(){
if (protocol == TCP && is_a_server) { if (protocol == TCP && is_a_server) {
if (file_des >= 0) { if (sockfd.newfd >= 0) {
close(file_des); close(sockfd.newfd);
file_des = -1; sockfd.newfd = -1;
} }
return; return;
} }
if (socketDescriptor >= 0) { if (sockfd.fd >= 0) {
close(socketDescriptor); close(sockfd.fd);
socketDescriptor = -1; sockfd.fd = -1;
} }
}; };
@ -385,12 +384,12 @@ public:
*/ */
int Connect(){ int Connect(){
if(file_des>0) return file_des; if(sockfd.newfd>0) return sockfd.newfd;
if (protocol==UDP) return -1; if (protocol==UDP) return -1;
if(is_a_server && protocol==TCP){ //server tcp; the server will wait for the clients connection if(is_a_server && protocol==TCP){ //server tcp; the server will wait for the clients connection
if (socketDescriptor>0) { if (sockfd.fd>0) {
if ((file_des = accept(socketDescriptor,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) { if ((sockfd.newfd = accept(sockfd.fd,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) {
cprintf(RED, "Error: with server accept, connection refused\n"); cprintf(RED, "Error: with server accept, connection refused\n");
switch(errno) { switch(errno) {
case EWOULDBLOCK: case EWOULDBLOCK:
@ -442,27 +441,27 @@ public:
else{ else{
inet_ntop(AF_INET, &(clientAddress.sin_addr), dummyClientIP, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(clientAddress.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
#ifdef VERY_VERBOSE #ifdef VERY_VERBOSE
cout << "client connected "<< file_des << endl; cout << "client connected "<< sockfd.newfd << endl;
#endif #endif
} }
} }
#ifdef VERY_VERBOSE #ifdef VERY_VERBOSE
cout << "fd " << file_des << endl; cout << "fd " << sockfd.newfd << endl;
#endif #endif
return file_des; return sockfd.newfd;
} else { } else {
if (socketDescriptor<=0) if (sockfd.fd<=0)
socketDescriptor = socket(AF_INET, getProtocol(),0); sockfd.fd = socket(AF_INET, getProtocol(),0);
// SetTimeOut(10); // SetTimeOut(10);
if (socketDescriptor < 0){ if (sockfd.fd < 0){
cprintf(RED, "Can not create socket\n"); cprintf(RED, "Can not create socket\n");
} else { } else {
if(connect(socketDescriptor,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){ if(connect(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
cprintf(RED, "Can not connect to socket\n"); cprintf(RED, "Can not connect to socket\n");
return -1; return -1;
} }
} }
return socketDescriptor; return sockfd.fd;
} }
}; };
@ -478,7 +477,7 @@ public:
* Shut down socket * Shut down socket
*/ */
void ShutDownSocket(){ void ShutDownSocket(){
shutdown(socketDescriptor, SHUT_RDWR); shutdown(sockfd.fd, SHUT_RDWR);
Disconnect(); Disconnect();
}; };
@ -494,13 +493,13 @@ public:
struct timeval tout; struct timeval tout;
tout.tv_sec = 0; tout.tv_sec = 0;
tout.tv_usec = 0; tout.tv_usec = 0;
if(::setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVTIMEO,
&tout, sizeof(struct timeval)) <0) { &tout, sizeof(struct timeval)) <0) {
cprintf(RED, "Error in setsockopt SO_RCVTIMEO %d\n", 0); cprintf(RED, "Error in setsockopt SO_RCVTIMEO %d\n", 0);
} }
tout.tv_sec = ts; tout.tv_sec = ts;
tout.tv_usec = 0; tout.tv_usec = 0;
if(::setsockopt(socketDescriptor, SOL_SOCKET, SO_SNDTIMEO, if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_SNDTIMEO,
&tout, sizeof(struct timeval)) < 0) { &tout, sizeof(struct timeval)) < 0) {
cprintf(RED, "Error in setsockopt SO_SNDTIMEO %d\n", ts); cprintf(RED, "Error in setsockopt SO_SNDTIMEO %d\n", ts);
} }
@ -684,11 +683,11 @@ public:
if (buf==NULL) return -1; if (buf==NULL) return -1;
total_sent=0; total_sent=0;
int tcpfd = socketDescriptor; int tcpfd = sockfd.fd;
switch(protocol) { switch(protocol) {
case TCP: case TCP:
tcpfd = (is_a_server ? file_des : socketDescriptor); tcpfd = (is_a_server ? sockfd.newfd : sockfd.fd);
if (tcpfd<0) return -1; if (tcpfd<0) return -1;
while(length>0){ while(length>0){
nsending = (length>packet_size) ? packet_size:length; nsending = (length>packet_size) ? packet_size:length;
@ -713,12 +712,12 @@ public:
break; break;
case UDP: case UDP:
if (socketDescriptor<0) return -1; if (sockfd.fd<0) return -1;
//if length given, listens to length, else listens for packetsize till length is reached //if length given, listens to length, else listens for packetsize till length is reached
if(length){ if(length){
while(length>0){ while(length>0){
nsending = (length>packet_size) ? packet_size:length; nsending = (length>packet_size) ? packet_size:length;
nsent = recvfrom(socketDescriptor,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, &clientAddress_length); nsent = recvfrom(sockfd.fd,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, &clientAddress_length);
if(nsent == header_packet_size) if(nsent == header_packet_size)
continue; continue;
if(nsent != nsending){ if(nsent != nsending){
@ -738,7 +737,7 @@ public:
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
cprintf(BLUE,"%d gonna listen\n", portno); fflush(stdout); cprintf(BLUE,"%d gonna listen\n", portno); fflush(stdout);
#endif #endif
nsent = recvfrom(socketDescriptor,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, &clientAddress_length); 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) //break out of loop only if read one packets size or read didnt work (cuz of shutdown)
if(nsent<=0 || nsent == packet_size) if(nsent<=0 || nsent == packet_size)
break; break;
@ -773,11 +772,11 @@ public:
total_sent=0; total_sent=0;
int tcpfd = socketDescriptor; int tcpfd = sockfd.fd;
switch(protocol) { switch(protocol) {
case TCP: case TCP:
tcpfd = (is_a_server ? file_des : socketDescriptor); tcpfd = (is_a_server ? sockfd.newfd : sockfd.fd);
if (tcpfd<0) return -1; if (tcpfd<0) return -1;
while(length>0){ while(length>0){
nsending = (length>packet_size) ? packet_size:length; nsending = (length>packet_size) ? packet_size:length;
@ -792,10 +791,10 @@ public:
} }
break; break;
case UDP: case UDP:
if (socketDescriptor<0) return -1; if (sockfd.fd<0) return -1;
while(length>0){ while(length>0){
nsending = (length>packet_size) ? packet_size:length; nsending = (length>packet_size) ? packet_size:length;
nsent = sendto(socketDescriptor,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, clientAddress_length); nsent = sendto(sockfd.fd,(char*)buf+total_sent,nsending, 0, (struct sockaddr *) &clientAddress, clientAddress_length);
if(!nsent) break; if(!nsent) break;
length-=nsent; length-=nsent;
total_sent+=nsent; total_sent+=nsent;
@ -819,8 +818,7 @@ protected:
int portno; int portno;
communicationProtocol protocol; communicationProtocol protocol;
int is_a_server; int is_a_server;
int socketDescriptor; mySocketDescriptors sockfd;
int file_des;
int packet_size; int packet_size;
struct sockaddr_in clientAddress, serverAddress; struct sockaddr_in clientAddress, serverAddress;
socklen_t clientAddress_length; socklen_t clientAddress_length;

View File

@ -25,11 +25,11 @@ class slsReceiver : private virtual slsReceiverDefs {
* Constructor * Constructor
* Starts up a Receiver server. Reads configuration file, options, and * Starts up a Receiver server. Reads configuration file, options, and
* assembles a Receiver using TCP and UDP detector interfaces * assembles a Receiver using TCP and UDP detector interfaces
* throws an exception in case of failure
* @param argc from command line * @param argc from command line
* @param argv from command line * @param argv from command line
* @param succecc socket creation was successfull
*/ */
slsReceiver(int argc, char *argv[], int &success); slsReceiver(int argc, char *argv[]);
/** /**
* Destructor * Destructor
@ -99,6 +99,5 @@ class slsReceiver : private virtual slsReceiverDefs {
private: private:
slsReceiverTCPIPInterface* tcpipInterface; slsReceiverTCPIPInterface* tcpipInterface;
UDPInterface* udp_interface;
}; };

View File

@ -26,21 +26,12 @@ class slsReceiverTCPIPInterface : private virtual slsReceiverDefs {
/** /**
* Constructor * Constructor
* reads config file, creates socket, assigns function table * reads config file, creates socket, assigns function table
* @param succecc socket creation was successfull * throws an exception in case of failure to construct
* @param rbase pointer to the receiver base
* @param pn port number (defaults to default port number) * @param pn port number (defaults to default port number)
*/ */
slsReceiverTCPIPInterface(int &success, UDPInterface* rbase, int pn=-1); slsReceiverTCPIPInterface(int pn=-1);
/**
* Sets the port number to listen to.
Take care that the client must know to whcih port it has to listen to, so normally it is better to use a fixes port from the instatiation or change it from the client.
@param pn port number (-1 only get)
\returns actual port number
*/
int setPortNumber(int pn=-1);
/** /**
* Starts listening on the TCP port for client comminication * Starts listening on the TCP port for client comminication
\returns OK or FAIL \returns OK or FAIL

View File

@ -9,19 +9,18 @@
#include <iostream> #include <iostream>
#include <exception> #include <exception>
using namespace std;
struct SharedMemoryException : public exception { struct SharedMemoryException : public std::exception {
public: public:
SharedMemoryException() {} SharedMemoryException() {}
string GetMessage() const { return "Shared Memory Failed";}; std::string GetMessage() const { return "Shared Memory Failed";};
}; };
struct ThreadpoolException : public exception { struct ThreadpoolException : public std::exception {
public: public:
ThreadpoolException() {} ThreadpoolException() {}
string GetMessage() const { return "Threadpool Failed";}; std::string GetMessage() const { return "Threadpool Failed";};
}; };
struct SocketException : public std::exception { struct SocketException : public std::exception {

View File

@ -193,16 +193,17 @@ int Listener::CreateUDPSockets() {
ShutDownUDPSocket(); ShutDownUDPSocket();
udpSocket = new genericSocket(*udpPortNumber, genericSocket::UDP, try{
generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize, genericSocket* g = new genericSocket(*udpPortNumber, genericSocket::UDP,
*udpSocketBufferSize); generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize,
int iret = udpSocket->getErrorStatus(); *udpSocketBufferSize);
if(!iret){ udpSocket = g;
FILE_LOG(logINFO) << index << ": UDP port opened at port " << *udpPortNumber; FILE_LOG(logINFO) << index << ": UDP port opened at port " << *udpPortNumber;
}else{ } catch (...) {
FILE_LOG(logERROR) << "Could not create UDP socket on port " << *udpPortNumber << " error: " << iret; FILE_LOG(logERROR) << "Could not create UDP socket on port " << *udpPortNumber;
return FAIL; return FAIL;
} }
udpSocketAlive = true; udpSocketAlive = true;
sem_init(&semaphore_socket,1,0); sem_init(&semaphore_socket,1,0);
@ -248,17 +249,21 @@ int Listener::CreateDummySocketForUDPSocketBufferSize(uint32_t s) {
if(udpSocket){ if(udpSocket){
udpSocket->ShutDownSocket(); udpSocket->ShutDownSocket();
delete udpSocket; delete udpSocket;
udpSocket = 0;
} }
//create dummy socket //create dummy socket
udpSocket = new genericSocket(*udpPortNumber, genericSocket::UDP, try {
genericSocket* g = new genericSocket(*udpPortNumber, genericSocket::UDP,
generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize, generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize,
*udpSocketBufferSize); *udpSocketBufferSize);
int iret = udpSocket->getErrorStatus(); udpSocket = g;
if (iret){ } catch (...) {
FILE_LOG(logERROR) << "Could not create a test UDP socket on port " << *udpPortNumber << " error: " << iret; FILE_LOG(logERROR) << "Could not create a test UDP socket on port " << *udpPortNumber;
return FAIL; return FAIL;
} }
// doubled due to kernel bookkeeping (could also be less due to permissions) // doubled due to kernel bookkeeping (could also be less due to permissions)
*actualUDPSocketBufferSize = udpSocket->getActualUDPSocketBufferSize(); *actualUDPSocketBufferSize = udpSocket->getActualUDPSocketBufferSize();
if (*actualUDPSocketBufferSize != (s*2)) { if (*actualUDPSocketBufferSize != (s*2)) {

View File

@ -5,8 +5,6 @@
***********************************************/ ***********************************************/
#include "UDPBaseImplementation.h" #include "UDPBaseImplementation.h"
#include "genericSocket.h"
#include "ZmqSocket.h"
#include <sys/stat.h> // stat #include <sys/stat.h> // stat
#include <iostream> #include <iostream>

View File

@ -18,11 +18,8 @@ using namespace std;
slsReceiver::slsReceiver(int argc, char *argv[], int &success): slsReceiver::slsReceiver(int argc, char *argv[]):
tcpipInterface (NULL), tcpipInterface (0) {
udp_interface (NULL)
{
success=OK;
// options // options
map<string, string> configuration_map; map<string, string> configuration_map;
@ -72,8 +69,7 @@ slsReceiver::slsReceiver(int argc, char *argv[], int &success):
tempval = GITREV; tempval = GITREV;
tempval = (tempval <<32) | GITDATE; tempval = (tempval <<32) | GITDATE;
cout << "SLS Receiver " << GITBRANCH << " (0x" << hex << tempval << ")" << endl; cout << "SLS Receiver " << GITBRANCH << " (0x" << hex << tempval << ")" << endl;
success = FAIL; // to exit throw exception();
break;
case 'h': case 'h':
default: default:
@ -87,26 +83,18 @@ slsReceiver::slsReceiver(int argc, char *argv[], int &success):
+ "\t receivers\n\n"; + "\t receivers\n\n";
FILE_LOG(logINFO) << help_message << endl; FILE_LOG(logINFO) << help_message << endl;
break; throw exception();
} }
} }
if( !fname.empty() ){ if( !fname.empty() && read_config_file(fname, &tcpip_port_no, &configuration_map) == FAIL) {
try{ throw exception();
FILE_LOG(logINFO) << "config file name " << fname;
success = read_config_file(fname, &tcpip_port_no, &configuration_map);
//VERBOSE_PRINT("Read configuration file of " + iline + " lines");
}
catch(...){
FILE_LOG(logERROR) << "Coult not open configuration file " << fname ;
success = FAIL;
}
} }
if (success==OK){ // might throw an exception
tcpipInterface = new slsReceiverTCPIPInterface(success, udp_interface, tcpip_port_no); tcpipInterface = new slsReceiverTCPIPInterface(tcpip_port_no);
}
} }
@ -131,40 +119,26 @@ int64_t slsReceiver::getReceiverVersion(){
} }
void slsReceiver::registerCallBackStartAcquisition(int (*func)(char*, char*, uint64_t, uint32_t, void*),void *arg){ void slsReceiver::registerCallBackStartAcquisition(int (*func)(
//tcpipInterface char*, char*, uint64_t, uint32_t, void*),void *arg){
if(udp_interface) tcpipInterface->registerCallBackStartAcquisition(func,arg);
udp_interface->registerCallBackStartAcquisition(func,arg);
else
tcpipInterface->registerCallBackStartAcquisition(func,arg);
} }
void slsReceiver::registerCallBackAcquisitionFinished(void (*func)(uint64_t, void*),void *arg){ void slsReceiver::registerCallBackAcquisitionFinished(
//tcpipInterface void (*func)(uint64_t, void*),void *arg){
if(udp_interface) tcpipInterface->registerCallBackAcquisitionFinished(func,arg);
udp_interface->registerCallBackAcquisitionFinished(func,arg);
else
tcpipInterface->registerCallBackAcquisitionFinished(func,arg);
} }
void slsReceiver::registerCallBackRawDataReady(void (*func)(char*, void slsReceiver::registerCallBackRawDataReady(void (*func)(char*,
char*, uint32_t, void*),void *arg){ char*, uint32_t, void*),void *arg){
//tcpipInterface tcpipInterface->registerCallBackRawDataReady(func,arg);
if(udp_interface)
udp_interface->registerCallBackRawDataReady(func,arg);
else
tcpipInterface->registerCallBackRawDataReady(func,arg);
} }
void slsReceiver::registerCallBackRawDataModifyReady(void (*func)(char*, void slsReceiver::registerCallBackRawDataModifyReady(void (*func)(char*,
char*, uint32_t &, void*),void *arg){ char*, uint32_t &, void*),void *arg){
//tcpipInterface tcpipInterface->registerCallBackRawDataModifyReady(func,arg);
if(udp_interface)
udp_interface->registerCallBackRawDataModifyReady(func,arg);
else
tcpipInterface->registerCallBackRawDataModifyReady(func,arg);
} }

View File

@ -31,16 +31,16 @@ slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
delete receiverBase; delete receiverBase;
} }
slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int &success, UDPInterface* rbase, int pn): slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int pn):
myDetectorType(GOTTHARD), myDetectorType(GOTTHARD),
receiverBase(rbase), receiverBase(0),
ret(OK), ret(OK),
fnum(-1), fnum(-1),
lockStatus(0), lockStatus(0),
killTCPServerThread(0), killTCPServerThread(0),
tcpThreadCreated(false), tcpThreadCreated(false),
portNumber(DEFAULT_PORTNO+2), portNumber(DEFAULT_PORTNO+2),
mySock(NULL) mySock(0)
{ {
//***callback parameters*** //***callback parameters***
startAcquisitionCallBack = NULL; startAcquisitionCallBack = NULL;
@ -51,83 +51,25 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int &success, UDPInterface*
rawDataModifyReadyCallBack = NULL; rawDataModifyReadyCallBack = NULL;
pRawDataReady = NULL; pRawDataReady = NULL;
unsigned short int port_no=portNumber; // create socket
if(receiverBase == NULL) portNumber = (pn > 0 ? pn : DEFAULT_PORTNO + 2);
receiverBase = 0; MySocketTCP* m = new MySocketTCP(portNumber);
mySock = m;
if (pn>0) //initialize variables
port_no = pn; strcpy(mySock->lastClientIP,"none");
strcpy(mySock->thisClientIP,"none1");
memset(mess,0,sizeof(mess));
strcpy(mess,"dummy message");
success=OK; function_table();
//create socket
if(success == OK){
mySock = new MySocketTCP(port_no);
if (mySock->getErrorStatus()) {
success = FAIL;
delete mySock;
mySock=NULL;
} else {
portNumber=port_no;
//initialize variables
strcpy(mySock->lastClientIP,"none");
strcpy(mySock->thisClientIP,"none1");
memset(mess,0,sizeof(mess));
strcpy(mess,"dummy message");
function_table();
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
FILE_LOG(logINFO) << "Function table assigned."; FILE_LOG(logINFO) << "Function table assigned.";
#endif #endif
}
}
} }
int slsReceiverTCPIPInterface::setPortNumber(int pn){
memset(mess, 0, sizeof(mess));
int p_number;
MySocketTCP *oldsocket = NULL;;
int sd = 0;
if (pn > 0) {
p_number = pn;
if (p_number < 1024) {
sprintf(mess,"Too low port number %d\n", p_number);
FILE_LOG(logERROR) << mess;
} else {
oldsocket=mySock;
mySock = new MySocketTCP(p_number);
if(mySock){
sd = mySock->getErrorStatus();
if (!sd){
portNumber=p_number;
strcpy(mySock->lastClientIP,oldsocket->lastClientIP);
delete oldsocket;
} else {
FILE_LOG(logERROR) << "Could not bind port " << p_number;
if (sd == -10) {
FILE_LOG(logINFO) << "Port "<< p_number << " already set";
} else {
delete mySock;
mySock=oldsocket;
}
}
} else {
mySock=oldsocket;
}
}
}
return portNumber;
}
int slsReceiverTCPIPInterface::start(){ int slsReceiverTCPIPInterface::start(){
FILE_LOG(logDEBUG) << "Creating TCP Server Thread"; FILE_LOG(logDEBUG) << "Creating TCP Server Thread";
killTCPServerThread = 0; killTCPServerThread = 0;
@ -566,15 +508,14 @@ int slsReceiverTCPIPInterface::get_last_client_ip() {
int slsReceiverTCPIPInterface::set_port() { int slsReceiverTCPIPInterface::set_port() {
ret = OK; ret = OK;
memset(mess, 0, sizeof(mess)); memset(mess, 0, sizeof(mess));
int unused = 0; int p_type = 0;
int p_number = -1; int p_number = -1;
MySocketTCP* mySocket = NULL; MySocketTCP* mySocket = 0;
char oldLastClientIP[INET_ADDRSTRLEN]; char oldLastClientIP[INET_ADDRSTRLEN];
memset(oldLastClientIP, 0, sizeof(oldLastClientIP)); memset(oldLastClientIP, 0, sizeof(oldLastClientIP));
int sd = -1;
// receive arguments // receive arguments
if (mySock->ReceiveDataOnly(&unused,sizeof(unused)) < 0 ) if (mySock->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 )
return printSocketReadError(); return printSocketReadError();
if (mySock->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 ) if (mySock->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 )
return printSocketReadError(); return printSocketReadError();
@ -586,29 +527,26 @@ int slsReceiverTCPIPInterface::set_port() {
FILE_LOG(logERROR) << mess; FILE_LOG(logERROR) << mess;
} }
else { else {
if (p_number<1024) { if (p_number < 1024) {
ret = FAIL; ret = FAIL;
sprintf(mess,"Port Number (%d) too low\n", p_number); sprintf(mess,"Port Number (%d) too low\n", p_number);
FILE_LOG(logERROR) << mess; FILE_LOG(logERROR) << mess;
} } else {
FILE_LOG(logINFO) << "set port to " << p_number <<endl; FILE_LOG(logINFO) << "set port to " << p_number <<endl;
strcpy(oldLastClientIP, mySock->lastClientIP); strcpy(oldLastClientIP, mySock->lastClientIP);
mySocket = new MySocketTCP(p_number);
if(mySocket){ try {
sd = mySocket->getErrorStatus(); mySocket = new MySocketTCP(p_number);
if (sd < 0) {
ret = FAIL;
sprintf(mess,"Could not bind port %d\n", p_number);
FILE_LOG(logERROR) << mess;
if (sd == -10) {
ret = FAIL;
sprintf(mess,"Port %d already set\n", p_number);
FILE_LOG(logERROR) << mess;
}
}
else
strcpy(mySock->lastClientIP,oldLastClientIP); strcpy(mySock->lastClientIP,oldLastClientIP);
} catch(SamePortSocketException e) {
ret = FAIL;
sprintf(mess, "Could not bind port %d. It is already set\n", p_number);
FILE_LOG(logERROR) << mess;
} catch (...) {
ret = FAIL;
sprintf(mess, "Could not bind port %d.\n", p_number);
FILE_LOG(logERROR) << mess;
}
} }
} }
@ -621,7 +559,7 @@ int slsReceiverTCPIPInterface::set_port() {
mySock->SendDataOnly(mess,sizeof(mess)); mySock->SendDataOnly(mess,sizeof(mess));
else { else {
mySock->SendDataOnly(&p_number,sizeof(p_number)); mySock->SendDataOnly(&p_number,sizeof(p_number));
if(sd>=0){ if(ret != FAIL){
mySock->Disconnect(); mySock->Disconnect();
delete mySock; delete mySock;
mySock = mySocket; mySock = mySocket;

View File

@ -2,11 +2,18 @@
#include "slsReceiver.h" #include "slsReceiver.h"
slsReceiverUsers::slsReceiverUsers(int argc, char *argv[], int &success) { slsReceiverUsers::slsReceiverUsers(int argc, char *argv[], int &success) {
receiver=new slsReceiver(argc, argv, success); // catch the exception here to limit it to within the library (for current version)
try {
slsReceiver* r = new slsReceiver(argc, argv);
receiver = r;
success = slsReceiverDefs::OK;
} catch (...) {
success = slsReceiverDefs::FAIL;
}
} }
slsReceiverUsers::~slsReceiverUsers() { slsReceiverUsers::~slsReceiverUsers() {
delete receiver; delete receiver;
} }
int slsReceiverUsers::start() { int slsReceiverUsers::start() {
@ -28,13 +35,13 @@ void slsReceiverUsers::registerCallBackStartAcquisition(int (*func)(char*, char*
void slsReceiverUsers::registerCallBackAcquisitionFinished(void (*func)(uint64_t, void*),void *arg){ void slsReceiverUsers::registerCallBackAcquisitionFinished(void (*func)(uint64_t, void*),void *arg){
receiver->registerCallBackAcquisitionFinished(func,arg); receiver->registerCallBackAcquisitionFinished(func,arg);
} }
void slsReceiverUsers::registerCallBackRawDataReady(void (*func)(char* header, void slsReceiverUsers::registerCallBackRawDataReady(void (*func)(char* header,
char* datapointer, uint32_t datasize, void*), void *arg){ char* datapointer, uint32_t datasize, void*), void *arg){
receiver->registerCallBackRawDataReady(func,arg); receiver->registerCallBackRawDataReady(func,arg);
} }
void slsReceiverUsers::registerCallBackRawDataModifyReady(void (*func)(char* header, void slsReceiverUsers::registerCallBackRawDataModifyReady(void (*func)(char* header,
char* datapointer, uint32_t& revDatasize, void*), void *arg){ char* datapointer, uint32_t& revDatasize, void*), void *arg){
receiver->registerCallBackRawDataModifyReady(func,arg); receiver->registerCallBackRawDataModifyReady(func,arg);
} }

View File

@ -8,6 +8,7 @@
#include <map> #include <map>
#include "utilities.h" #include "utilities.h"
#include "logger.h"
using namespace std; using namespace std;
@ -21,9 +22,15 @@ int read_config_file(string fname, int *tcpip_port_no, map<string, string> * con
int success = slsReceiverDefs::OK; int success = slsReceiverDefs::OK;
FILE_LOG(logINFO) << "config file name " << fname;
try {
infile.open(fname.c_str(), ios_base::in);
} catch(...) {
FILE_LOG(logERROR) << "Could not open configuration file " << fname ;
success = slsReceiverDefs::FAIL;
}
infile.open(fname.c_str(), ios_base::in); if (success == slsReceiverDefs::OK && infile.is_open()) {
if (infile.is_open()) {
while(infile.good()){ while(infile.good()){
getline(infile,sLine); getline(infile,sLine);
iline++; iline++;