From b1aaa89116a7b178879ae0b2ccf611114e152e40 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 9 Jul 2024 19:20:35 +0900 Subject: [PATCH] Expose socket descriptors for all sockets. Update tests. --- src/serversocket/ServerSocket.cpp | 10 ++++++++++ src/serversocket/ServerSocket.h | 2 ++ src/socket/TCPSocket.cpp | 9 +++++++-- src/socket/TCPSocket.h | 1 + src/socket/UDPSocket.cpp | 9 +++++++-- src/socket/UDPSocket.h | 1 + tests/serversocket/ServerSocketTCPTest.cpp | 5 +++++ tests/socket/TCPSocketTest.cpp | 3 +++ tests/socket/UDPSocketTest.cpp | 2 ++ 9 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/serversocket/ServerSocket.cpp b/src/serversocket/ServerSocket.cpp index abd68c5..13799de 100644 --- a/src/serversocket/ServerSocket.cpp +++ b/src/serversocket/ServerSocket.cpp @@ -293,6 +293,16 @@ namespace kt return this->port; } + SOCKET ServerSocket::getSocket() const + { + return this->socketDescriptor; + } + + kt::SocketAddress ServerSocket::getSocketAddress() const + { + return this->serverAddress; + } + /** * @return the *kt::InternetProtocolVersion* for this *kt::ServerSocket*. */ diff --git a/src/serversocket/ServerSocket.h b/src/serversocket/ServerSocket.h index c50d994..e1fc16c 100644 --- a/src/serversocket/ServerSocket.h +++ b/src/serversocket/ServerSocket.h @@ -66,6 +66,8 @@ namespace kt kt::SocketType getType() const; kt::InternetProtocolVersion getInternetProtocolVersion() const; unsigned short getPort() const; + SOCKET getSocket() const; + kt::SocketAddress getSocketAddress() const; void close(); }; diff --git a/src/socket/TCPSocket.cpp b/src/socket/TCPSocket.cpp index 6c12a67..8d8fdd1 100644 --- a/src/socket/TCPSocket.cpp +++ b/src/socket/TCPSocket.cpp @@ -135,8 +135,13 @@ namespace kt return this->send(message.c_str(), message.size(), flags); } - std::string TCPSocket::getHostname() const - { + SOCKET TCPSocket::getSocket() const + { + return this->socketDescriptor; + } + + std::string TCPSocket::getHostname() const + { return this->hostname; } diff --git a/src/socket/TCPSocket.h b/src/socket/TCPSocket.h index e0ca7d4..50edca5 100644 --- a/src/socket/TCPSocket.h +++ b/src/socket/TCPSocket.h @@ -68,6 +68,7 @@ namespace kt bool send(const char*, const int&, const int& = 0) const; bool send(const std::string&, const int& = 0) const; + SOCKET getSocket() const; std::string getHostname() const; unsigned short getPort() const; kt::InternetProtocolVersion getInternetProtocolVersion() const; diff --git a/src/socket/UDPSocket.cpp b/src/socket/UDPSocket.cpp index fa1e9ec..b14e119 100644 --- a/src/socket/UDPSocket.cpp +++ b/src/socket/UDPSocket.cpp @@ -200,8 +200,13 @@ namespace kt return std::make_pair(flag, receiveAddress); } - bool UDPSocket::isUdpBound() const - { + SOCKET UDPSocket::getListeningSocket() const + { + return this->receiveSocket; + } + + bool UDPSocket::isUdpBound() const + { return this->bound; } diff --git a/src/socket/UDPSocket.h b/src/socket/UDPSocket.h index 17a86c3..3c26bb5 100644 --- a/src/socket/UDPSocket.h +++ b/src/socket/UDPSocket.h @@ -71,6 +71,7 @@ namespace kt std::pair, std::pair> receiveFrom(const int&, const int& = 0); std::pair receiveFrom(char*, const int&, const int& = 0) const; + SOCKET getListeningSocket() const; bool isUdpBound() const; kt::InternetProtocolVersion getInternetProtocolVersion() const; std::optional getListeningPort() const; diff --git a/tests/serversocket/ServerSocketTCPTest.cpp b/tests/serversocket/ServerSocketTCPTest.cpp index 4e4ba74..e4f6c73 100644 --- a/tests/serversocket/ServerSocketTCPTest.cpp +++ b/tests/serversocket/ServerSocketTCPTest.cpp @@ -32,6 +32,7 @@ namespace kt ASSERT_EQ(SocketType::Wifi, serverSocket.getType()); ASSERT_NE(InternetProtocolVersion::Any, serverSocket.getInternetProtocolVersion()); ASSERT_EQ(PORT_NUMBER, serverSocket.getPort()); + ASSERT_FALSE(kt::isInvalidSocket(serverSocket.getSocket())); } /* @@ -60,6 +61,10 @@ namespace kt TEST_F(ServerSocketTCPTest, TestCopyConstructor) { ServerSocket server2(serverSocket); + ASSERT_EQ(serverSocket.getInternetProtocolVersion(), server2.getInternetProtocolVersion()); + ASSERT_EQ(serverSocket.getPort(), server2.getPort()); + ASSERT_EQ(serverSocket.getSocket(), server2.getSocket()); + ASSERT_EQ(serverSocket.getType(), server2.getType()); TCPSocket client("127.0.0.1", serverSocket.getPort()); diff --git a/tests/socket/TCPSocketTest.cpp b/tests/socket/TCPSocketTest.cpp index d6bf988..500d893 100644 --- a/tests/socket/TCPSocketTest.cpp +++ b/tests/socket/TCPSocketTest.cpp @@ -6,6 +6,7 @@ #include "../../src/socket/TCPSocket.h" #include "../../src/serversocket/ServerSocket.h" #include "../../src/socketexceptions/BindingException.hpp" +#include "../../src/socketexceptions/SocketError.h" const std::string LOCALHOST = "localhost"; //"127.0.0.1"; @@ -35,6 +36,7 @@ namespace kt ASSERT_FALSE(socket.ready()); ASSERT_EQ(LOCALHOST, socket.getHostname()); ASSERT_EQ(serverSocket.getInternetProtocolVersion(), socket.getInternetProtocolVersion()); + ASSERT_FALSE(kt::isInvalidSocket(socket.getSocket())); } /* @@ -78,6 +80,7 @@ namespace kt TCPSocket server = serverSocket.acceptTCPConnection(); TCPSocket copiedSocket(socket); + ASSERT_EQ(socket.getSocket(), copiedSocket.getSocket()); ASSERT_EQ(socket.getHostname(), copiedSocket.getHostname()); ASSERT_EQ(socket.getPort(), copiedSocket.getPort()); ASSERT_EQ(socket.getInternetProtocolVersion(), copiedSocket.getInternetProtocolVersion()); diff --git a/tests/socket/UDPSocketTest.cpp b/tests/socket/UDPSocketTest.cpp index a673a1a..d657073 100644 --- a/tests/socket/UDPSocketTest.cpp +++ b/tests/socket/UDPSocketTest.cpp @@ -34,6 +34,7 @@ namespace kt ASSERT_FALSE(socket.isUdpBound()); ASSERT_EQ(std::nullopt, socket.getListeningPort()); ASSERT_EQ(kt::InternetProtocolVersion::Any, socket.getInternetProtocolVersion()); + ASSERT_TRUE(kt::isInvalidSocket(socket.getListeningSocket())); } TEST_F(UDPSocketTest, UDPCopyConstructors) @@ -41,6 +42,7 @@ namespace kt socket.bind(); UDPSocket copiedSocket(socket); + ASSERT_EQ(socket.getListeningSocket(), copiedSocket.getListeningSocket()); ASSERT_EQ(socket.isUdpBound(), copiedSocket.isUdpBound()); ASSERT_NE(std::nullopt, socket.getListeningPort()); ASSERT_NE(std::nullopt, copiedSocket.getListeningPort());