feat: Add TcpConnection and SocketConnectionListener with observables - #15
Merged
Conversation
Add generic Observer and Observable interfaces for event-style notifications. Observables keep weak references to registered observers, dispatch callbacks with a context object, and automatically remove expired observers while maintaining a clean notification pipeline.
Introduce TcpConnection (header + implementation) to manage a TCP socket and optionally upgrade it to TLS. Provides upgradeToTls(ssl_ctx) to wrap the socket into an asio::ssl::stream and perform async handshake, write(string_view) that async_writes to either the SSL stream or raw socket, close() that cleanly shuts down/ closes TLS and raw connections, and operator<< stream-style writer. Tracks _isClosed and _isTlsActive and logs handshake/write/close errors. Designed as a shared_from_this-enabled connection helper for async servers.
Introduce SocketConnectionListener (header and implementation). The class derives from Observable<std::shared_ptr<TcpConnection>> and manages ASIO IPv4 and IPv6 acceptors bound to a given port. Provides start/stop methods, runs async_accept loops for both address families, logs new connections, constructs shared TcpConnection instances and dispatches them to observers. stop() closes acceptors with std::error_code.
Introduce xtrpg_network static library (src/network/SocketConnectionListener.cpp, src/network/TcpConnection.cpp). Add public includes and link against asio and OpenSSL. Apply platform-specific settings: set _WIN32_WINNT/WINVER and link ws2_32/wsock32/crypt32 on Windows; link pthread and dl on Unix. Link xtrpg_network into xtrpg_cpp_server. Removes the previously commented global target_link_libraries snippet in CMakeLists.txt.
Clean up CMakeLists.txt by removing a large commented-out block that referenced platform-specific linking (ws2_32, wsock32, crypt32 for Windows; pthread, dl for Unix) and related notes about imported targets. This reduces clutter and keeps the file focused on active configuration; no functional changes were made.
This change configures the Windows console to use UTF-8, wraps initialization in a try/catch so startup exceptions are surfaced cleanly, and preserves the existing preamble and config registration flow for TOML and CLI parsing.
Change TcpConnection constructor to accept asio::ip::tcp::socket by value and move it into the member to allow ownership transfer and avoid binding to a temporary/reference. Also fix a typo in TcpConnection::upgrade: use the correct member name `_state` instead of `__state`. These are minor API/cleanup fixes to ensure correct move semantics and proper member access.
This updates the app to display the configured project version instead of a hardcoded 0.1.0 value, and includes the generated version header. It also fixes the generated version metadata to use the correct CMake version variables and the xtrpg namespace, while switching the fallback BUILD_VERSION to 0.0.0 so version info stays consistent across builds.
This change adds a strand to TcpConnection so socket writes, upgrades, and closes cannot race each other. It also introduces a closing-state check and guards upgrade/close/write paths against closed, closing, or already-secure connections. Handshake and I/O failures now close the underlying socket and transition the connection to CLOSED, preventing stale state and invalid async operations.
Replace concrete IPv4/IPv6 acceptors with std::optional acceptors and add initializeAcceptors() to attempt an IPv6 dual-stack socket first, falling back to IPv4. Add robust start/stop logic that checks optionals, cancels/closes acceptors safely, and guards accept loops. Introduce isListenerShutdownError helper to suppress expected shutdown errors and improve logging for accept failures.
Add a destructor to SocketConnectionListener that calls stop() during cleanup. This ensures acceptors and socket resources are shut down when the listener is destroyed, preventing lingering network state.
This patch prevents WinSock conflicts on Windows by defining `_WINSOCKAPI_` before including <windows.h>. It also reorders the includes so the project headers are included consistently with the platform-specific header setup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.