Skip to content
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

Feature: join servers based on their invite code #9432

merged 3 commits into from Jul 11, 2021


Copy link

@TrueBrain TrueBrain commented Jul 11, 2021

This is currently only working with staging. See Testing how to test this PR!

Motivation / Problem

Currently joining a server requires you to have either of these two:

  • Know the hostname + port of the server.
  • Find the server in the (huge) list of public servers.

This is often annoying if you want your friend to join the same server.

Additionally, this allows for several other future additions, that without a system like this would be impossible:

  • "invite only" games: games that are open but not listed. Via an invite code you can join such games.
  • STUN / TURN support, where you no longer need to port-forward in order to play.


There are now two type of connection-strings:

  • server address. This connection-string is of the form hostname:port.
  • invite code. This connection-string always starts with a +.

The + of the connection-string to indicate invite codes is part of the network protocol, and the Game Coordinator can either give a server address or an invite code per server. This is done for backwards compatibility, allowing you to still see servers from 1.11 and before. These are always server address based, ofc.

As using an invite code means you do not know the hostname:port of the server, several packets are needed to talk to the Game Coordinator to discover this information. The idea is very simple:

  1. you tell the GC you want to connect to an indicate invite code
  2. the GC tells you how to reach that server, currently the only supported thing is a "direct-ip", where it gives you the hostname:port of the server. (in the future, STUN and TURN will be added)
  3. you connect to that address.
  4. you tell the GC the outcome: could you connect or not.
  5. the GC might offer an alternative way of connecting to the server.
  6. if all methods are exhausted, the GC tells you there is no way for you to connect to the server.


This is deployed on staging as of now, and you can test that by starting OpenTTD with:


It will NOT (!) work on production, so starting this PR unmodified will result in a non-functional multiplayer experience!


Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')


This allows future extensions to have different ways of referencing
a server, instead of forcing to use IP:port.
@TrueBrain TrueBrain changed the title Feature: also allow using invite codes to join servers Feature: join servers based on their invite code Jul 11, 2021
Copy link
Member Author

TrueBrain commented Jul 11, 2021

Okay, I deployed a compatible Game Coordinator on staging; from what I can see it is doing what it should be doing, but it could of course use a bit of testing :)

Tnx to #9429 this became a lot easier, you just have to set the env-variable OTTD_COORDINATOR_CS to

@TrueBrain TrueBrain marked this pull request as ready for review Jul 11, 2021
Copy link

glx22 commented Jul 11, 2021

Invite codes works for me with IPv6 or IPv4 (not both at the same time of course, current limitation of GC ;) )

src/network/core/address.cpp Outdated Show resolved Hide resolved
src/network/core/address.cpp Outdated Show resolved Hide resolved
src/network/core/tcp_coordinator.h Outdated Show resolved Hide resolved
src/network/network_coordinator.cpp Outdated Show resolved Hide resolved
src/network/network_coordinator.cpp Outdated Show resolved Hide resolved
src/network/network_coordinator.cpp Show resolved Hide resolved
src/network/core/tcp_coordinator.h Show resolved Hide resolved
src/network/core/tcp.h Outdated Show resolved Hide resolved
Normally TCPConnecter will do a DNS resolving of the connection_string
and connect to it. But for SERVER_ADDRESS_INVITE_CODE this is different:
the Game Coordinator does the "resolving".

This means we need to allow TCPConnecter to not setup a connection
and allow it to be told when a connection has been setup by an external
(to TCPConnecter) part of the code. We do this by telling the (active)
socket for the connection.

This means the rest of the code doesn't need to know the TCPConnecter
is not doing a simple resolve+connect. The rest of the code only
cares the connection is established; not how it was established.
@TrueBrain TrueBrain force-pushed the invite-codes branch 2 times, most recently from d043338 to 9d31875 Compare Jul 11, 2021
This removes the need to know a server IP to join it. Invite codes
are small (~7 characters) indentifiers for servers, which can be
exchanged with other players to join the servers.
@TrueBrain TrueBrain merged commit e4d216e into OpenTTD:master Jul 11, 2021
15 checks passed
@TrueBrain TrueBrain deleted the invite-codes branch Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants