@@ -52,6 +52,8 @@ void NetSIO::begin(std::string host, int port, int baud)
5252 end ();
5353 }
5454
55+ discard_timeout_ms = 0 ;
56+
5557 _baud = baud;
5658 _resume_time = 0 ;
5759 setHost (host, port);
@@ -565,28 +567,61 @@ void NetSIO::updateFIFO()
565567 return ;
566568}
567569
570+ void NetSIO::handle_write_sync (uint8_t c)
571+ {
572+ // handle pending sync request
573+ if (_sync_write_size < 0 )
574+ {
575+ // SYNC RESPONSE
576+ // send byte (should be ACK/NAK) bundled in sync response
577+ sendSyncResponse (NETSIO_ACK_SYNC, c, 0 );
578+ return ;
579+ }
580+ else
581+ {
582+ // sio_late_ack() was not from whatever reason followed by bus_to_peripheral()
583+ Debug_println (" Warn: NetSIO late ACK without bus_to_peripheral" );
584+ // send late ACK byte
585+ sendSyncResponse (NETSIO_ACK_SYNC, _sync_ack_byte, _sync_write_size);
586+ }
587+
588+ return ;
589+ }
590+
568591size_t NetSIO::dataOut (const void *buffer, size_t size)
569592{
570593 int result;
571594 int to_send;
572595 int txbytes = 0 ;
573596 uint8_t txbuf[513 ];
597+ uint8_t *ptr = (uint8_t *) buffer;
574598
575599 if (!_initialized)
576600 return 0 ;
577601
578- while (txbytes < size)
602+ if (size && _sync_request_num >= 0 )
603+ {
604+ handle_write_sync (ptr[0 ]);
605+ ptr++;
606+ size--;
607+ }
608+
609+ while (size)
579610 {
580611 // send block
581- to_send = (( size-txbytes) > sizeof (txbuf)- 1 ) ? sizeof (txbuf)- 1 : (size-txbytes );
612+ to_send = std::min ( size, sizeof (txbuf) - 1 );
582613 txbuf[0 ] = NETSIO_DATA_BLOCK;
583- memcpy (txbuf+1 , (( uint8_t *)buffer) +txbytes, to_send);
614+ memcpy (txbuf+1 , ptr +txbytes, to_send);
584615 // ? calculate credit based on amount of data ?
585616 if (!wait_for_credit (1 ))
586617 break ;
587618 result = write_sock (txbuf, to_send+1 );
588619 if (result > 0 )
589- txbytes += result-1 ;
620+ {
621+ result--;
622+ txbytes += result;
623+ size -= result;
624+ }
590625 else if (result < 0 )
591626 break ;
592627 }
0 commit comments