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: Iddff989ccce8261395bb1ea36a23716a742c6ea2
  • Loading branch information
MSoliankoLuxoft committed Jan 24, 2025
1 parent 6426997 commit 8b6c8ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
35 changes: 19 additions & 16 deletions net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,34 +621,37 @@ int UDPSocketStarboard::InternalReadMultiplePackets(
int UDPSocketStarboard::InternalSendTo(IOBuffer* buf,
int buf_len,
const IPEndPoint* address) {
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);
return result;
// If remote_address_ is set, it indicates that the socket is connected.
// This is because remote_address_ is assigned in InternalConnect.
// In this case, we treat the socket as connected and ignore the 'address' parameter,
// sending the data to the pre-established remote address.
// Otherwise, if remote_address_ is not set, we use the 'address' parameter
// to specify the destination for the data.
if (remote_address_) {
int result = SbSocketSendTo(socket_, buf->data(), buf_len, nullptr);
if (result < 0) {
result = MapLastSocketError(socket_);
}
LogWrite(result, buf->data(), nullptr);
return result;
}

SbSocketAddress sb_address;

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)
if (result < 0) {
result = MapLastSocketError(socket_);
}

if (result != ERR_IO_PENDING) {
if (!address) {
LogWrite(result, buf->data(), nullptr);
} else {
LogWrite(result, buf->data(), address);
}
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 8b6c8ab

Please sign in to comment.