From 56e39cc852daa819ca1a51c737b6ec0965ee39f2 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Sun, 29 Dec 2019 15:27:38 +0800 Subject: [PATCH] improve iostream --- docs/changes.md | 2 ++ src/tkc/iostream.c | 27 +++++++++++++++++++++ src/tkc/iostream.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/docs/changes.md b/docs/changes.md index 102fc0616..d5c0fda23 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,6 +1,8 @@ # 最新动态 + * 2019/12/29 * 使用event\_source\_manager实现主循环。 + * iostream增加几个包装函数。 * 2019/12/27 * 增加str\_from\_wstr\_with\_len diff --git a/src/tkc/iostream.c b/src/tkc/iostream.c index d6f193e8c..94548ab6f 100644 --- a/src/tkc/iostream.c +++ b/src/tkc/iostream.c @@ -32,3 +32,30 @@ tk_ostream_t* tk_iostream_get_ostream(tk_iostream_t* stream) { return stream->get_ostream(stream); } + +int32_t tk_iostream_read(tk_iostream_t* stream, uint8_t* buff, uint32_t max_size) { + tk_istream_t* is = tk_iostream_get_istream(stream); + + return tk_istream_read(is, buff, max_size); +} + +int32_t tk_iostream_read_len(tk_iostream_t* stream, uint8_t* buff, uint32_t max_size, + uint32_t timeout_ms) { + tk_istream_t* is = tk_iostream_get_istream(stream); + + return tk_istream_read_len(is, buff, max_size, timeout_ms); +} + +int32_t tk_iostream_write(tk_iostream_t* stream, const uint8_t* buff, uint32_t max_size) { + tk_ostream_t* os = tk_iostream_get_ostream(stream); + + return tk_ostream_write(os, buff, max_size); +} + +int32_t tk_iostream_write_len(tk_iostream_t* stream, const uint8_t* buff, uint32_t max_size, + uint32_t timeout_ms) { + tk_ostream_t* os = tk_iostream_get_ostream(stream); + + return tk_ostream_write_len(os, buff, max_size, timeout_ms); +} + diff --git a/src/tkc/iostream.h b/src/tkc/iostream.h index d71836a8e..f79c64f74 100644 --- a/src/tkc/iostream.h +++ b/src/tkc/iostream.h @@ -72,6 +72,66 @@ tk_istream_t* tk_iostream_get_istream(tk_iostream_t* stream); */ tk_ostream_t* tk_iostream_get_ostream(tk_iostream_t* stream); +/** + * @method tk_iostream_read + * + * 读取数据。 + * + * @param {tk_iostream_t*} stream iostream对象。 + * @param {uint8_t*} buff 返回数据的缓冲区。 + * @param {uint32_t} max_size 缓冲区的大小。 + * + * @return {int32_t} 返回负数表示读取失败,否则返回实际读取数据的长度。 + * + */ +int32_t tk_iostream_read(tk_iostream_t* stream, uint8_t* buff, uint32_t max_size); + +/** + * @method tk_iostream_read_len + * + * 读取指定长度的数据。 + * + * @param {tk_iostream_t*} stream iostream对象。 + * @param {uint8_t*} buff 返回数据的缓冲区。 + * @param {uint32_t} max_size 缓冲区的大小。 + * @param {uint32_t} timeout_ms timeout. + * + * @return {int32_t} 返回负数表示读取失败,否则返回实际读取数据的长度。 + * + */ +int32_t tk_iostream_read_len(tk_iostream_t* stream, uint8_t* buff, uint32_t max_size, + uint32_t timeout_ms); + +/** + * @method tk_iostream_write + * + * 写入数据。 + * + * @param {tk_iostream_t*} stream iostream对象。 + * @param {const uint8_t*} buff 返回数据的缓冲区。 + * @param {uint32_t} max_size 缓冲区的大小。 + * + * @return {int32_t} 返回负数表示写入失败,否则返回实际写入数据的长度。 + * + */ +int32_t tk_iostream_write(tk_iostream_t* stream, const uint8_t* buff, uint32_t max_size); + +/** + * @method tk_iostream_write_len + * + * 写入指定长度的数据。 + * + * @param {tk_iostream_t*} stream iostream对象。 + * @param {const uint8_t*} buff 数据的缓冲区。 + * @param {uint32_t} max_size 缓冲区的大小。 + * @param {uint32_t} timeout_ms timeout. + * + * @return {int32_t} 返回负数表示写入失败,否则返回实际写入数据的长度。 + * + */ +int32_t tk_iostream_write_len(tk_iostream_t* stream, const uint8_t* buff, uint32_t max_size, + uint32_t timeout_ms); + #define TK_IOSTREAM(obj) ((tk_iostream_t*)(obj)) END_C_DECLS