refactor

Fri, 05 Feb 2021 11:36:38 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 05 Feb 2021 11:36:38 +0200
changeset 200
3fb775db4829
parent 198
54d64e5a4204
child 201
1bfa1cdffb02

refactor

sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/network/rconsession.cpp	Thu Jan 28 11:39:30 2021 +0200
+++ b/sources/network/rconsession.cpp	Fri Feb 05 11:36:38 2021 +0200
@@ -58,10 +58,10 @@
 //
 void RCONSession::connect(net::ip_address address)
 {
-	m_address = address;
-	m_state = RCON_CONNECTING;
-	m_interface->updateStatusBar();
-	sendHello();
+	this->m_address = address;
+	this->m_state = RCON_CONNECTING;
+	this->m_interface->updateStatusBar();
+	this->tick();
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -98,24 +98,37 @@
 	if (m_state == RCON_DISCONNECTED)
 		return;
 
-	time_t now;
-	time(&now);
+	const std::time_t now = std::time(nullptr);
 
 	if (m_lastPing < now)
 	{
-		if (m_state == RCON_CONNECTING)
-		{
-			sendHello();
-		}
-		else if (m_state == RCON_AUTHENTICATING)
+		// This code block is reached every second
+		switch (m_state)
 		{
-			sendPassword();
+		case RCON_CONNECTING:
+			m_interface->print("Connecting to %s...\n", net::ip_address_to_string(m_address).data());
+			this->send({CLRC_BEGINCONNECTION, RCON_PROTOCOL_VERSION});
+			break;
+		case RCON_AUTHENTICATING:
+			m_interface->print("Authenticating...\n");
+			{
+				std::vector<unsigned char> message;
+				Bytestream stream(message);
+				stream.writeByte(CLRC_PASSWORD);
+				stream.writeString(md5((m_salt + m_password).data()));
+				this->send(message);
+			}
+			break;
+		case RCON_CONNECTED:
+			if (this->m_lastPing + 5 < now)
+			{
+				this->send({CLRC_PONG});
+			}
+			break;
+		case RCON_DISCONNECTED:
+			break;
 		}
-		else if (m_state == RCON_CONNECTED and m_lastPing + 5 < now)
-		{
-			send({CLRC_PONG});
-			bumpLastPing();
-		}
+		this->m_lastPing = now;
 	}
 
 	// Check for new packets in our socket
@@ -160,7 +173,6 @@
 			case SVRC_SALT:
 				m_salt = stream.readString();
 				m_state = RCON_AUTHENTICATING;
-				sendPassword();
 				break;
 
 			case SVRC_INVALIDPASSWORD:
@@ -288,28 +300,6 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-void RCONSession::sendHello()
-{
-	m_interface->print("Connecting to %s...\n", net::ip_address_to_string(m_address).data());
-	send({CLRC_BEGINCONNECTION, RCON_PROTOCOL_VERSION});
-	bumpLastPing();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-void RCONSession::sendPassword()
-{
-	m_interface->print("Authenticating...\n");
-	std::vector<unsigned char> message;
-	Bytestream stream(message);
-	stream.writeByte(CLRC_PASSWORD);
-	stream.writeString(md5((m_salt + m_password).data()));
-	send(message);
-	bumpLastPing();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
 void RCONSession::setPassword(const std::string& password)
 {
 	m_password = password;
@@ -344,7 +334,6 @@
 	stream.writeByte(CLRC_COMMAND);
 	stream.writeString(commandString);
 	send(message);
-	bumpLastPing();
 	return true;
 }
 
@@ -387,7 +376,6 @@
 		stream.writeByte(CLRC_TABCOMPLETE);
 		stream.writeString(part);
 		send(message);
-		bumpLastPing();
 		m_lastTabComplete = part;
 	}
 	else
--- a/sources/network/rconsession.h	Thu Jan 28 11:39:30 2021 +0200
+++ b/sources/network/rconsession.h	Fri Feb 05 11:36:38 2021 +0200
@@ -119,7 +119,7 @@
 	RCONSessionState m_state;
 	net::ip_address m_address;
 	net::UDPSocket m_socket;
-	time_t m_lastPing;
+	std::time_t m_lastPing;
 	std::string m_password;
 	std::string m_salt;
 	int m_serverProtocol;

mercurial