Now disconnects properly without instantly quitting

Sun, 10 Jan 2016 20:04:28 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 10 Jan 2016 20:04:28 +0200
changeset 111
51c93a0cc317
parent 110
cad1163333b9
child 112
c062273efa33

Now disconnects properly without instantly quitting

sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Sun Jan 10 19:38:11 2016 +0200
+++ b/sources/interface.cpp	Sun Jan 10 20:04:28 2016 +0200
@@ -166,7 +166,7 @@
 	OutputLines.clear();
 	OutputLines << ColoredLine();
 	Session.set_interface (this);
-	Title.sprintf (APPNAME " %s (%s)", full_version_string(), changeset_date_string());
+	reset_title();
 
 	if (::has_colors())
 	{
@@ -227,7 +227,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-void Interface::safe_disconnect (std::function<void()> afterwards)
+void Interface::safe_disconnect (std::function<void(bool)> afterwards)
 {
 	if (Session.is_active())
 	{
@@ -235,7 +235,7 @@
 		set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
 	}
 	else
-		afterwards();
+		afterwards(false);
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -637,7 +637,7 @@
 		if (ch == 'y' or ch == 'Y')
 		{
 			Session.disconnect();
-			DisconnectConfirmFunction();
+			DisconnectConfirmFunction(true);
 		}
 		else if (ch == 'n' or ch == 'N')
 			set_input_state (INPUTSTATE_NORMAL);
@@ -659,11 +659,10 @@
 			break;
 
 		case INPUTSTATE_NORMAL:
-			safe_disconnect ([&]()
+			safe_disconnect ([&](bool hadsession)
 			{
-				if (Session.is_active())
+				if (hadsession)
 				{
-					Session.disconnect();
 					set_input_state (INPUTSTATE_NORMAL);
 				}
 				else
@@ -844,7 +843,7 @@
 
 	case 'N' - 'A' + 1: // ^N
 		if (CurrentInputState == INPUTSTATE_NORMAL)
-			safe_disconnect ([&]() {set_input_state (INPUTSTATE_ADDRESS);});
+			safe_disconnect ([&](bool){set_input_state (INPUTSTATE_ADDRESS);});
 		break;
 
 	case '\x1b': // Escape
@@ -980,6 +979,10 @@
 				OutputLines.last().add_char (*cp);
 		}
 
+		// Remove some lines if there's too many of them. 20,000 should be enough, I hope.
+		while (OutputLines.size() > 20000)
+			OutputLines.remove_at(0);
+
 		OutputLines.last().add_char (ch);
 	}
 
@@ -1080,4 +1083,20 @@
 		print_error("Unknown command %s\n", command.chars());
 }
 
+// -------------------------------------------------------------------------------------------------
+//
+void Interface::disconnected()
+{
+	print("Disconnected from %s\n", Session.address().to_string(IPAddress::WITH_PORT).chars());
+	reset_title();
+	render_full();
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void Interface::reset_title()
+{
+	Title.sprintf (APPNAME " %s (%s)", full_version_string(), changeset_date_string());
+}
+
 END_ZFC_NAMESPACE
--- a/sources/interface.h	Sun Jan 10 19:38:11 2016 +0200
+++ b/sources/interface.h	Sun Jan 10 20:04:28 2016 +0200
@@ -60,6 +60,7 @@
 	void tab_complete (const String& part, String complete);
 	RCONSession* get_session() { return &Session; }
 	void handle_command(const String& input);
+	void disconnected();
 
 	void vprint (const char* fmtstr, va_list args);
 	void __cdecl print (const char* fmtstr, ...);
@@ -81,7 +82,7 @@
 	int OutputScroll;
 	String Title;
 	InputState CurrentInputState;
-	std::function<void (void)> DisconnectConfirmFunction;
+	std::function<void(bool)> DisconnectConfirmFunction;
 	IPAddress CurrentAddress;
 	String StatusBarText;
 	StringList PlayerNames;
@@ -89,7 +90,7 @@
 	RCONSession Session;
 
 	void render_titlebar();
-	void safe_disconnect (std::function<void()> afterwards);
+	void safe_disconnect (std::function<void(bool)> afterwards);
 	int render_colorline (int y, int x0, int width, const ColoredLine& line, bool allowWrap);
 	int nicklist_width();
 	void render_output();
@@ -108,6 +109,7 @@
 	void yank (int a, int b);
 	int find_previous_word();
 	int find_next_word();
+	void reset_title();
 };
 
 END_ZFC_NAMESPACE
\ No newline at end of file
--- a/sources/network/rconsession.cpp	Sun Jan 10 19:38:11 2016 +0200
+++ b/sources/network/rconsession.cpp	Sun Jan 10 20:04:28 2016 +0200
@@ -73,8 +73,7 @@
 		Bytestream packet;
 		packet.write_byte (CLRC_DISCONNECT);
 		this->send (packet);
-		m_interface->print ("Disconnected from %s\n", m_address.to_string (IPAddress::WITH_PORT).chars());
-		m_interface->update_statusbar();
+		m_interface->disconnected();
 	}
 
 	m_state = RCON_DISCONNECTED;

mercurial