Berry add limit to tcp reads

This commit is contained in:
Stephan Hadinger 2022-01-27 18:48:57 +01:00
parent 32ec553cc7
commit f4aaaa2462
1 changed files with 14 additions and 0 deletions

View File

@ -325,10 +325,17 @@ extern "C" {
int32_t wc_tcp_read(struct bvm *vm);
int32_t wc_tcp_read(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
int32_t max_read = -1; // by default read as much as we can
if (be_top(vm) >= 2 && be_isint(vm, 2)) {
max_read = be_toint(vm, 2);
}
int32_t btr = tcp->available();
if (btr <= 0) {
be_pushstring(vm, "");
} else {
if ((max_read >= 0) && (btr > max_read)) {
btr = max_read;
}
char * buf = (char*) be_pushbuffer(vm, btr);
int32_t btr2 = tcp->read((uint8_t*) buf, btr);
be_pushnstring(vm, buf, btr2);
@ -340,10 +347,17 @@ extern "C" {
int32_t wc_tcp_readbytes(struct bvm *vm);
int32_t wc_tcp_readbytes(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
int32_t max_read = -1; // by default read as much as we can
if (be_top(vm) >= 2 && be_isint(vm, 2)) {
max_read = be_toint(vm, 2);
}
int32_t btr = tcp->available();
if (btr <= 0) {
be_pushbytes(vm, nullptr, 0);
} else {
if ((max_read >= 0) && (btr > max_read)) {
btr = max_read;
}
uint8_t * buf = (uint8_t*) be_pushbuffer(vm, btr);
int32_t btr2 = tcp->read(buf, btr);
be_pushbytes(vm, buf, btr2);