Skip to content

Commit

Permalink
More socket helpers cleanup
Browse files Browse the repository at this point in the history
Change-Id: I112f93c1c544cd4a4d626958e0f52e438cca712c
  • Loading branch information
madhurajayaraman committed Sep 26, 2024
1 parent f4c8610 commit e9d1c3c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 122 deletions.
3 changes: 3 additions & 0 deletions starboard/nplb/socket_get_interface_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 17

#include "starboard/common/socket.h"
#include "starboard/nplb/socket_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -184,3 +186,4 @@ INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes,
} // namespace
} // namespace nplb
} // namespace starboard
#endif // SB_API_VERSION < 17
4 changes: 4 additions & 0 deletions starboard/nplb/socket_get_local_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 17

#include <utility>

#include "starboard/common/socket.h"
Expand Down Expand Up @@ -140,3 +142,5 @@ INSTANTIATE_TEST_CASE_P(
} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION < 17
101 changes: 0 additions & 101 deletions starboard/nplb/socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,6 @@ int GetPortNumberForTests() {
#endif
}

bool IsUnspecified(const SbSocketAddress* address) {
// Look at each piece of memory and make sure too many of them aren't zero.
int components = (address->type == kSbSocketAddressTypeIpv4 ? 4 : 16);
int zero_count = 0;
for (int i = 0; i < components; ++i) {
if (address->address[i] == 0) {
++zero_count;
}
}
return components == zero_count;
}

bool IsLocalhost(const SbSocketAddress* address) {
if (address->type == kSbSocketAddressTypeIpv4) {
return address->address[0] == 127;
}

if (address->type == kSbSocketAddressTypeIpv6) {
bool may_be_localhost = true;
for (int i = 0; i < 15; ++i) {
may_be_localhost &= (address->address[i] == 0);
}

return (may_be_localhost && address->address[15] == 1);
}

return false;
}

SbSocket CreateServerTcpSocket(SbSocketAddressType address_type) {
SbSocket server_socket = SbSocketCreate(address_type, kSbSocketProtocolTcp);
if (!SbSocketIsValid(server_socket)) {
Expand Down Expand Up @@ -146,78 +117,6 @@ SbSocket CreateListeningTcpSocket(SbSocketAddressType address_type, int port) {
return server_socket;
}

int Transfer(SbSocket receive_socket,
char* out_data,
SbSocket send_socket,
const char* send_data,
int size) {
int send_total = 0;
int receive_total = 0;
while (receive_total < size) {
if (send_total < size) {
int bytes_sent = SbSocketSendTo(send_socket, send_data + send_total,
size - send_total, NULL);
if (bytes_sent < 0) {
if (SbSocketGetLastError(send_socket) != kSbSocketPending) {
return -1;
}
bytes_sent = 0;
}

send_total += bytes_sent;
}

int bytes_received = SbSocketReceiveFrom(
receive_socket, out_data + receive_total, size - receive_total, NULL);
if (bytes_received < 0) {
if (SbSocketGetLastError(receive_socket) != kSbSocketPending) {
return -1;
}
bytes_received = 0;
}

receive_total += bytes_received;
}

return size;
}

int Transfer(Socket* receive_socket,
char* out_data,
Socket* send_socket,
const char* send_data,
int size) {
int send_total = 0;
int receive_total = 0;
while (receive_total < size) {
if (send_total < size) {
int bytes_sent =
send_socket->SendTo(send_data + send_total, size - send_total, NULL);
if (bytes_sent < 0) {
if (!send_socket->IsPending()) {
return -1;
}
bytes_sent = 0;
}

send_total += bytes_sent;
}

int bytes_received = receive_socket->ReceiveFrom(
out_data + receive_total, size - receive_total, NULL);
if (bytes_received < 0) {
if (!receive_socket->IsPending()) {
return -1;
}
bytes_received = 0;
}

receive_total += bytes_received;
}

return size;
}

int64_t TimedWait(SbSocketWaiter waiter) {
int64_t start = CurrentMonotonicTime();
SbSocketWaiterWait(waiter);
Expand Down
21 changes: 0 additions & 21 deletions starboard/nplb/socket_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ namespace nplb {

const int64_t kSocketTimeout = 200'000; // 200ms

// Returns true if the given address is the unspecified address (all zeros),
// supporting both address types.
bool IsUnspecified(const SbSocketAddress* address);

// Returns true if the given address is the localhost address, supporting both
// address types.
bool IsLocalhost(const SbSocketAddress* address);

// Returns a valid port number that can be bound to for use in nplb tests.
// This will always return the same port number.
int GetPortNumberForTests();
Expand All @@ -50,19 +42,6 @@ SbSocket CreateBoundTcpSocket(SbSocketAddressType address_type, int port);
// Creates a TCP/IP socket listening on all interfaces on the given port.
SbSocket CreateListeningTcpSocket(SbSocketAddressType address_type, int port);

// Transfers data between the two connected local sockets, spinning until |size|
// has been transferred, or an error occurs.
int Transfer(SbSocket receive_socket,
char* out_data,
SbSocket send_socket,
const char* send_data,
int size);
int Transfer(Socket* receive_socket,
char* out_data,
Socket* send_socket,
const char* send_data,
int size);

// Waits on the given waiter, and returns the elapsed time in microseconds.
int64_t TimedWait(SbSocketWaiter waiter);

Expand Down

0 comments on commit e9d1c3c

Please sign in to comment.