Skip to content

Commit

Permalink
Merge pull request #5 from Kilemonn/create-tcp-socket-directly-from-a…
Browse files Browse the repository at this point in the history
…ddress

Add TCPSocket constructor from a SocketAddress. Add tests.
  • Loading branch information
Kilemonn authored Jul 14, 2024
2 parents ed374c1 + 1da9e92 commit 8c2b7cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/socket/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,33 @@ namespace kt
this->serverAddress = acceptedAddress;
}

TCPSocket::TCPSocket(const kt::TCPSocket& socket)
TCPSocket::TCPSocket(const kt::SocketAddress address)
{
std::optional<std::string> resolvedHostname = kt::getAddress(address);
this->hostname = resolvedHostname.value_or("");
this->port = kt::getPortNumber(address);
this->protocolVersion = kt::getInternetProtocolVersion(address);

addrinfo hints = kt::createTcpHints();
this->socketDescriptor = socket(address.address.sa_family, hints.ai_socktype, hints.ai_protocol);
if (isInvalidSocket(this->socketDescriptor))
{
throw kt::SocketException("Unable to construct socket to provided addresses with hostname [" + this->hostname + ":" + std::to_string(this->port) + "] " + getErrorCode());
}

int connectionResult = connect(this->socketDescriptor, &address.address, sizeof(address));
if (connectionResult == 0)
{
this->serverAddress = address;
this->protocolVersion = static_cast<kt::InternetProtocolVersion>(address.address.sa_family);
}
else
{
throw kt::SocketException("Unable to connect to provided address with hostname [" + this->hostname + ":" + std::to_string(this->port) + "] " + getErrorCode());
}
}

TCPSocket::TCPSocket(const kt::TCPSocket& socket)
{
this->socketDescriptor = socket.socketDescriptor;
this->hostname = socket.hostname;
Expand Down
1 change: 1 addition & 0 deletions src/socket/TCPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace kt
TCPSocket() = delete;
TCPSocket(const std::string&, const unsigned short&, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
TCPSocket(const SOCKET&, const std::string&, const unsigned short&, const kt::InternetProtocolVersion, const kt::SocketAddress&);
TCPSocket(const kt::SocketAddress);

TCPSocket(const kt::TCPSocket&);
kt::TCPSocket& operator=(const kt::TCPSocket&);
Expand Down
30 changes: 30 additions & 0 deletions tests/socket/TCPSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ namespace kt
TCPSocket failedSocket(LOCALHOST, serverSocket.getPort() + 1);
}, SocketException);
}

// Ensure we can construct and connect to a server from the address it is listening on
TEST_F(TCPSocketTest, TCPConstructor_FromAddress)
{
// Accept current incoming connection
TCPSocket server = serverSocket.acceptTCPConnection();

TCPSocket fromAddress(serverSocket.getSocketAddress());
TCPSocket acceptedFromAddress = serverSocket.acceptTCPConnection();

std::string sentFromAddress = "sentFromAddress";
ASSERT_TRUE(fromAddress.send(sentFromAddress).first);
ASSERT_TRUE(acceptedFromAddress.ready());
ASSERT_EQ(sentFromAddress, acceptedFromAddress.receiveAmount(sentFromAddress.size()));

std::string sentFromServer = "sentFromServer";
ASSERT_TRUE(acceptedFromAddress.send(sentFromServer).first);
ASSERT_TRUE(fromAddress.ready());
ASSERT_EQ(sentFromServer, fromAddress.receiveAmount(sentFromServer.size()));
}

// Ensure we throw a SocketException if we cannot construct a TCP socket from the provided SocketAddress
TEST_F(TCPSocketTest, TCPConstructor_FromEmptyAddress)
{
// Accept the incoming connection to make sure the server is ready
TCPSocket server = serverSocket.acceptTCPConnection();

kt::SocketAddress address{};
ASSERT_THROW(TCPSocket fromEmptyAddress(address), SocketException);
}

/*
* Ensure that a Socket created from the copy constructor is still able to send and receive from the copied socket.
Expand Down

0 comments on commit 8c2b7cd

Please sign in to comment.