CSM
CSM copied to clipboard
Use relay server so no port forwarding is necessary
To simplify hosting a server, we should provide a possibility where port forwarding is not necessary. Different levels of relaying could be possible:
- Use a technique called UDP hole punching to create the connection, then send messages directly (https://en.wikipedia.org/wiki/UDP_hole_punching). LiteNetLib has support for this. An example can be found here: https://github.com/RevenantX/LiteNetLib/issues/51#issuecomment-292790482
- Relay all messages (needs much more performance on the relay)
Things to discuss:
- What if the relay is not reachable? Fallback to the current solution?
- Hardcode the relay IP/domain? Allow the user to enter a custom IP?
I have a webserver that we can use for the first method as it doesn't have much performance or bandwidth.
i have a vps if you want to test these options
It's not quite as reliable as using a 3rd-party server to holepunch (since some routers don't support the feature or have it disabled), but I would also look into UPnP Port Mapping.
It's not quite as reliable as using a 3rd-party server to holepunch (since some routers don't support the feature or have it disabled), but I would also look into UPnP Port Mapping.
UPnP Port Mapping is in theory implemented, but I have no idea if it works at all (the async work around might be broken, Cities: Skylines uses an old version of .NET that does not support async/await)
https://github.com/CitiesSkylinesMultiplayer/CSM/blob/c6205aa8c06f8ea00852b15915bb187b537f7de6/src/Networking/Server.cs#L93-L94
It does work, I guess partly. For me I was able to launch and play without doing manual port-forwarding. Although I know many do have to do it manually.
Good to know, thanks. I'll take a look today at producing a simple server + client + relay example (for UDP hole punching) (make sure it works), and then attempt to integrate that into the mod (I've got a VPS I can use).
Last time I checked on Windows, UPnP was working, although it doesn't work on my current Linux system (But I also didn't bother looking into it, so it may be my fault). I guess many people just have UPnP deactivated on their routers, which is a good decision from a security perspective for most people.
About UDP hole punching: The LunaMultiplayer mod for Kerbal Space Program has an implementation for that. A very elaborate description can be found here: https://github.com/LunaMultiplayer/LunaMultiplayer/wiki/Nat-punchthrough and the source code might also be useful.
Awesome, thanks!
Another option for hosting a relay server could be the oracle cloud. you can get a free vps there.
Current testing code is here:
https://github.com/CitiesSkylinesMultiplayer/HolePunchTest
Managed to get it working one way (with no port forwarding), but not the other way. More testing required. (example client code is super messy).
UDP NAT hole punching was implemented in #298