From 9c20a9d549f85269cc1469fb61444d3aae92a0f3 Mon Sep 17 00:00:00 2001 From: MSoliankoLuxoft Date: Fri, 24 Jan 2025 16:16:59 +0200 Subject: [PATCH] Fix send UDP socket This commit fixes UDP packet sending for both connected and unconnected sockets. b\383980995 Change-Id: I2aff8facdc676233e9dac10259cdbe3124a984a6 --- net/socket/udp_socket_starboard.cc | 25 ++++++++++++++++++++---- starboard/shared/posix/socket_send_to.cc | 9 ++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/net/socket/udp_socket_starboard.cc b/net/socket/udp_socket_starboard.cc index 856dc6a42ffd..18dc45a57370 100644 --- a/net/socket/udp_socket_starboard.cc +++ b/net/socket/udp_socket_starboard.cc @@ -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_); @@ -650,7 +668,6 @@ int UDPSocketStarboard::InternalSendTo(IOBuffer* buf, LogWrite(result, buf->data(), address); } } - return result; } diff --git a/starboard/shared/posix/socket_send_to.cc b/starboard/shared/posix/socket_send_to.cc index 4f73c455393c..322bbd99c698 100644 --- a/starboard/shared/posix/socket_send_to.cc +++ b/starboard/shared/posix/socket_send_to.cc @@ -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."; @@ -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(bytes_written);