Drop disconnected members from chat rooms
C++ · C++ · intermediate · modification
Clients that hang up stay listed in their rooms until they send `/leave`, which they never do once their socket is gone. This PR adds `Room::prune_disconnected()` (declared in `src/chat/room.hpp`, implemented in `src/chat/room.cpp`): it collects the nicks of members whose connection is closed, removes each one with the existing `leave`, then announces `* NICK left` to the members who are still there and returns how many it removed. `src/chat/server.cpp` calls it on every room at the start of the once-a-second `Server::sweep()`, ahead of the existing step that drops empty rooms. `tests/room_test.cpp` (new) adds unit tests for `prune_disconnected`.
C++20, standard library only, built with CMake (unchanged: it builds `src/chat/*.cpp` into the server and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 locally and GCC 13 in CI, with `-Wall -Wextra -Wconversion -Werror`. The chat server runs a single-threaded event loop: `Room` and `Server` are used only on that thread, and a connection's `is_open()` changes only while the loop handles socket events, never during a call into `Room` or `Server`. Existing and unchanged: `chat::Connection` (`src/chat/connection.hpp`) is an abstract socket wrapper with `bool is_open() const noexcept` and `void send(std::string_view line) noexcept`, which queues a line and is a no-op on a closed connection. `Room` keeps its members in a `std::vector<Member>` in join order (`Member` is a nick and a `std::shared_ptr<Connection>`), at most 500 per room; `join` refuses a null connection and a nick already in the room, so nicks are unique within a room and every stored connection is non-null. `Server` (`src/chat/server.hpp`, unchanged) owns the rooms in a `std::map<std::string, Room, std::less<>>`. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed. `tests/fake_connection.hpp` (existing) provides `FakeConnection`, which records every line sent while open and has `close()`.
Requirements
- Add `Room::prune_disconnected()`: in one call it removes from the room every member whose connection is closed (`conn->is_open()` is false), whatever their position in the room, and keeps the other members in join order. It returns how many members it removed (0 when none are closed, including for an empty room).
- After removing them, it sends each remaining member one line `* NICK left` per removed member, in the removed members' join order. Removed members receive nothing, and nothing is sent when no member was removed. A removed nick is free to join the room again.
- `Server::sweep()` (existing, called by the event loop once a second) calls `prune_disconnected()` on every room before it drops the rooms that are empty, so a room whose last member disconnected is dropped in the same sweep.
- Unchanged: `join`, `leave` and `broadcast`. Ship unit tests for `prune_disconnected` using the repo's `tests/check.hpp` helper.
Files touched
- src/chat/room.hpp
- src/chat/room.cpp
- src/chat/server.cpp
- tests/room_test.cpp