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[]){
qClient *cl =new qClient(argv[1]);
qClient* cl = 0;
try {
qClient *c = new qClient(argv[1]);
cl = c;
} catch(...) {
return 0;
}
cl->executeLine(argc-2, argv+2);
delete cl;
return 0;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
qClient::qClient(char* hostname){
//create socket
mySocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO);
if (mySocket->getErrorStatus()){
cout << "Error: could not connect to host:" << hostname << " with port " << DEFAULT_GUI_PORTNO << endl;
delete mySocket;
exit(-1);
}
qClient::qClient(char* hostname):
mySocket(0),
myStopSocket(0){
//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")
stopAcquisition();
else{
cout << "Error: could not parse arguments: " << argument << endl;
cprintf(RED,"Error: could not parse arguments: %s\n", argument.c_str());
printCommands();
return FAIL;
}

View File

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