Skip to content

peek() in Stream class problem #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
miomir1981 opened this issue Apr 24, 2017 · 7 comments
Closed

peek() in Stream class problem #329

miomir1981 opened this issue Apr 24, 2017 · 7 comments

Comments

@miomir1981
Copy link

Hi all,

I have found that peek() method is not implemented properlly in Stream class. It returns 0.

I discovered this when I was testing ThingSpeak functionality. Response code is obtained by trying to parseInt() within response form server and this is where application get stuck.

@me-no-dev
Copy link
Member

Ahhh took me a while to track it down ;) You mean that it was not implemented right on the WiFiClient class.

@miomir1981
Copy link
Author

Now peek() will return -1. This didn't solve the issue.

I ran into this problem when I was testing ThingSpeak for ESP32. WriteFields() call sends the data to ThingSpeak server and data is successfully published, but there is an error when it tries to parse response because it is unable to read integer code of the response. ThingSpeak tries to parse integer from response received, starting from the position in the stream where it finds "HTTP/1.1". If peek() method returns -1, then parseInt()->peekNextDigit()->timedPeek()->peek() calls in Stream class will always return 0, i.e. result is interpreted as timeout. WiFiClient is not able to properly execute the parseInt() method.

peek() method for ESP8266 have implementation in ClientContext class. It have access to receive buffer and it returns byte from current position of the buffer.

@me-no-dev
Copy link
Member

Yes, but on ESP32 things are different and we use the POSIX API to access the network. I'm not sure that we can peek at the data with POSIX.

@miomir1981
Copy link
Author

miomir1981 commented May 7, 2017

I think this will work. I'm getting correct answer from parseInt() method now.

int WiFiClient::peek()
{
if(!available()) {
return -1;
}
uint8_t data = 0;
int res = recv(fd(), &data, 1, MSG_PEEK);
if(res < 0 && errno != EWOULDBLOCK) {
log_e("%d", errno);
stop();
}
return data;
}

I found this description on http://pubs.opengroup.org/onlinepubs/009695399/functions/recv.html
It states this:

"MSG_PEEK
Peeks at an incoming message. The data is treated as unread and the next recv() or similar function shall still return this data."

@miomir1981
Copy link
Author

miomir1981 commented May 17, 2017

@me-no-dev
Hi, did you rewiev the proposed code for peek() method or it is not correct?

Thanks

@me-no-dev
Copy link
Member

yes, sorry I'm otherwise preoccupied with work. If you have tested this and it works, please do submit a PR. It will be much easier for me to merge it :)

@me-no-dev
Copy link
Member

implemented!

blue-2357 pushed a commit to blue-2357/arduino-esp32 that referenced this issue Jul 17, 2024
dash0820 added a commit to dash0820/arduino-esp32-stripped that referenced this issue Mar 10, 2025
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

No branches or pull requests

2 participants