Skip to content

Commit

Permalink
Socket: Replace SocketClient::close calls with SocketDescriptor::close
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Aug 11, 2024
1 parent fb6877c commit 177f005
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
2 changes: 0 additions & 2 deletions Libraries/Socket/Internal/SocketClient.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ SC::Result SC::SocketClient::connect(SocketIPAddress ipAddress)
return Result(true);
}

SC::Result SC::SocketClient::close() { return socket.close(); }

SC::Result SC::SocketClient::write(Span<const char> data)
{
SocketDescriptor::Handle nativeSocket;
Expand Down
10 changes: 3 additions & 7 deletions Libraries/Socket/SocketDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct String;

namespace SC
{
struct SocketDescriptor;
struct SC_COMPILER_EXPORT SocketDescriptor;
struct SocketFlags;
struct SocketIPAddress;
struct SocketNetworking;
Expand Down Expand Up @@ -235,11 +235,7 @@ struct SC::SocketClient
{
/// @brief Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)
/// @param socket A socket descriptor created with SocketDescriptor::create to be used as client
SocketClient(SocketDescriptor& socket) : socket(socket) {}

/// @brief Calls SocketDescriptor::close
/// @return The Result of SocketDescriptor::close
[[nodiscard]] Result close();
SocketClient(const SocketDescriptor& socket) : socket(socket) {}

/// @brief Connect to a given address and port combination
/// @param address Address as string
Expand Down Expand Up @@ -272,7 +268,7 @@ struct SC::SocketClient
[[nodiscard]] Result readWithTimeout(Span<char> data, Span<char>& readData, Time::Milliseconds timeout);

private:
SocketDescriptor& socket;
const SocketDescriptor& socket;
};

/// @brief Synchronous DNS Resolution
Expand Down
15 changes: 8 additions & 7 deletions Libraries/Socket/Tests/SocketDescriptorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void SC::SocketDescriptorTest::socketClientServer(SocketFlags::SocketType sock
buf[0]++;
params.writeRes = client.write({buf, sizeof(buf)});
params.eventObject.wait();
params.closeRes = client.close();
params.closeRes = clientSocket.close();
};
Thread thread;
SC_TEST_EXPECT(thread.start(func));
Expand All @@ -153,16 +153,17 @@ void SC::SocketDescriptorTest::socketClientServer(SocketFlags::SocketType sock
SC_TEST_EXPECT(acceptedClientSocket.isValid());
}

SocketClient acceptedClient(protocol == SocketFlags::ProtocolTcp ? acceptedClientSocket : serverSocket);
Span<char> readData;
char buf[1] = {0};
SocketDescriptor& socket = protocol == SocketFlags::ProtocolTcp ? acceptedClientSocket : serverSocket;
SocketClient acceptedClient(socket);
Span<char> readData;
char buf[1] = {0};
SC_TEST_EXPECT(acceptedClient.read({buf, sizeof(buf)}, readData));
SC_TEST_EXPECT(buf[0] == testValue and testValue != 0);
SC_TEST_EXPECT(not acceptedClient.readWithTimeout({buf, sizeof(buf)}, readData, Time::Milliseconds(10)));
params.eventObject.signal();
SC_TEST_EXPECT(acceptedClient.readWithTimeout({buf, sizeof(buf)}, readData, Time::Seconds(10)));
SC_TEST_EXPECT(buf[0] == testValue + 1);
SC_TEST_EXPECT(acceptedClient.close());
SC_TEST_EXPECT(socket.close());
SC_TEST_EXPECT(server.close());
params.eventObject.signal();
SC_TEST_EXPECT(thread.join());
Expand Down Expand Up @@ -239,7 +240,7 @@ SC::Result SC::SocketDescriptorTest::socketClientAcceptSnippet()
SC_TRY(acceptedClient.readWithTimeout({buf, sizeof(buf)}, readData, Time::Milliseconds(10000)));

// Close the client
SC_TRY(acceptedClient.close());
SC_TRY(acceptedClientSocket.close());
//! [socketClientAcceptSnippet]
return Result(true);
}
Expand Down Expand Up @@ -284,7 +285,7 @@ SC::Result SC::SocketDescriptorTest::socketClientConnectSnippet()
SC_TRY(client.write({buf, sizeof(buf)}));

// Close the socket
SC_TRY(client.close());
SC_TRY(clientSocket.close());
//! [socketClientConnectSnippet]
return Result(true);
}
Expand Down

0 comments on commit 177f005

Please sign in to comment.