From a78307488d2ea7af37f0d54744c861255e87de4a Mon Sep 17 00:00:00 2001 From: lixianjing Date: Tue, 24 Sep 2019 14:09:40 +0800 Subject: [PATCH] improve istream/ostream --- src/tkc/istream.c | 14 ++++++++++---- src/tkc/ostream.c | 13 +++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/tkc/istream.c b/src/tkc/istream.c index 91b7c8c20..502810466 100644 --- a/src/tkc/istream.c +++ b/src/tkc/istream.c @@ -55,7 +55,7 @@ ret_t tk_istream_flush(tk_istream_t* stream) { int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_size, uint32_t timeout_ms) { - uint32_t start = 0; + uint32_t now = 0; uint32_t end = 0; int32_t offset = 0; int32_t read_bytes = 0; @@ -63,8 +63,8 @@ int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_si return_value_if_fail(stream != NULL && stream->read != NULL, -1); return_value_if_fail(buff != NULL, 0); - start = time_now_ms(); - end = start + timeout_ms; + now = time_now_ms(); + end = now + timeout_ms; do { read_bytes = tk_istream_read(stream, buff + offset, remain_bytes); @@ -75,7 +75,13 @@ int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_si offset += read_bytes; remain_bytes -= read_bytes; - if (time_now_ms() > end) { + + if(remain_bytes == 0) { + break; + } + + now = time_now_ms(); + if (now > end) { log_debug("read timeout\n"); break; } diff --git a/src/tkc/ostream.c b/src/tkc/ostream.c index 3a3b6a09d..b35168dcc 100644 --- a/src/tkc/ostream.c +++ b/src/tkc/ostream.c @@ -38,7 +38,7 @@ ret_t tk_ostream_seek(tk_ostream_t* stream, uint32_t offset) { int32_t tk_ostream_write_len(tk_ostream_t* stream, const uint8_t* buff, uint32_t max_size, uint32_t timeout_ms) { - uint32_t start = 0; + uint32_t now = 0; uint32_t end = 0; int32_t offset = 0; int32_t write_bytes = 0; @@ -46,8 +46,8 @@ int32_t tk_ostream_write_len(tk_ostream_t* stream, const uint8_t* buff, uint32_t return_value_if_fail(stream != NULL && stream->write != NULL, -1); return_value_if_fail(buff != NULL, 0); - start = time_now_ms(); - end = start + timeout_ms; + now = time_now_ms(); + end = now + timeout_ms; do { write_bytes = tk_ostream_write(stream, buff + offset, remain_bytes); @@ -57,8 +57,13 @@ int32_t tk_ostream_write_len(tk_ostream_t* stream, const uint8_t* buff, uint32_t offset += write_bytes; remain_bytes -= write_bytes; + + if(remain_bytes == 0) { + break; + } - if (time_now_ms() > end) { + now = time_now_ms(); + if (now > end) { log_debug("write timeout\n"); break; }