-
Notifications
You must be signed in to change notification settings - Fork 13.3k
WiFiClient - op bool should return false only for empty Client #9053
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
Conversation
in 'Arduino language' a Client with op bool returning false is used instead of a NULL pointer
This will probably break a lot of existing code. TBH, it makes sense to see I get it when you try to make the interface of each library acting the same, but when looking at the WiFiClient code, I don't think checking whether And while we're looking at what to expect from a function call, please have a look at this: uint8_t WiFiClient::connected()
{
if (!_client || _client->state() == CLOSED)
return 0;
return _client->state() == ESTABLISHED || available();
} I can imagine why it seemed useful to have the So please can you elaborate when it would be of any use to the user of this class to know whether the I'm a full supporter of making code better and easier to understand. |
the way you would manage clients connected to a server in 'Arduino language' is to have an array of WiFiClient objects. lets name it To get a client you set the value with in 'Arduino language' it is normal to pass WiFiClient object to a function as a copy (to avoid writing C's * or &).
this is a good code in 'Arduino language'. String and Client are classes which just wrap a pointer and have methods to manage it. my |
I hope you don't mean this WiFiClient should also be copied when calling? About this array of clients. There are only 2 exceptions:
It is not deleted when a connection is closed or aborted. So I still don't get it why it is of any use to the user of such a class to know the state of this pointer. Maybe I am just in dire need of some more coffee, but I really don't see it. |
deleted |
That's exactly what I meant with this:
It isn't such a strange way of 'resetting' an object as it allows to set some defaults without making assumptions of the default constructor implementation of that class. But here you check for |
Seems platform-specific thing? No mention of bool operator() in reference doc, so I would assume this would only help out with consistency between Cores Should the method then check for everything that connected() does, without |
https://www.arduino.cc/reference/en/libraries/ethernet/if-ethernetclient/ @mcspr please read second comment above. the first replay to TD-er |
Not sure if there are other values for |
This mentions "Indicates if the specified Ethernet client is ready." Edit: |
OK, so I would agree on pointer comment. Re-reading ethernet, c/p from connected() without available() makes more sense imo |
It can happen that a remote peer sends a final value (which is received locally) then remotely close the connection before this last data is read and still not lost.
The general issue with the arduino API is its inability to distinguish input and output (like the misunderstanding of When is this The current implementation is |
The main use of op bool is to check if server.accept() returned a valid client or a 'null' client. |
now I see that |
in 'Arduino language' which doesn't use pointers or references, an empty Client with operator bool returning false is used instead of a NULL pointer. an example is the return value of
server.accept()
.Implementations of Client operator bool in libraries by Arduino:
the WiFiNINA library
the classic Ethernet library
the MbedCore base class for WiFiClient and EthernetClient