ESP32 WiFiServer.available() not working like Arduino's version
Posted: Sun Jan 29, 2023 8:11 am
Hello, I've been pounding my head against a wall lately trying to deal with multiple continuous TCP connections being handled from my ESP32 device. It is claimed that the ESP32 WiFi classes should act like Arduino's versions, but I have come to find that they don't in a way that has been causing me a lot of consternation, and I wanted to check here before filing an issue on GitHub over it. It's possible that the ESP32 team has decided to do it differently intentionally and I just haven't found the reasoning why.
Okay, so here what Arduino says about WiFiServer's server.available method (LINK)
The problem with the ESP32 version is this doesn't appear to be true. When I look in the WiFiClient.cpp code, it shows the object's deconstructor call stop(), which removes the inner connection to the underlying socket, which then causes the handle deconstructor to be called, which calls close on the underlying socket. I was pulling my hair out for quite a while trying to figure out why my connection kept immediately disconnecting after I just got it when the documentation clearly said it was supposed to be persistent.
Obviously, this isn't the way Arduino set this up to work, but this is an ESP32 library, so I wanted to know. Is this library supposed to reflect the functionality of the original Arduino library or have they decided that Arduino's functionality wasn't in line with how they wanted things done and changed it? Is there documentation anywhere on this to specify? I'm mostly wondering if it would be a waste of their time to put an issue on the libraries Github over it. I don't know the underlying C library very well, but I also wouldn't feel comfortable making changes and submitting code as I have no clue what the maintainers thoughts on how the TCP connections should be handled like. This may be a policy issue more than a bug, although, I will say it acts a lot like a bug.
I can't say I like Arduino's method of having the Server hold onto the connections and only handing over handles. Either you should get direct access to all handles or it should just hand them off to you right as soon as they connect.
Okay, so here what Arduino says about WiFiServer's server.available method (LINK)
Okay, so according to Arduino, the WiFiServer is supposed to hand over a client that has connected to it when you call this method. You can store it in a WiFiClient variable and manipulate it or get info out of it. The problem I am having is where it says that the server stays connected to the client even when the variable you are storing it in goes out of scope. Basically, what it gave you is just a handle to the real client and it doesn't matter if you lose it, because the Server is maintaining the real client itself.Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().
The problem with the ESP32 version is this doesn't appear to be true. When I look in the WiFiClient.cpp code, it shows the object's deconstructor call stop(), which removes the inner connection to the underlying socket, which then causes the handle deconstructor to be called, which calls close on the underlying socket. I was pulling my hair out for quite a while trying to figure out why my connection kept immediately disconnecting after I just got it when the documentation clearly said it was supposed to be persistent.
Obviously, this isn't the way Arduino set this up to work, but this is an ESP32 library, so I wanted to know. Is this library supposed to reflect the functionality of the original Arduino library or have they decided that Arduino's functionality wasn't in line with how they wanted things done and changed it? Is there documentation anywhere on this to specify? I'm mostly wondering if it would be a waste of their time to put an issue on the libraries Github over it. I don't know the underlying C library very well, but I also wouldn't feel comfortable making changes and submitting code as I have no clue what the maintainers thoughts on how the TCP connections should be handled like. This may be a policy issue more than a bug, although, I will say it acts a lot like a bug.
I can't say I like Arduino's method of having the Server hold onto the connections and only handing over handles. Either you should get direct access to all handles or it should just hand them off to you right as soon as they connect.