Skip to content

Commit

Permalink
Make calls to recv() blocking.
Browse files Browse the repository at this point in the history
Add localhostname arg to UDP bind().
Bump version to 0.5.3.
  • Loading branch information
Kilemonn committed Aug 31, 2024
1 parent bc44c8b commit b2f1c99
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJECT_NAME CppSocketLibrary)

project(${PROJECT_NAME} VERSION 0.5.2)
project(${PROJECT_NAME} VERSION 0.5.3)

set(HEADERS
src/serversocket/ServerSocket.h
Expand Down
3 changes: 1 addition & 2 deletions src/socket/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ namespace kt
int TCPSocket::receiveAmount(char* buffer, const unsigned int amountToReceive, const int& flags) const
{
int counter = 0;

if (amountToReceive == 0 || !this->ready())
if (amountToReceive == 0)
{
return counter;
}
Expand Down
6 changes: 3 additions & 3 deletions src/socket/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace kt
*
* @throw BindingException - if the socket fails to bind
*/
std::pair<bool, kt::SocketAddress> kt::UDPSocket::bind(const unsigned short& port, const kt::InternetProtocolVersion protocolVersion)
std::pair<bool, kt::SocketAddress> kt::UDPSocket::bind(const std::optional<std::string>& localHostname, const unsigned short& port, const kt::InternetProtocolVersion protocolVersion)
{
if (this->isUdpBound())
{
Expand All @@ -58,7 +58,7 @@ namespace kt
#endif

addrinfo hints = kt::createUdpHints(protocolVersion, AI_PASSIVE);
std::pair<std::vector<kt::SocketAddress>, int> resolvedAddresses = kt::resolveToAddresses(kt::getLocalAddress(protocolVersion), port, hints);
std::pair<std::vector<kt::SocketAddress>, int> resolvedAddresses = kt::resolveToAddresses(localHostname.has_value() ? localHostname.value().c_str() : kt::getLocalAddress(protocolVersion), port, hints);
if (resolvedAddresses.second != 0 || resolvedAddresses.first.empty())
{
throw kt::BindingException("Failed to resolve bind address with the provided port: " + std::to_string(port));
Expand Down Expand Up @@ -179,7 +179,7 @@ namespace kt
std::pair<int, kt::SocketAddress> UDPSocket::receiveFrom(char* buffer, const int& receiveLength, const int& flags) const
{
kt::SocketAddress receiveAddress{};
if (!this->bound || receiveLength == 0 || !this->ready())
if (!this->bound || receiveLength == 0)
{
return std::make_pair(-1, receiveAddress);
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket/UDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace kt
UDPSocket(const kt::UDPSocket&);
kt::UDPSocket& operator=(const kt::UDPSocket&);

std::pair<bool, kt::SocketAddress> bind(const unsigned short& = 0, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
std::pair<bool, kt::SocketAddress> bind(const std::optional<std::string>& = std::nullopt, const unsigned short& = 0, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
void close();
bool ready(const unsigned long = 100) const;

Expand Down
18 changes: 9 additions & 9 deletions tests/socket/UDPSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace kt
kt::UDPSocket newServer;
ASSERT_FALSE(newServer.isUdpBound());
ASSERT_NE(std::nullopt, socket.getListeningPort());
EXPECT_THROW(newServer.bind(socket.getListeningPort().value()), BindingException);
EXPECT_THROW(newServer.bind(std::nullopt, socket.getListeningPort().value()), BindingException);
}

/*
Expand All @@ -75,7 +75,7 @@ namespace kt
TEST_F(UDPSocketTest, UDPBind_WithoutSpecifiedPort)
{
ASSERT_FALSE(socket.isUdpBound());
ASSERT_TRUE(socket.bind(0).first);
ASSERT_TRUE(socket.bind(std::nullopt, 0).first);
ASSERT_TRUE(socket.isUdpBound());
ASSERT_NE(0, socket.getListeningPort());
}
Expand Down Expand Up @@ -212,10 +212,10 @@ namespace kt
*/
TEST_F(UDPSocketTest, UDPManipulateAddress_IPV4)
{
ASSERT_TRUE(socket.bind(0, kt::InternetProtocolVersion::IPV4).first);
ASSERT_TRUE(socket.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV4).first);

kt::UDPSocket client;
ASSERT_TRUE(client.bind(0, kt::InternetProtocolVersion::IPV4).first);
ASSERT_TRUE(client.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV4).first);

std::string message = std::to_string(client.getListeningPort().value());
ASSERT_TRUE(client.sendTo("127.0.0.1", socket.getListeningPort().value(), message).first.first);
Expand All @@ -235,10 +235,10 @@ namespace kt

TEST_F(UDPSocketTest, UDPManipulateAddress_IPV6)
{
ASSERT_TRUE(socket.bind(0, kt::InternetProtocolVersion::IPV6).first);
ASSERT_TRUE(socket.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV6).first);

kt::UDPSocket client;
ASSERT_TRUE(client.bind(0, kt::InternetProtocolVersion::IPV6).first);
ASSERT_TRUE(client.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV6).first);

std::string message = std::to_string(client.getListeningPort().value());
ASSERT_TRUE(client.sendTo("::1", socket.getListeningPort().value(), message).first.first);
Expand All @@ -261,7 +261,7 @@ namespace kt
TEST_F(UDPSocketTest, SendToBoundAddress)
{
// Setting IP version here for windows as its seems to be biasing towards different IP versions
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(0, kt::InternetProtocolVersion::IPV4);
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV4);
ASSERT_TRUE(bindResult.first);

UDPSocket client;
Expand All @@ -278,7 +278,7 @@ namespace kt
*/
TEST_F(UDPSocketTest, LargePayloadSend)
{
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(0, kt::InternetProtocolVersion::IPV4);
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV4);
ASSERT_TRUE(bindResult.first);

int receiveBufferSize;
Expand Down Expand Up @@ -336,7 +336,7 @@ namespace kt
*/
TEST_F(UDPSocketTest, LargePayloadRecieve_AndPreSendSocketOperation)
{
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(0, kt::InternetProtocolVersion::IPV4);
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(std::nullopt, 0, kt::InternetProtocolVersion::IPV4);
ASSERT_TRUE(bindResult.first);

int receiveBufferSize;
Expand Down

0 comments on commit b2f1c99

Please sign in to comment.