-
Notifications
You must be signed in to change notification settings - Fork 51
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
Detect if a connection is disconnected without reading data? #58
Comments
Does https://wiki.libsdl.org/SDL_net/SDLNet_CheckSockets report the socket has new information in the case of a disconnect? I would check that first and see what happens. |
Oh, it isn't enough to know something happened, you need the socket to retain new data. https://wiki.libsdl.org/SDL_net/SDLNet_TCP_Recv You can probably request zero bytes of data and see if you get -1, but we probably need a better API to ensure this does what you want. Failing that, you can keep a small buffer in your app that contains bytes read from the socket but not yet given to the app. |
What I've done right now is the following:
|
OK. I've added some simple code to the SDL_net library:
And in the SDL_net.h (before the same function):
Then in my own project's code I called it together with the CheckSockets call for detecting if a connection is being requested:
But somehow, SDLNet_TCP_isready still reports the ready flag being set, even if the connection isn't being requested anymore? |
Also I've already tried receiving 0 bytes and checking for -1. It will keep continuing to give a result of 0, never -1. The SDLNet_TCP_Recv will always return 0 in below case (the listensocketset entry is the accepted TCP connection and the mysock entry is the accepted TCP socket itself that's in the socket set for listening to incoming data).
|
Hmmm... I've just looked at the new SDL3_net code. Could it be that with my TCPServer_incoming() function (of course everything ported to SDL3) that can be properly handled now on SDL3_net? Like (according to my understanding):
That would allow (in my case, other virtual software modem developers in the same way) allow for checking of the incoming connection without accepting it (giving a ringtone on the receiver's end) and the connection not receiving any data before the receiver's end is ready? Am I understanding this correctly? Ofc I also don't see any SDL2_net to SDL3_net migration guide I think? |
Yes, this is how it works; it'll tell you there's at least one connection, without actually accepting it, and it can be checked without blocking.
It's a complete rewrite of SDL_net, with a totally different API. You can see the motivations for it in #77, and some basic programs are in the "examples" directory. But there isn't really a migration guide, because it's pretty different. |
I've looked at the documentation of the functions and based on that modified my SDL2_net code to support SDL3_net as well. The basics are similar enough, just most of it being simplified (less memory buffer allocations and function calls). The basic framework (adjusting for some type changes) of my code can still work (https://bitbucket.org/superfury/commonemuframework/src/default/support/tcphelper.c ). It's roughly based on a modified version of Dosbox's connecting and server code (although extended for multiple incoming connections). It seems to work with the default case (IPv6 connecting to dual stack IPv4/v6 server). But when I connect using IPv4 it can't connect somehow? I've debugged into SDL3_net's code, but neither the server socket (using Other than that weird IPv4/v6 dual stack connecting though IPv4 issue, it's working properly with that basic setup (running as single client/server 'modem' emulation as well as single client with the server being 256 clients (raw TCP connections supported)). |
We don't explicitly turn off the IPV6_V6ONLY flag at the moment...on Linux, this defaults to off (dual-stack support), but WinSock defaults it to on (no dual-stack support). This is a bug, we intend to fix it. |
(It defaults to 1 for WinSock and 0 most other places.) We try to do this no matter what, and we don't care if it fails; in case of failure, it was either applied to a non-IPv6 socket where it would be meaningless, or we're just going to have to live with what we got anyhow. Reference Issue #84. Reference Issue #58.
Dual-stack sockets should be enabled on all platforms, as of the latest in revision control. |
OK. I just checked the latest commit with my app. I made my program setup a server socket, which is running properly. Edit: Checked a bit deeper now. When I look at the connection through 'netstat -q' on Windows 10, I see the following (the listening server port replaced with 1234 and PC name with ABCD):
The behaviour of the server listener is the same, it just polls SDLNet_WaitUntilInputAvailable with the first parameter being the server socket (that's listening to port 1234). So obviously the server socket is somehow read incorrectly from Windows at least? Does this happen on other platforms as well, or is it just a weird Windows issue? |
I have a case where I need to detect if a connection is disconnected (e.g. SDL_Net_TCP_Recv with size 1 returns <=0), but without actually receiving data (since the client itself isn't ready to parse it yet). And dropping the data is a big no-go, since that isn't expected of the setup (a virtual dial-up modem line being emulated over TCP).
Is this possible in any way with SDL(2)_net?
The text was updated successfully, but these errors were encountered: