Skip to content

Commit

Permalink
Expose socket descriptors for all sockets.
Browse files Browse the repository at this point in the history
Update tests.
  • Loading branch information
Kilemonn committed Jul 9, 2024
1 parent cfeb031 commit b1aaa89
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/serversocket/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/serversocket/ServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
9 changes: 7 additions & 2 deletions src/socket/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/socket/TCPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/socket/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/socket/UDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace kt
std::pair<std::optional<std::string>, std::pair<int, kt::SocketAddress>> receiveFrom(const int&, const int& = 0);
std::pair<int, kt::SocketAddress> receiveFrom(char*, const int&, const int& = 0) const;

SOCKET getListeningSocket() const;
bool isUdpBound() const;
kt::InternetProtocolVersion getInternetProtocolVersion() const;
std::optional<unsigned short> getListeningPort() const;
Expand Down
5 changes: 5 additions & 0 deletions tests/serversocket/ServerSocketTCPTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/*
Expand Down Expand Up @@ -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());

Expand Down
3 changes: 3 additions & 0 deletions tests/socket/TCPSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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()));
}

/*
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions tests/socket/UDPSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ 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)
{
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());
Expand Down

0 comments on commit b1aaa89

Please sign in to comment.