From 18270fecce0c6396648fc4c642f46e60c6f87c70 Mon Sep 17 00:00:00 2001
From: Alessio Igor Bogani <alessioigorbogani@gmail.com>
Date: Wed, 7 Feb 2024 08:59:00 +0100
Subject: [PATCH] Add a check for port number and consolidate ones for timeout

---
 src/Socket2.cpp | 49 ++++++++++++-------------------------------------
 src/Socket2.h   |  2 ++
 2 files changed, 14 insertions(+), 37 deletions(-)

diff --git a/src/Socket2.cpp b/src/Socket2.cpp
index 08b97be..6c535ba 100644
--- a/src/Socket2.cpp
+++ b/src/Socket2.cpp
@@ -316,9 +316,7 @@ void Socket2::get_device_property()
 	{
 		proto = UDP;
 		DEBUG_STREAM << "Using UDP protocol" << endl;
-	}
-	else
-	{
+	} else {
 		proto = TCP;
 		DEBUG_STREAM << "Using TCP protocol" << endl;
 	}
@@ -328,13 +326,13 @@ void Socket2::get_device_property()
 	{
 		multiplexing = SLEEP;
 		DEBUG_STREAM << "Using sleep IO multiplexing type" << endl;
-	}
-	else
-	{
+	}	else {
 		multiplexing = SELECT;
 		DEBUG_STREAM << "Using select IO multiplexing type" << endl;
 	}
 
+	if (port > 0xFFFF)
+		init_error = "Invalit port number";
 	/*----- PROTECTED REGION END -----*/	//	Socket2::get_device_property_after
 }
 //--------------------------------------------------------
@@ -388,9 +386,16 @@ void Socket2::always_executed_hook()
 	{
 		set_state(Tango::FAULT);
 		set_status(init_error);
+		return;
 	}
-	else
+
+	tout.tv_sec = timeout / 1000;
+	tout.tv_usec = timeout % 1000 * 1000;
+	if ( ! timerisset( &tout ) )
 	{
+		set_state(Tango::FAULT);
+		set_status("Invalid timeout");
+	} else {
 		check_connection( );
 	}
 
@@ -501,16 +506,6 @@ void Socket2::write(const Tango::DevVarCharArray *argin)
 	/*----- PROTECTED REGION ID(Socket2::write) ENABLED START -----*/
 	check_init();
 	
-	timeval tout;
-	tout.tv_sec = timeout / 1000;
-	tout.tv_usec = timeout % 1000 * 1000;
-	if ( ! timerisset( &tout ) )
-	{
-		Tango::Except::throw_exception( "",
-				"Invalid timeout",
-				"Socket2::write()");
-	}
-
 	char *argin_data = new char[ argin->length() ];
 	for( unsigned int i=0; i<argin->length(); ++i )
 	{
@@ -622,16 +617,6 @@ Tango::DevVarCharArray *Socket2::read(Tango::DevLong argin)
 	/*----- PROTECTED REGION ID(Socket2::read) ENABLED START -----*/
 	check_init();
 
-	timeval tout;
-	tout.tv_sec = timeout / 1000;
-	tout.tv_usec = timeout % 1000 * 1000;
-	if ( ! timerisset( &tout ) )
-	{
-		Tango::Except::throw_exception( "",
-				"Invalid timeout",
-				"Socket2::read()");
-	}
-
 	if (argin < 0)
 	{
 		Tango::Except::throw_exception("",
@@ -677,16 +662,6 @@ Tango::DevVarCharArray *Socket2::read_until(const Tango::DevVarCharArray *argin)
 	/*----- PROTECTED REGION ID(Socket2::read_until) ENABLED START -----*/
 	check_init();
 
-	timeval tout;
-	tout.tv_sec = timeout / 1000;
-	tout.tv_usec = timeout % 1000 * 1000;
-	if ( ! timerisset( &tout ) )
-	{
-		Tango::Except::throw_exception( "",
-				"Invalid timeout",
-				"Socket2::read_until()");
-	}
-
 	if (argin->length() != 1)
 	{
 		Tango::Except::throw_exception("",
diff --git a/src/Socket2.h b/src/Socket2.h
index db8db7e..569a9ac 100644
--- a/src/Socket2.h
+++ b/src/Socket2.h
@@ -97,6 +97,8 @@ class Socket2 : public TANGO_BASE_CLASS
 
 	enum { SLEEP, SELECT } multiplexing;
 
+	timeval tout;
+
 /*----- PROTECTED REGION END -----*/	//	Socket2::Data Members
 
 //	Device property data members
-- 
GitLab