bjoern avatar bjoern committed 4ca82e5

added option to split overflowing packets to bytewise udp

Comments (0)

Files changed (3)

 
 There are three different UDP libraries included:
 
-UdpBytewise mimics byte-by-byte reading and writing of packets as used in the Serial and Wire libraries of Arduino. This library has a small footprint and offers continuity with existing programming models, but it has some limitations as packet boundaries are ignored by the read function, and packets may have to get split by the write function.
+UdpBytewise mimics byte-by-byte reading and writing of packets as used in the Serial and Wire libraries of Arduino. This library has a small footprint and offers continuity with existing programming models, but it has some limitations as packet boundaries are ignored by the read function. For writing, when the maximum packet length is exceeded, users can choose to either truncate the packet or auto-split into multiple packets.
 
 UdpString uses Tom Igoe/Hernando Barragan's WString library for packets. This library offer high-level abstractions that preserve packet boundaries. However, it is currently rather large and does not leave much space for the user's sketch. The package contains a redistribution of WString for this library.
 

libraries/Ethernet/UdpBytewise.cpp

 	_txIndex =0;
 	_rxIndex =0;
 	_rxSize = 0;
+	_txOverflowStrategy = UDP_TX_OVERFLOW_SPLIT;	
 	socket(_sock,Sn_MR_UDP,_port,0);
 }
 
 
 
 /* Add a byte to the currently assembled packet if there is space
- * TODO: how do we indicate that we can't add to full buffer?
+ * if there isn't space, either truncate (ignore) or split the packet.
  */
 void UdpBytewiseClass::write(uint8_t b) {
-	if(_txIndex>= UDP_TX_PACKET_MAX_SIZE)
-		return;		
+	if(_txIndex>= UDP_TX_PACKET_MAX_SIZE) {
+		//buffer is full - we can either truncate the packet or split in two
+		switch (_txOverflowStrategy) {
+			case UDP_TX_OVERFLOW_SPLIT:
+				endPacket();
+				beginPacket(_txIp,_txPort);
+				//fall through to normal add of byte to buffer below
+				break;
+			case UDP_TX_OVERFLOW_TRUNCATE:
+			default:
+				//don't add - just ignore bytes past buffer size
+				return;
+		}
+	}
 	_txBuffer[_txIndex++] = b;
 }
 
 	return _rxPort;
 }
 
+/* what should we do when we try to add to a full outgoing packet? 
+ * UDP_TX_OVERFLOW_TRUNCATE - throw overflow bytes away
+ * UDP_TX_OVERFLOW_SPLIT - split into multiple packets
+ */
+void UdpBytewiseClass::setOverflowStrategy(uint8_t strategy) {
+	_txOverflowStrategy = strategy;
+}
+
 /* Create one global object */
 UdpBytewiseClass UdpBytewise;

libraries/Ethernet/UdpBytewise.h

 #define UDP_TX_PACKET_MAX_SIZE 32
 #define UDP_RX_PACKET_MAX_SIZE 32
 
+#define UDP_TX_OVERFLOW_TRUNCATE 0
+#define UDP_TX_OVERFLOW_SPLIT 1
+
 class UdpBytewiseClass: public Print {
 private:
 	uint8_t _sock;  // socket ID for Wiz5100
 	uint8_t _txIndex;
 	uint8_t _txIp[4];
 	uint16_t _txPort;
+	uint8_t _txOverflowStrategy; 
 	
 	uint8_t _rxBuffer[UDP_RX_PACKET_MAX_SIZE];
 	uint8_t _rxIndex;
 	
 	void getSenderIp(uint8_t * ip);  //get remote IP of the packet we're currently reading from
 	unsigned int getSenderPort();  //get remote port# of the packet we're currently reading from
-	
+	void setOverflowStrategy(uint8_t); // what to do when our packet is full and we try to add more?
 };
 
 extern UdpBytewiseClass UdpBytewise;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.