Berry 'tcp.write()' add 'offset' and 'len' (#24076)

This commit is contained in:
s-hadinger 2025-10-29 20:09:43 +01:00 committed by GitHub
parent 00b8c62fbe
commit 37fa0ba699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 4 deletions

View File

@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- Berry `gc_heap` and `gc_time` to `tasmota.memory()` (#24054) - Berry `gc_heap` and `gc_time` to `tasmota.memory()` (#24054)
- Scripter array transfer via UFS (#24060) - Scripter array transfer via UFS (#24060)
- ESP8266 GPIOViewer memory map if enabled with `#define GV_USE_ESPINFO` - ESP8266 GPIOViewer memory map if enabled with `#define GV_USE_ESPINFO`
- Berry `tcp.write()` add `offset` and `len`
### Breaking Changed ### Breaking Changed

View File

@ -38,7 +38,7 @@ extern "C" {
if (top >= 5) { if (top >= 5) {
if (!is_binary) { be_raise(vm, "argument_error", "start and len are not allowed with string payloads"); } if (!is_binary) { be_raise(vm, "argument_error", "start and len are not allowed with string payloads"); }
payload_start = be_toint(vm, 5); payload_start = be_toint(vm, 5);
if (payload_start < 0) payload_start = 0; if (payload_start < 0) { payload_start = 0; }
} }
if (top >= 6) { len = be_toint(vm, 6); } if (top >= 6) { len = be_toint(vm, 6); }
const char * topic = be_tostring(vm, 2); const char * topic = be_tostring(vm, 2);

View File

@ -511,10 +511,12 @@ extern "C" {
be_return(vm); be_return(vm);
} }
// tcp.write(bytes | string) -> int // tcp.write(bytes | string[, offset:int, len:int]) -> int
int32_t wc_tcpasync_write(struct bvm *vm); int32_t wc_tcpasync_write(struct bvm *vm);
int32_t wc_tcpasync_write(struct bvm *vm) { int32_t wc_tcpasync_write(struct bvm *vm) {
int32_t argc = be_top(vm); int32_t argc = be_top(vm);
int32_t offset = 0;
int32_t len = -1; // send all of it
if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) {
AsyncTCPClient * tcp = wc_gettcpclientasync_p(vm); AsyncTCPClient * tcp = wc_gettcpclientasync_p(vm);
const char * buf = nullptr; const char * buf = nullptr;
@ -525,8 +527,22 @@ extern "C" {
} else { // bytes } else { // bytes
buf = (const char*) be_tobytes(vm, 2, &buf_len); buf = (const char*) be_tobytes(vm, 2, &buf_len);
} }
size_t bw = tcp->write(buf, buf_len); if (argc >= 3 && be_isint(vm, 3)) { offset = be_toint(vm, 3); }
be_pushint(vm, bw); if (argc >= 4 && be_isint(vm, 4)) { len = be_toint(vm, 4); }
if (offset < 0) { offset = 0; } // default to the beginning of the buffer
if (offset >= buf_len) { len = 0; offset = 0; } // default to the end of the buffer (nothing to write)
else if (len < 0) { len = buf_len - offset; } // default to the end of the buffer
else if (offset + len > buf_len) { len = buf_len - offset; } // len is too long, adjust
if (len > 0) {
// now adjust the buffer with offset
buf_len += offset;
size_t bw = tcp->write(buf, buf_len);
be_pushint(vm, bw);
} else {
be_pushint(vm, 0); // nothing to send
}
be_return(vm); /* return code */ be_return(vm); /* return code */
} }
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);