sources/main.cpp

changeset 27
089e37c0887e
parent 24
e651d02802c0
child 30
21fba5183768
--- a/sources/main.cpp	Sun Dec 14 23:41:00 2014 +0200
+++ b/sources/main.cpp	Mon Dec 15 01:41:06 2014 +0200
@@ -35,6 +35,8 @@
 #include "huffman/huffman.h"
 #include "interface.h"
 
+static bool g_shouldExit = false;
+
 // -------------------------------------------------------------------------------------------------
 //
 FUNCTION
@@ -43,35 +45,53 @@
 	HUFFMAN_Construct();
 	Interface::initialize();
 
-	for (;;)
+	try
 	{
-		fd_set fdset;
-		int highest = 0;
-		timeval timeout;
-		timeout.tv_sec = 0;
-		timeout.tv_usec = 250000; // 0.25 seconds
-		FD_ZERO (&fdset);
-		FD_SET (0, &fdset);
-		RCONSession* session = RCONSession::get_session();
-
-		if (session)
+		for (;;)
 		{
-			int fd = session->socket()->file_descriptor();
-			highest = max (highest, fd);
-			FD_SET (fd, &fdset);
-		}
+			if (g_shouldExit)
+				break;
 
-		select (highest + 1, &fdset, nullptr, nullptr, &timeout);
+			fd_set fdset;
+			int highest = 0;
+			timeval timeout;
+			timeout.tv_sec = 0;
+			timeout.tv_usec = 250000; // 0.25 seconds
+			FD_ZERO (&fdset);
+			FD_SET (0, &fdset);
+			RCONSession* session = RCONSession::get_session();
 
-		if (FD_ISSET (0, &fdset))
-			// stdin is ready, what's incoming?
-			Interface::handle_input();
+			if (session)
+			{
+				int fd = session->socket()->file_descriptor();
+				highest = max (highest, fd);
+				FD_SET (fd, &fdset);
+			}
+
+			select (highest + 1, &fdset, nullptr, nullptr, &timeout);
 
-		if (session)
-			session->tick();
+			if (FD_ISSET (0, &fdset))
+				// stdin is ready, what's incoming?
+				Interface::handle_input();
+
+			if (session)
+				session->tick();
 
-		Interface::render();
+			Interface::render();
+		}
 	}
+	catch (const Exitception&) {}
+
+	if (RCONSession::get_session())
+		RCONSession::get_session()->disconnect();
 
 	return EXIT_SUCCESS;
 }
+
+// -------------------------------------------------------------------------------------------------
+//
+FUNCTION
+request_exit() -> void
+{
+	g_shouldExit = true;
+}

mercurial