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

netplay: introduce abstractions for client/server-side sockets and connection providers #4105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ManManson
Copy link
Member

@ManManson ManManson commented Oct 25, 2024

This patch introduces multiple high-level abstractions over raw low-level sockets, which are necessary for supporting network backends other than default legacy TCP_DIRECT implementation:

  1. WzConnectionProvider - abstracts the way WZ establishes server-side and client-side connections. This thing effectively provides usable listen sockets and client connections to work with, hence the name.
  2. IListenSocket - abstraction over listen sockets.
  3. IClientConnection - abstraction over client-side sockets (and also server-side connections to the game clients).
  4. IConnectionPollGroup - generalization of socket sets for polling multiple connections in one go.
  5. ConnectionProviderRegistry - trivial singleton class providing storage for connection providers.
  6. IConnectionAddress - opaque connection address object, aimed to replace direct uses of addrinfo and provide a bit more abstract way to represent connection credentials.

The existing implementation in netplay/netsocket.h(.cpp) has been moved to the tcp subfolder and wrapped entirely into the tcp namespace.

The patch provides TCP*-prefixed implementations of the base interfaces mentioned above, which are implemented in terms of the old netsocket code.

There's now a ConnectionProviderType::TCP_DIRECT enumeration descriptor for accessing the default connection provider.

All uses in the high-level code (netplay.cpp, joiningscreen.cpp) are amended appropriately to use the all-new high-level abstractions instead of old low-level tcp-specific Socket and SocketSet.

NOTE: there are still a few functions from the tcp:: namespace used directly in the Discord RPC integration code, but these shouldn't pose any problem to either extract these into a more generic abstraction layer or to be rewritten not to use these functions at all, because they don't actually use any low-level stuff that's hard to refactor.

@ManManson ManManson force-pushed the higher_level_sockets branch 2 times, most recently from 23c5fc0 to d401946 Compare October 25, 2024 16:48
@ManManson ManManson added this to the 4.6.0 milestone Oct 25, 2024
@ManManson ManManson force-pushed the higher_level_sockets branch 4 times, most recently from 5353467 to 8cef02e Compare October 25, 2024 19:06
@ManManson ManManson force-pushed the higher_level_sockets branch 3 times, most recently from 7568a5c to 6d1f975 Compare November 16, 2024 12:16
@ManManson
Copy link
Member Author

Moved the async client connection code to the base WzConnectionProvider class. The logic is universal enough so that custom connection providers won't even need to overload openClientConnectionAsync() in most cases.

@ManManson ManManson force-pushed the higher_level_sockets branch from 08721fb to 770eb71 Compare November 17, 2024 13:04
@ManManson
Copy link
Member Author

Changed ConnectionProviderRegistry to avoid including backend-specific headers in connection_provider_registry.h (and thus leaking impl details from netplay library).

…nnection providers

This patch introduces multiple high-level abstractions over raw low-level
sockets, which are necessary for supporting network backends other
than default legacy `TCP_DIRECT` implementation:

1. `WzConnectionProvider` - abstracts the way WZ establishes
   server-side and client-side connections. This thing effectively
   provides usable listen sockets and client connections to work with,
   hence the name.
2. `IListenSocket` - abstraction over listen sockets.
3. `IClientConnection` - abstraction over client-side sockets (and
   also server-side connections to the game clients).
4. `IConnectionPollGroup` - generalization of socket sets for polling
   multiple connections in one go.
5. `ConnectionProviderRegistry` - trivial singleton class providing
   storage for connection providers.
6. `ConnectionAddress` - opaque connection address object, aimed
   to replace direct uses of `addrinfo` and provide a bit more
   abstract way to represent connection credentials. Still looks like
   a crutch right now, but it's better than nothing, nonetheless.

The existing implementation in `netplay/netsocket.h(.cpp)` has been
moved to the `tcp` subfolder and wrapped entirely into the `tcp`
namespace.

The patch provides `TCP*`-prefixed implementations of the base
interfaces mentioned above, which are implemented in terms of the
old `netsocket` code.

There's now a `ConnectionProviderType::TCP_DIRECT` enumeration
descriptor for accessing the default connection provider.

All uses in the high-level code (`netplay.cpp`, `joiningscreen.cpp`)
are amended appropriately to use the all-new high-level abstractions
instead of old low-level tcp-specific `Socket` and `SocketSet`.

NOTE: there are still a few functions from the `tcp::` namespace
used directly in the Discord RPC integration code, but these
shouldn't pose any problem to either extract these into a more generic
abstraction layer or to be rewritten not to use these functions at all,
because they don't actually use any low-level stuff that's hard to
refactor.

Signed-off-by: Pavel Solodovnikov <[email protected]>
This way it looks much cleaner and naturally doesn't leak any
implementation details to clients. Also, now it's much
easier to provide native connection address implementations
for each backend implementation.

Signed-off-by: Pavel Solodovnikov <[email protected]>
…e `WzConnectionProvider`

Provide the default implementation for `openClientConnectionAsync()`
in `WzConnectionProvider`, which just spawns a new thread and
piggibacks on the `resolveHost()` + `openClientConnectionAny()`
combination.

The `TCPConnectionProvider` is now simpler because it can use
the default implementation from the base class.

Signed-off-by: Pavel Solodovnikov <[email protected]>
@ManManson ManManson force-pushed the higher_level_sockets branch from 770eb71 to 1ae4f40 Compare November 17, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants