From 605831db932b7b6594525757f14a65a2e6ae2711 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Tue, 13 Jul 2021 13:52:31 +0200
Subject: [PATCH] added message-legnth-limit (cut Topic or Payload)

---
 keywords.txt        |  1 -
 src/didacticNet.cpp | 62 +++------------------------------------------
 2 files changed, 3 insertions(+), 60 deletions(-)

diff --git a/keywords.txt b/keywords.txt
index 1342edf..60c736c 100644
--- a/keywords.txt
+++ b/keywords.txt
@@ -5,7 +5,6 @@
 #######################################
 # Datatypes (KEYWORD1)
 #######################################
-
 didacticPSNet		KEYWORD1
 didacticPSNetBroker	KEYWORD1
 didacticPSNetClient	KEYWORD1
diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp
index ddc2d7b..0df22f5 100644
--- a/src/didacticNet.cpp
+++ b/src/didacticNet.cpp
@@ -150,8 +150,7 @@ int didacticPSNet::extractData(int startCounter, int maxLength, char* buffer, ch
 		buffer[counter-startCounter] = _readBufferMessage[counter];
 		counter++;
 		if((counter-startCounter) > maxLength){
-		//return -1;
-      Serial.print("\nextractData - Return on MAX length\n");
+      counter--;
       break; //if > maxLenght -> leave while and return
 		}
 	}
@@ -232,24 +231,6 @@ int didacticPSNetClient::publish(char* topic,  char* payload){
   for(int i = 0; i < payloadLength; i++){
 			_sendBufferMessage[2+topicLength+1+i] = payload[i];
 		}
-/*
-	if(topicLength <= MAX_LEN_TOPICS){
-		for(int i = 0; i < topicLength; i++){
-			_sendBufferMessage[2+i] = topic[i];
-		}
-	}else { //cut topic if longer than max
-    _dataToSend = false;
-		return false;
-	}
-	if(payloadLength <= MAX_LEN_PAYLOAD){
-		for(int i = 0; i < payloadLength; i++){
-			_sendBufferMessage[2+topicLength+1+i] = payload[i];
-		}
-	}else { //cut message if longer than max
-    _dataToSend = false;
-		return false;
-	}
-  */
 
   _dataToSend = true;
 	return error;
@@ -264,7 +245,7 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i
 	_sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER;
 	_sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0';
 
-	  //TODO: check
+	//TODO: check
   if(topicLength > MAX_LEN_TOPICS){
     topicLength = MAX_LEN_TOPICS;
     error += DN_ERROR_TOPIC_LEN;
@@ -280,24 +261,7 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i
   for(int i = 0; i < payloadLength; i++){
 			_sendBufferMessage[2+topicLength+1+i] = payload[i];
 		}
-/*
-	if(topicLength <= MAX_LEN_TOPICS){
-		for(int i = 0; i < topicLength; i++){
-			_sendBufferMessage[2+i] = topic[i];
-		}
-	}else { //cut topic if longer than max
-    _dataToSend = false;
-		return false;
-	}
-	if(payloadLength <= MAX_LEN_PAYLOAD){
-		for(int i = 0; i < payloadLength; i++){
-			_sendBufferMessage[2+topicLength+1+i] = payload[i];
-		}
-	}else { //cut message if longer than max
-    _dataToSend = false;
-		return false;
-	}
-  */
+
   _dataToSend = true;
 	return error;
 }
@@ -323,8 +287,6 @@ int didacticPSNetClient::subscribe(char* topic){
     if(topicNumber < 0){
       topicNumber = 0;
     }
-
-  //  if( topicLength <= MAX_LEN_TOPICS){
   		for(int i = 0; i < topicLength; i++){
   			_topic[topicNumber][i] = topic[i];
         _sendBufferMessage[2+i]= topic[i];
@@ -332,21 +294,13 @@ int didacticPSNetClient::subscribe(char* topic){
       _topic[topicNumber][topicLength] = '\0';
       _sendBufferMessage[2+topicLength+1]= '\0';
       _dataToSend = true;
-  /*	}
-  	else {
-      _dataToSend = false;
-  		return false;
-  	}*/
-
   }
   else{
-    //if( topicLength <= MAX_LEN_TOPICS){
   		for(int i = 0; i < topicLength; i++){
         _sendBufferMessage[2+i]= topic[i];
   		}
       _sendBufferMessage[2+topicLength+1]= '\0';
       _dataToSend = true;
-  	//}
   }
   while(_dataToSend){
     handleNetwork();
@@ -373,8 +327,6 @@ int didacticPSNetClient::subscribe(char* topic, int topicLength){
     if(topicNumber < 0){
       topicNumber = 0;
     }
-
-  //  if( topicLength <= MAX_LEN_TOPICS){
   		for(int i = 0; i < topicLength; i++){
   			_topic[topicNumber][i] = topic[i];
         _sendBufferMessage[2+i]= topic[i];
@@ -382,21 +334,13 @@ int didacticPSNetClient::subscribe(char* topic, int topicLength){
       _topic[topicNumber][topicLength] = '\0';
       _sendBufferMessage[2+topicLength+1]= '\0';
       _dataToSend = true;
-  /*	}
-  	else {
-      _dataToSend = false;
-  		return false;
-  	}*/
-
   }
   else{
-    //if( topicLength <= MAX_LEN_TOPICS){
   		for(int i = 0; i < topicLength; i++){
         _sendBufferMessage[2+i]= topic[i];
   		}
       _sendBufferMessage[2+topicLength+1]= '\0';
       _dataToSend = true;
-  	//}
   }
   while(_dataToSend){
     handleNetwork();
-- 
GitLab