Skip to content

Commit

Permalink
Better error handling for network_read and network_read_nb
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Aug 31, 2024
1 parent 971197a commit c751591
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

## [4.5.3] - 2024-08-32

- network_read and network_read_nb will exit if there is a general error.
network_read will set fn_bytes_read to the bytes read into the buffer so far, for client to decide what to do.

## [4.5.2] - 2024-08-25

- [atari] fuji_read_appkey no longer uses malloc, but requires the data buffer passed in to be at least 2 bytes larger than the keysize to work.
Expand Down
14 changes: 12 additions & 2 deletions common/src/fn_network/network_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ int16_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing.
#endif

// check if the status failed
if (r != 0) return -r;
// check if the status failed. The buffer may be partially filled, up to client if they want to use any of it. The count is in fn_bytes_read
if (r != 0) {
fn_bytes_read = total_read;
// r is the FN_ERR code
return -r;
}

// EOF hit, exit reading
if (fn_network_error == 136) break;

// is there another error? The buffer may be partially filled, up to client if they want to use any of it. The count is in fn_bytes_read
if (fn_network_error != 1) {
fn_bytes_read = total_read;
return -FN_ERR_IO_ERROR;
}

// we are waiting for bytes to become available while still connected.
// Causes tight loop if there's a long delay reading from network into FN, so we may see lots of status requests
if (fn_network_bw == 0 && fn_network_conn == 1) {
Expand Down
11 changes: 9 additions & 2 deletions common/src/fn_network/network_read_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,19 @@ int16_t network_read_nb(char *devicespec, uint8_t *buf, uint16_t len)
r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing.
#endif

// check if the status failed
if (r != 0) return -r;
// check if the status failed.
if (r != 0) {
return -r;
}

// EOF hit, exit reading
if (fn_network_error == 136) return 0;

// is there another error?
if (fn_network_error != 1) {
return -fn_network_error;
}

// we are waiting for bytes to become available while still connected, so no data can be read
if (fn_network_bw == 0 && fn_network_conn == 1) {
return 0;
Expand Down
8 changes: 4 additions & 4 deletions fujinet-network.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ uint8_t network_open(char* devicespec, uint8_t mode, uint8_t trans);
* @brief Non-blocking read from channel
*
* The read will grab whatever is waiting in the FujiNet buffer. If fewer than the requested len, the return count will reflect this.
* Errors are returned as the negative value of the error.
* Errors are returned as the negative value of the FUJI standard error. fn_network_error contains the device specific error code. fn_bytes_read will be 0 on errors.
*
* @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/"
* @param buf Buffer
* @param len length
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values)
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code
*/
int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len);

Expand All @@ -117,12 +117,12 @@ int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len);
*
* The read will block until it has read all the bytes requested from the device, or the EOF is hit.
* This will block waiting for as much data as it can, so that the client does not need to handle counting.
* Errors are returned as the negative value of the error.
* Errors are returned as the negative value of the error. fn_network_error contains the device specific error code. fn_bytes_read will contain the count of bytes read before error occurred.
*
* @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/"
* @param buf Buffer
* @param len length
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values)
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code
*/
int16_t network_read(char* devicespec, uint8_t *buf, uint16_t len);

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.5.2
4.5.3

0 comments on commit c751591

Please sign in to comment.