improve istream/ostream

This commit is contained in:
lixianjing 2019-09-24 14:09:40 +08:00
parent 1e88ba179e
commit a78307488d
2 changed files with 19 additions and 8 deletions

View File

@ -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;
}

View File

@ -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;
}