- rcon sessions are no longer allocated on the heap

Tue, 16 Dec 2014 02:48:18 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 16 Dec 2014 02:48:18 +0200
changeset 58
d175243ad169
parent 57
a2f41245e387
child 59
00a084f8ed26

- rcon sessions are no longer allocated on the heap

sources/interface.cpp file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
sources/network/bytestream.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Tue Dec 16 02:30:31 2014 +0200
+++ b/sources/interface.cpp	Tue Dec 16 02:48:18 2014 +0200
@@ -830,7 +830,8 @@
 		case INPUTSTATE_PASSWORD:
 			if (g_inputState == INPUTSTATE_PASSWORD and not current_input().is_empty())
 			{
-				RCONSession* session = RCONSession::new_session();
+				RCONSession* session = RCONSession::get_session();
+				session->disconnect();
 				session->set_password (current_input());
 				session->connect (g_address);
 				set_input_state (INPUTSTATE_NORMAL);
@@ -963,7 +964,8 @@
 	if (g_address.port == 0)
 		g_address.port = 10666;
 
-	RCONSession* session = RCONSession::new_session();
+	RCONSession* session = RCONSession::get_session();
+	session->disconnect();
 	session->set_password (password);
 	session->connect (g_address);
 }
--- a/sources/main.cpp	Tue Dec 16 02:30:31 2014 +0200
+++ b/sources/main.cpp	Tue Dec 16 02:48:18 2014 +0200
@@ -49,7 +49,6 @@
 	}
 
 	Interface::initialize();
-	RCONSession::new_session();
 
 	if (argc == 3)
 		Interface::connect (argv[1], argv[2]);
--- a/sources/network/bytestream.cpp	Tue Dec 16 02:30:31 2014 +0200
+++ b/sources/network/bytestream.cpp	Tue Dec 16 02:48:18 2014 +0200
@@ -68,7 +68,7 @@
 //
 Bytestream::~Bytestream()
 {
-	delete m_data;
+	delete[] m_data;
 }
 
 // -------------------------------------------------------------------------------------------------
--- a/sources/network/rconsession.cpp	Tue Dec 16 02:30:31 2014 +0200
+++ b/sources/network/rconsession.cpp	Tue Dec 16 02:48:18 2014 +0200
@@ -1,48 +1,24 @@
 #include "rconsession.h"
 #include "../interface.h"
 
-RCONSession* g_rconSession = nullptr;
-
 // -------------------------------------------------------------------------------------------------
 //
 RCONSession::RCONSession() :
 	m_state (RCON_DISCONNECTED),
 	m_lastPing (0),
-	m_numAdmins (0) {}
-
-// -------------------------------------------------------------------------------------------------
-//
-STATIC METHOD
-RCONSession::new_session() -> RCONSession*
+	m_numAdmins (0)
 {
-	if (g_rconSession != NULL)
+	if (not m_socket.set_blocking (false))
 	{
-		g_rconSession->disconnect();
-		delete g_rconSession;
+		print_to (stderr, "unable to set socket as non-blocking: %s\n",
+			m_socket.error_string().chars());
+		exit (EXIT_FAILURE);
 	}
-
-	g_rconSession = new RCONSession;
-
-	if (not g_rconSession->socket()->set_blocking (false))
-	{
-		print ("unable to set socket as non-blocking: %s\n",
-			g_rconSession->socket()->error_string().chars());
-		delete g_rconSession;
-		return nullptr;
-	}
-
-	return g_rconSession;
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-RCONSession::~RCONSession()
-{
-	disconnect();
-
-	if (g_rconSession == this)
-		g_rconSession = nullptr;
-}
+RCONSession::~RCONSession() {}
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -279,10 +255,8 @@
 STATIC METHOD
 RCONSession::get_session() -> RCONSession*
 {
-	if (g_rconSession == nullptr)
-		new_session();
-
-	return g_rconSession;
+	static RCONSession session;
+	return &session;
 }
 
 // -------------------------------------------------------------------------------------------------
--- a/sources/network/rconsession.h	Tue Dec 16 02:30:31 2014 +0200
+++ b/sources/network/rconsession.h	Tue Dec 16 02:48:18 2014 +0200
@@ -108,7 +108,6 @@
 	METHOD level() const -> const String&;
 	METHOD is_active() const -> bool;
 
-	static METHOD new_session() -> RCONSession*;
 	static METHOD get_session() -> RCONSession*;
 
 private:

mercurial