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

Modernize NucleusLib async callbacks using lambdas #1539

Merged
merged 15 commits into from
Jan 1, 2024

Conversation

dgelessus
Copy link
Contributor

Trying to improve the "callback hell" in the low-/middle-level network code. The AsyncCore APIs relied heavily on C-style callback functions with untyped void* context parameters, which made it difficult to follow the execution and data flow in NetGameLib.

This PR migrates almost all of the async callbacks to use std::function (where they didn't already) and the callers to use lambdas for capturing context variables. This obsoletes the void* parameters and removes lots of manual casts.

AsyncSocket is a special case: its callback function was really multiple distinct callbacks wrapped in a generic signature, so I replaced it with a "callbacks object" with multiple virtual methods that the caller has to implement. This is a very "Java-ish" solution and is incompatible with lambdas, but works well with how the AsyncSocket API is used. It also clarifies the different callbacks' signatures and helps with seeing where each one is called.

The PR diff for the NetGameLib files is a bit noisy, because I changed some functions to instance methods and had to rename a couple of fields. The individual commit diffs are more readable there (e34454c is the noisy refactor).

@Hoikas Hoikas merged commit 020b54a into H-uru:master Jan 1, 2024
17 checks passed
@dgelessus dgelessus deleted the nucleuslib_nicer_callbacks branch January 2, 2024 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants