Procházet zdrojové kódy

try to quit ssl more cleanly, make network thread die graciously if the tcp connection was closed from the other side and no data was left

Missingmew před 4 roky
rodič
revize
ade4e6057e
1 změnil soubory, kde provedl 25 přidání a 9 odebrání
  1. 25 9
      cli/src/ioman.cpp

+ 25 - 9
cli/src/ioman.cpp

@@ -183,12 +183,12 @@ bool IoMan::connect() {
 		}
 		delete ep;
 	}
-	if (usessl) {
+	if (connected && usessl) {
 		// try to do ssl handshake
 		printMessage(string(__PRETTY_FUNCTION__) + string(" doing ssl handshake with ") + ipstring, debug);
 		sslsock->handshake(boost::asio::ssl::stream_base::client, errcode);
 		if (errcode) {
-			root["error"] = errcode.message();
+			root["error"] = string("couldnt connect via ssl: ") + errcode.message();
 			connected = false;
 			disconnect();
 		} else {
@@ -203,13 +203,22 @@ bool IoMan::connect() {
 
 void IoMan::disconnect() {
 	printMessage("IoMan::disconnect()", debug);
+
+	if (connected) {
+		connected = false;
+		runnetwork = false;
+	}
+
+	if (usessl)
+		sslsock->shutdown(errcode);
+	if (errcode)
+		printMessage(string(__PRETTY_FUNCTION__) + string("ssl shutdown says ") + errcode.message(), debug);
 	tcpsock->shutdown(tcp::socket::shutdown_both, errcode);
 	if (errcode)
 		printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), debug);
 	tcpsock->close(errcode);
 	if (errcode)
-		printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), debug);
-	connected = false;
+		printMessage(string(__PRETTY_FUNCTION__) + string("tcp close says ") + errcode.message(), debug);
 	cmdman.stateSetDisconnected();
 }
 
@@ -264,13 +273,18 @@ void IoMan::networkMain() {
 		// printMessage(string("have ") + std::to_string(toprocess.size()) +
 		// string(" commands"), debug);
 
+		if (readsize < 1) {
+			printMessage(string(__PRETTY_FUNCTION__) + string(" no read size stopping network"), debug);
+			networkmutex.lock();
+			runnetwork = false;
+			networkmutex.unlock();
+			break;
+		}
+
 		timestampmutex.lock();
 		sendtimestampValid = false;
 		timestampmutex.unlock();
 
-		if (readsize < 1) {
-			break;
-		}
 		if (errcode && errcode != boost::asio::error::eof) {
 			printMessage("IoMan::networkMain() couldnt read json data\n" + errcode.message(), debug);
 			continue;
@@ -588,9 +602,11 @@ void IoMan::run() {
 		mainmutex.unlock();
 
 		timestampmutex.lock();
-		if (sendtimestampValid && std::difftime(time(NULL), sendtimestamp) > 15) {
-			// answer took more than 15 seconds
+		if (sendtimestampValid && (!runnetwork || std::difftime(time(NULL), sendtimestamp) > 15)) {
+			// answer took more than 15 seconds or network thread stopped
+			runnetwork = false;
 			disconnect();
+			tnetwork.join();
 			// cmdman gets informed inside disconnect method
 			// inform the user by giving output
 			Json::Value root;