Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Writing to WiFiClient: how to know available space #6735

Unanswered
bertmelis asked this question in Q&A
Discussion options

WiFiClient --> Client --> Stream --> Print.

Print defines virtual int availableForWrite() { return 0; } but this isn't implemented in WiFiclient so calling this method returns 0.

How do you know the available space to write data to WiFiClient?
Same question for WiFiClientSecure.

You must be logged in to vote

Replies: 4 comments 1 reply

Comment options

Of course I can do something like

int wantToSend = someValue;
int sent = client.write(data, size);
if (sent < 0 || wantToSend < sent) {
 // no space left
}

But I find it more elegant to know up front.

You must be logged in to vote
0 replies
Comment options

Print.h states:

 // add availableForWrite to make compatible with Arduino Print.h
 // default to zero, meaning "a single write may block"
 // should be overriden by subclasses with buffering
 virtual int availableForWrite() { return 0; }

Looking at the implementations of WiFiClient::write(), none of them use buffering. Instead they try up to WIFI_CLIENT_MAX_WRITE_RETRY times to send the data, and return how much was actually sent. Thus these functions basically block until all data sends. For that reason it doesn't make sense to have an availableForWrite(). size_t WiFiClient::write(Stream &stream) looks async, but it also blocks until the stream empties.

You must be logged in to vote
1 reply
Comment options

Actually WiFiClient implementation is NetworkClient, isn't it?
I wonder why write function is not non-blocking. If so, MCU can be freed to handle other time-sensitive task like a hardware interrupt.

Comment options

I see. I missed that comment somehow 🤔

I'm looking at the regular "write(data, size)" method though. That doesn't seem to be blocking (MSG_DONTWAIT) but keeps writing and ultimately tries (#defined) 10 times before returning the amount written.

But then again, how to write large data? EAGAIN and EWOULDBLOCK result in zero bytes written. Should I divide the data into small chunks myself?

You must be logged in to vote
0 replies
Comment options

Wait, does socket send return EWOULDBLOCK only when the buffer is completely full? Or also when size is bigger then the buffer's free space?

(I really should read a book about socket programming, this is completely new for me)

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /