Am 02.12.2019 um 20:39 schrieb Davide Bettio:
So you're running the stock lwIP that comes with ESP32? If so, I don't
know how "vanilla" this is and how they keep their copy of lwIP up to
date...
Yes, I'm using the ESP32 version.
>
> We decided to use netconn API since it provides us asynchronous callbacks.
> However I'm experiencing an unexpected behavior with it (when using it
> as a TCP server):
> I get my callback called also for netconns which I didn't accept yet.
I'm not sure I understand that. Just for you to know: netconn is the
basis of the standard socket implementation in lwIP but otherwise not
may not too be widely used.
Do you suggest me to avoid using netconn? Anyway I would appreciate an API which allows me to:
- Receive asynchronous events (callbacks are ok)
- 0 copy to improve efficiency
- No need for a BSD socket API, I'm writing a virtual machine, so everything is wrapped
netconn looked like the right choice for this kind of needs.
Should I use some kind of hybrid approach? like using sockets and somehow callbacks?
Being like that, you *may* get problems when using netconn in a
different way than the socket API uses it internally. Maybe you can
cross-check your code against sockets.c and see if you do anything
different?
I'm using it in this way:
struct netconn *conn = netconn_new_with_proto_and_callback(NETCONN_TCP, 0, socket_callback); |
err_t status = netconn_bind(conn, IP_ADDR_ANY, port); |
if (UNLIKELY(status != ERR_OK)) { |
fprintf(stderr, "bind error: %i\n", status); |
status = netconn_getaddr(conn, &naddr, &nport, 1); |
if (UNLIKELY(status != ERR_OK)) { |
fprintf(stderr, "getaddr error: %i\n", status); |
}
Anyway I keep getting my callback called for netconns I didn't yet accept.
>
> So I get this output from my code:
>
> handler not found for: 0x3ffc3bf4
> processed all events
> found handler for: 0x3ffcd464
> tcp_server_handler
> going to send a ready message
> accepted conn: 0x3ffc3bf4
> accepted_socket
>
> Is this an intended behavior?
That's no debug output from lwIP
Yes, right, it's just a trace of my application.
> - Is it possible to check if any byte is already in the buffer ready to
> be received?
Yes, you can use nonblocking receive. Again, see sockets.c if you need
an example.
> - Is it possible to check if a netconn is still open or it has been closed?
Well, you get informed it is closed and you'll get an error trying to
use it...
Yes right, I was trying to understand to if it was possible to get this kind of information by inspecting netconn struct.
Thank you for your help so far.
Regards,
Davide Bettio.