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;
}