Source

arduino_osc / libraries / Ethernet / UdpBytewise.cpp

Diff from to

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;