Skip to content

Conversation

iojan
Copy link

@iojan iojan commented Nov 26, 2021

First, thanks for this great library!
Current connect method only allows null terminated strings as willMessage payload.
I needed to send binary data, so I added a willMessageLenth argument.
If willMessageLenth is zero, willMessage will be copied as a null terminated string. If bigger than zero, willMessageLenth bytes will be copied form willMessage.
To keep it simple, buffer length check and copy were implemented inside the connect method. I can create a new check macro and copy function if you prefer.

@Illirgway
Copy link

Illirgway commented Dec 28, 2021

Wouldn't it be better to use memcopy / memmove instead of direct byte-to-byte copy loop?

                // const uint8_t* willMsg, uint16_t willMsgSz
                // check willMsg len
                // TODO check overflow
                if (length + 2 + willMsgSz > this->bufferSize) {
                  _client->stop();
                  return false;
                }

                // store msg size prefix
                this->buffer[length+0] = (willMsgSz >> 8);
                this->buffer[length+1] = (willMsgSz & 0xFF);
                length += 2; // count size prefix

                memcpy(&this->buffer[length], willMsg, willMsgSz); // or memmove

                length += willMsgSz;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants