Remove ServerSettings
This commit is contained in:
parent
47a2135093
commit
9582dbdc88
@ -8,8 +8,8 @@
|
|||||||
int main() {
|
int main() {
|
||||||
spdlog::set_level(spdlog::level::debug);
|
spdlog::set_level(spdlog::level::debug);
|
||||||
|
|
||||||
tcp::NonBlockingServer server{{.port = 65432}};
|
tcp::NonBlockingServer server{};
|
||||||
if (auto res = server.start_listening(); !res) {
|
if (auto res = server.start_listening(65432); !res) {
|
||||||
spdlog::error("Failed to start server: {}", res.error());
|
spdlog::error("Failed to start server: {}", res.error());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/tcp.cpp
12
src/tcp.cpp
@ -53,16 +53,12 @@ namespace tcp {
|
|||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
NonBlockingServer::NonBlockingServer(ServerSettings settings)
|
|
||||||
: m_settings{settings}, m_serverAddress(get_local_address(settings.port)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
NonBlockingServer::~NonBlockingServer() {
|
NonBlockingServer::~NonBlockingServer() {
|
||||||
close_client_socket();
|
close_client_socket();
|
||||||
close_server_socket();
|
close_server_socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<void, int> NonBlockingServer::start_listening() {
|
std::expected<void, int> NonBlockingServer::start_listening(uint16_t port) {
|
||||||
/// Create socket
|
/// Create socket
|
||||||
|
|
||||||
close_server_socket();
|
close_server_socket();
|
||||||
@ -72,8 +68,10 @@ std::expected<void, int> NonBlockingServer::start_listening() {
|
|||||||
|
|
||||||
/// Bin socket and start listening
|
/// Bin socket and start listening
|
||||||
|
|
||||||
if (::bind(m_serverSocket, (struct sockaddr*)&m_serverAddress,
|
const struct sockaddr_in serverAddress = get_local_address(port);
|
||||||
sizeof(m_serverAddress)) != 0) {
|
|
||||||
|
if (::bind(m_serverSocket, (struct sockaddr*)&serverAddress,
|
||||||
|
sizeof(serverAddress)) != 0) {
|
||||||
spdlog::error("tcp::NonBlockingServer: bind() failed with errno={}",
|
spdlog::error("tcp::NonBlockingServer: bind() failed with errno={}",
|
||||||
errno);
|
errno);
|
||||||
close(m_serverSocket);
|
close(m_serverSocket);
|
||||||
|
|||||||
@ -18,26 +18,24 @@ namespace tcp {
|
|||||||
|
|
||||||
using HostString = std::array<char, 64>;
|
using HostString = std::array<char, 64>;
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Non blocking server
|
// Non blocking server
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
struct ServerSettings {
|
|
||||||
uint16_t port = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NonBlockingServer {
|
class NonBlockingServer {
|
||||||
public:
|
public:
|
||||||
NonBlockingServer(ServerSettings settings);
|
NonBlockingServer() = default;
|
||||||
NonBlockingServer(const NonBlockingServer&) = delete;
|
NonBlockingServer(const NonBlockingServer&) = delete;
|
||||||
NonBlockingServer(NonBlockingServer&&) = delete;
|
NonBlockingServer(NonBlockingServer&&) = delete;
|
||||||
NonBlockingServer& operator=(const NonBlockingServer&) = delete;
|
NonBlockingServer& operator=(const NonBlockingServer&) = delete;
|
||||||
NonBlockingServer& operator=(NonBlockingServer&&) = delete;
|
NonBlockingServer& operator=(NonBlockingServer&&) = delete;
|
||||||
~NonBlockingServer();
|
~NonBlockingServer();
|
||||||
|
|
||||||
[[nodiscard]] std::expected<void, int> start_listening();
|
[[nodiscard]] std::expected<void, int> start_listening(uint16_t port);
|
||||||
|
|
||||||
bool next_client_available();
|
bool next_client_available();
|
||||||
[[nodiscard]] std::expected<void, int> accept_next_client();
|
[[nodiscard]] std::expected<void, int> accept_next_client();
|
||||||
|
|||||||
@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
|
|
||||||
TEST(TcpServer, Accept) {
|
TEST(TcpServer, Accept) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
EXPECT_FALSE(server.next_client_available());
|
EXPECT_FALSE(server.next_client_available());
|
||||||
|
|
||||||
@ -25,12 +25,12 @@ TEST(TcpServer, Accept) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpServer, DataAvailable) {
|
TEST(TcpServer, DataAvailable) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
EXPECT_FALSE(server.data_available());
|
EXPECT_FALSE(server.data_available());
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
@ -48,10 +48,10 @@ TEST(TcpServer, DataAvailable) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpServer, GetClientIPAsString) {
|
TEST(TcpServer, GetClientIPAsString) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
auto ipRes = server.get_client_ip_as_string();
|
auto ipRes = server.get_client_ip_as_string();
|
||||||
@ -61,7 +61,7 @@ TEST(TcpServer, GetClientIPAsString) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClient, Connect) {
|
TEST(TcpClient, Connect) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
/// Default connection status, i.e., before attempting connection
|
/// Default connection status, i.e., before attempting connection
|
||||||
@ -80,7 +80,7 @@ TEST(TcpClient, Connect) {
|
|||||||
|
|
||||||
/// Connection status when connection is acknowledged
|
/// Connection status when connection is acknowledged
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
|
|
||||||
auto conRes2 = client.connect({"localhost"}, 1234);
|
auto conRes2 = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
@ -98,12 +98,12 @@ TEST(TcpClient, Connect) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClient, Reconnect) {
|
TEST(TcpClient, Reconnect) {
|
||||||
tcp::NonBlockingServer server1{{.port = 1234}};
|
tcp::NonBlockingServer server1;
|
||||||
tcp::NonBlockingServer server2{{.port = 2345}};
|
tcp::NonBlockingServer server2;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes1 = server1.start_listening();
|
auto lisRes1 = server1.start_listening(1234);
|
||||||
auto lisRes2 = server2.start_listening();
|
auto lisRes2 = server2.start_listening(2345);
|
||||||
|
|
||||||
auto conRes1 = client.connect({"localhost"}, 1234);
|
auto conRes1 = client.connect({"localhost"}, 1234);
|
||||||
|
|
||||||
@ -119,12 +119,12 @@ TEST(TcpClient, Reconnect) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClient, DataAvailable) {
|
TEST(TcpClient, DataAvailable) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
EXPECT_FALSE(client.data_available());
|
EXPECT_FALSE(client.data_available());
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
@ -145,10 +145,10 @@ TEST(TcpClient, DataAvailable) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClientServer, ClientSendServerReceive) {
|
TEST(TcpClientServer, ClientSendServerReceive) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
@ -165,10 +165,10 @@ TEST(TcpClientServer, ClientSendServerReceive) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TcpClientServer, ClientReceiveServerSend) {
|
TEST(TcpClientServer, ClientReceiveServerSend) {
|
||||||
tcp::NonBlockingServer server{{.port = 1234}};
|
tcp::NonBlockingServer server;
|
||||||
tcp::NonBlockingClient client;
|
tcp::NonBlockingClient client;
|
||||||
|
|
||||||
auto lisRes = server.start_listening();
|
auto lisRes = server.start_listening(1234);
|
||||||
auto conRes = client.connect({"localhost"}, 1234);
|
auto conRes = client.connect({"localhost"}, 1234);
|
||||||
auto accRes = server.accept_next_client();
|
auto accRes = server.accept_next_client();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user