Add stop_listening()
This commit is contained in:
parent
440837cdbc
commit
4766d4755a
@ -57,6 +57,10 @@ NonBlockingServer::~NonBlockingServer() {
|
||||
close_server_socket();
|
||||
}
|
||||
|
||||
///
|
||||
/// @details Calling this repeatedly closes the previous socket and creates a
|
||||
/// new one.
|
||||
///
|
||||
std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||
/// Create socket
|
||||
|
||||
@ -87,6 +91,10 @@ std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||
return {};
|
||||
}
|
||||
|
||||
void NonBlockingServer::stop_listening() {
|
||||
close_server_socket();
|
||||
}
|
||||
|
||||
bool NonBlockingServer::next_client_available() {
|
||||
int ret = poll(&m_serverPfdIn, 1, 0);
|
||||
if (ret < 0) {
|
||||
|
||||
@ -36,6 +36,7 @@ public:
|
||||
~NonBlockingServer();
|
||||
|
||||
[[nodiscard]] std::expected<void, int> start_listening(uint16_t port);
|
||||
void stop_listening();
|
||||
|
||||
bool next_client_available();
|
||||
[[nodiscard]] std::expected<void, int> accept_next_client();
|
||||
|
||||
@ -24,6 +24,48 @@ TEST(TcpServer, Accept) {
|
||||
EXPECT_FALSE(server.next_client_available());
|
||||
}
|
||||
|
||||
TEST(TcpServer, RestartListening) {
|
||||
tcp::NonBlockingServer server;
|
||||
tcp::NonBlockingClient client;
|
||||
|
||||
auto lisRes = server.start_listening(1234);
|
||||
|
||||
auto conRes = client.connect({"localhost"}, 1234);
|
||||
|
||||
EXPECT_TRUE(server.next_client_available());
|
||||
|
||||
auto lisRes2 = server.start_listening(2345);
|
||||
|
||||
EXPECT_FALSE(server.next_client_available());
|
||||
|
||||
auto conRes2 = client.connect({"localhost"}, 2345);
|
||||
|
||||
EXPECT_TRUE(server.next_client_available());
|
||||
|
||||
auto statRes2 = client.get_last_connection_status();
|
||||
if (!statRes2) return;
|
||||
EXPECT_EQ(statRes2.value(), EISCONN);
|
||||
}
|
||||
|
||||
TEST(TcpServer, StopListening) {
|
||||
tcp::NonBlockingServer server;
|
||||
tcp::NonBlockingClient client;
|
||||
|
||||
auto lisRes = server.start_listening(1234);
|
||||
|
||||
auto conRes = client.connect({"localhost"}, 1234);
|
||||
|
||||
EXPECT_TRUE(server.next_client_available());
|
||||
|
||||
server.stop_listening();
|
||||
|
||||
EXPECT_FALSE(server.next_client_available());
|
||||
|
||||
auto conRes2 = client.connect({"localhost"}, 1234);
|
||||
|
||||
EXPECT_EQ(client.get_last_connection_status().value(), ECONNREFUSED);
|
||||
}
|
||||
|
||||
TEST(TcpServer, DataAvailable) {
|
||||
tcp::NonBlockingServer server;
|
||||
tcp::NonBlockingClient client;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user