EthernetServer problems with current implementation of operator bool
.
#236
Labels
topic: code
Related to content of the project itself
type: imperfection
Perceived defect in any part of project
Problem 1)
server.available()
works even withoutserver.begin()
. ifavailable()
doesn't find a listening socket, it callsbegin
to add it. This should not happen.Problem 2) server's operator bool returns false if a client already connected to the listening socket. When a client connects, the socket state changes to established. The next call to
available()
adds a new listening socket, but until thenif (server)
is false so it is not possible to use op bool to detect if the server is started. It would be useful in a sketch and for Problem 1.The documentation for
operator bool
says "It can also tell you when no more sockets are available to listen for more clients, because the maximum number have connected.". This is a bad attempt to make a special case of the Problem 2 a feature. Returning false from op bool if there is no more free position to add a new listening socket is not useful.available
still works for connected clients and it will add a listening socket as soon as there is a free position.One solution would be to add a field
bool started
, set it to true in begin and use it in op bool (and set it to false inend()
PR).Other solution would be in op bool to return true if
(server_port[i] == _port)
without checking the status. This will work because afterbegin
there will always be at least one record with _port. Methodavailable()
removes disconnected clients, but always adds a new listening socket if there isn't one. This version of op bool then can be used at the beginning of available() to check if server was started withbegin()
. I prefer this version and I will try to a PR.The text was updated successfully, but these errors were encountered: