Skip to content

Commit

Permalink
Fix send UDP socket
Browse files Browse the repository at this point in the history
    This commit fixes UDP packet sending for both connected and unconnected sockets.

    b\383980995

Change-Id: I2aff8facdc676233e9dac10259cdbe3124a984a6
  • Loading branch information
MSoliankoLuxoft committed Jan 31, 2025
1 parent 6426997 commit 9c20a9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
25 changes: 21 additions & 4 deletions net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,25 +621,43 @@ int UDPSocketStarboard::InternalReadMultiplePackets(
int UDPSocketStarboard::InternalSendTo(IOBuffer* buf,
int buf_len,
const IPEndPoint* address) {
#if SB_HAS_QUIRK(SENDING_CONNECTED_UDP_SOCKETS)
// If the socket is already connected (remote_address_ is set),
// call SbSocketSendTo without destination address as it will use
// the address from the connection
if (remote_address_) {
int result = SbSocketSendTo(socket_, buf->data(), buf_len, nullptr);
if (result < 0) {
result = MapLastSocketError(socket_);
}
if (result != ERR_IO_PENDING) {
LogWrite(result, buf->data(), nullptr);
}
return result;
}
#endif

// Old logic for platforms without SENDING_CONNECTED_UDP_SOCKETS support
// or for unconnected sockets
SbSocketAddress sb_address;
if (!address && !g_socket_extension) {
// Platforms without the socket extension require a destination address.
address = remote_address_.get();
if (!address) {
int result = ERR_FAILED;
LogWrite(result, NULL, NULL);
LogWrite(result, nullptr, nullptr);
return result;
}
}

if (address && !address->ToSbSocketAddress(&sb_address)) {
int result = ERR_FAILED;
LogWrite(result, NULL, NULL);
LogWrite(result, nullptr, nullptr);
return result;
}

int result = SbSocketSendTo(socket_, buf->data(), buf_len,
address ? &sb_address : nullptr);

if (result < 0)
result = MapLastSocketError(socket_);

Expand All @@ -650,7 +668,6 @@ int UDPSocketStarboard::InternalSendTo(IOBuffer* buf,
LogWrite(result, buf->data(), address);
}
}

return result;
}

Expand Down
9 changes: 6 additions & 3 deletions starboard/shared/posix/socket_send_to.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ int SbSocketSendTo(SbSocket socket,
socket->error = sbposix::TranslateSocketErrno(errno);
return -1;
} else if (socket->protocol == kSbSocketProtocolUdp) {
ssize_t bytes_written = 0;
sbposix::SockAddr sock_addr;
const sockaddr* sockaddr = NULL;
const sockaddr* sockaddr = nullptr;
socklen_t sockaddr_length = 0;

if (destination) {
if (!sock_addr.FromSbSocketAddress(destination)) {
SB_LOG(FATAL) << "Invalid destination passed to UDP send.";
Expand All @@ -70,8 +72,9 @@ int SbSocketSendTo(SbSocket socket,
sockaddr_length = sock_addr.length;
}

ssize_t bytes_written = sendto(socket->socket_fd, data, data_size,
kSendFlags, sockaddr, sockaddr_length);
bytes_written = sendto(socket->socket_fd, data, data_size, kSendFlags,
sockaddr, sockaddr_length);

if (bytes_written >= 0) {
socket->error = kSbSocketOk;
return static_cast<int>(bytes_written);
Expand Down

0 comments on commit 9c20a9d

Please sign in to comment.