Is SDL_net fast enough to handle being a game server?

I’ve written a Windows .exe app that acts as a server for my game (it just passes TCP packets between devices that have chosen to connect to make a network game). It currently uses asynchronous Windows Sockets, but about once a month it crashes - and I can’t figure out why!

So, I’m thinking of re-writing it but using SDL_net 2 instead. My worry is that SDL_net might not be as quick as doing it with WinSock directly, and I think speed is very important as it’s a real-time game. Seems like you have to set a fixed number of allowed client connections in SDLNet_AllocSocketSet too which is a bit annoying, I can have hundreds of client connections at a time.

Also, there seems to be an SDL_net 3 as it’s hinted at here:

But the link to GitHub on that page shows the latest version is 2.2.0. Is SDL_net 3 due out soon?

I’d be surprised if you noticed a difference, I never have when switching from direct WinSock calls to using SDL2_net (not that my applications have been all that speed critical).

The thing I most want to see in SDL3_net is built-in support for SSL (I’m hoping it will be possible to embed OpenSSL in much the same way as Harfbuzz is embedded in SDL2_ttf).

I currently use SDL_net 2 for all of the devices playing my game, but they are all acting as clients which connect to my server (written with my own cobbled together WinSock code). SDL_net 2 seems quick enough for that but then again they’re only making a single connection, to the server.

I managed to find the code for SDL_net 3:

Not sure why it’s not listed as one of the releases though? Is it still in beta? Or is it ready to go?

Looks like SDL_net 3 is better written for me as it doesn’t have that maximum client limit. As the read function seem non-blocking and it doesn’t sit and wait for eg an FD_READ message to be sent when packets arrive, I guess you just need to sit in a while(TRUE) loop, constantly checking if a packet has arrived? I’m never keen to do that though, I always imagine that’ll take 100% processor time. Is there a way to sleep and just wake up when a packet arrives with SDL_net 3?

hi.

I don’t have experience with neither SDL_Net nor multiplayer game development.

so while being aware of that

I recommend libuv, or its historical predecessor which is still updated, libevent. both work on Windows, but will also work on any UNIX system for a production system. I don’t know about libevent, but libuv will use whatever is the fastest most performant most efficient APIs available for each operating system, such as IOCP on Windows. I’ve been using libuv, because I like using technology that powers a product of a big player, which is Node.js specifically. libuv abuses aliasing in C to have object-oriented inheritance between structs, which is weird, and requires -fno-strict-aliasing when compiling. libuv is a little weird in the beginning, but with experimentation, you can get the hand of it. I’m using libuv to implement an HTTP Web server. libuv doesn’t provide TLS out-of-the-box, you have to do it yourslf, and I’m currently struggling to implement it. libuv and libevent have nice documentation, but do check frequently examples and test programs to see how the API is used.

do you have a graphical game that optionally works as server? then initially I think you should stick with SDL_Net, because I don’t know how libuv and SDL play together, such as if they try to compete for any system resources, but I think libuv can be made to run inside SDL loop easily, but I don’t have experience.

I don’t know how libuv would interact with a physics engine, I’m really curious about that.

I think that with enough C preprocessor usage, you can share code between client and server, even if they have different backends (SDL_Net for the game client, libuv for server), but I’m not there yet to confirm.

never used sdl_net, but you could try valve’s open source network code

SDL3_net is in that nebulous “almost done” state. It has a few bugs in the issue tracker that need resolving before it officially ships.

It is (imho) a much better API than SDL2_net.

But I would think if the server is largely just passing packets around, either should work well enough in terms of performance.

That’s fantastic news! Thank you so much! My cobbled together code from an old book on WinSock crashes every few weeks, just can’t figure out why, and I’m banking on your code being more solid than mine! Signed up to your Patreon to say thanks for all your great work!

1 Like

Can SDL3_net be used with SDL2?

No, it uses a bunch of SDL3 things internally.

(But one of my upcoming goals is to make a run at trying to get OpenGLES1 working in SDL3 again, so maybe we can get you moved off of SDL2 entirely. :slight_smile: )

That would be great, but I’d still need to use SDL2-compat because my app (a programming language as you know) exposes SDL2 functions by name to the end-user (via dlsym).

I’ve already ascertained that on desktop platforms (when I can use full OpenGL) SDL2-compat seems to provide good enough compatibility for my app, including the way I mix SDL2 and direct calls to OpenGL.

The only thing that caught me out was an issue with audio, and the strange way that SDL_AudioSpec differs in its layout between MSVC and GCC. Linking my GCC-compiled code with the precompiled SDL2-compat DLL initially crashed.