Skip to content

Conversation

TD-er
Copy link
Collaborator

@TD-er TD-er commented Jul 18, 2025

As discussed here: #29 (comment)

Not yet tested, as I really need to get some sleep now...

My biggest concern right now is whether writeNextMsgId might also need to call flushBuffer() first.
But as I said, can't think clearly right now so if you can have a look @hmueller01

@hmueller01
Copy link
Owner

I think this can't be implemented like this.

size_t PubSubClient::write(const uint8_t* buffer, size_t size) {
    const size_t rc = appendBuffer(buffer, size);
    if (rc != 0) {
        lastOutActivity = millis();
    }
    return rc;
}

We can't set lastOutActivity here, as appendBuffer() does not necessarily write to the the broker. And we need this to ping the broker in time ...

if (keepAliveMillis && ((t - lastInActivity > this->keepAliveMillis) || (t - lastOutActivity > this->keepAliveMillis))) {

But setting lastOutActivity in flushBuffer() only, also won't work in all cases (e.g. if the application takes to long to fill all the needed data), as here

this->buffer[0] = MQTTPINGREQ;
we will overwrite the buffer to send the MQTTPINGREQ.
Maybe we can just flushBuffer() here, check the rc and only if it's 0 do the MQTTPINGREQ.

To your writeNextMsgId() concern. I don't think there is a problem. writeNextMsgId() is used in endPublish() there you did the flushBuffer() before and in subscribe() / unsubscribe() - don't care ...

@TD-er
Copy link
Collaborator Author

TD-er commented Jul 19, 2025

Well since appendBuffer may call flushBuffer, which does actual communications (and thus should set the lastOutActivity), I agree that appendBuffer itself and thus also these write calls should not set this lastOutActivity .

@TD-er
Copy link
Collaborator Author

TD-er commented Jul 19, 2025

I was thinking... maybe all _client.write calls should use this appendBuffer, just to make sure there isn't any risk of mixing up different calls.

@hmueller01
Copy link
Owner

This was my first thought too. But then more things need to be rewritten ...

@hmueller01
Copy link
Owner

And yes, at least here

rc += _client->write((uint8_t)pgm_read_byte_near(payload + i));

we should use it as well to solve #36 ...

@hmueller01
Copy link
Owner

@TD-er Did some implementations mentioned above. Looks quite promising. If you have time you may have a look over it.

@hmueller01
Copy link
Owner

@TD-er We could merge appendBuffer(uint8_t data)

size_t PubSubClient::appendBuffer(uint8_t data) {

into
size_t PubSubClient::write(uint8_t data) {
return appendBuffer(data);
}

and remove appendBuffer(uint8_t data). What do you think? Might be less clear ...

@hmueller01
Copy link
Owner

This PR fixes #29.

@hmueller01
Copy link
Owner

This fixes #36

@hmueller01
Copy link
Owner

@TD-er If you could take a look over it, I'd like to merge this. Tested it for a few weeks on a device of mine (but only uses publish_P()).

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