Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix send for connected UDP socket #4402

Draft
wants to merge 1 commit into
base: 25.lts.1+
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions starboard/shared/posix/socket_bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ SbSocketError SbSocketBind(SbSocket socket,
return (socket->error = sbposix::TranslateSocketErrno(errno));
}

socket->is_connected = true;
return (socket->error = kSbSocketOk);
}
1 change: 1 addition & 0 deletions starboard/shared/posix/socket_connect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ SbSocketError SbSocketConnect(SbSocket socket, const SbSocketAddress* address) {
return (socket->error = kSbSocketErrorFailed);
}

socket->is_connected = true;
return (socket->error = kSbSocketOk);
}
6 changes: 5 additions & 1 deletion starboard/shared/posix/socket_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct SbSocketPrivate {
protocol(protocol),
socket_fd(fd),
error(kSbSocketOk),
waiter(kSbSocketWaiterInvalid) {}
waiter(kSbSocketWaiterInvalid),
is_connected(false) {}
~SbSocketPrivate() {}

// The address domain of this socket, IPv4 or IPv6.
Expand All @@ -49,6 +50,9 @@ struct SbSocketPrivate {

// The waiter this socket is registered with, or kSbSocketWaiterInvalid.
SbSocketWaiter waiter;

// Whether this socket is connected or not.
bool is_connected;
};

namespace starboard {
Expand Down
58 changes: 37 additions & 21 deletions starboard/shared/posix/socket_send_to.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,47 @@ int SbSocketSendTo(SbSocket socket,
socket->error = sbposix::TranslateSocketErrno(errno);
return -1;
} else if (socket->protocol == kSbSocketProtocolUdp) {
sbposix::SockAddr sock_addr;
const sockaddr* sockaddr = NULL;
socklen_t sockaddr_length = 0;
if (destination) {
if (!sock_addr.FromSbSocketAddress(destination)) {
SB_LOG(FATAL) << "Invalid destination passed to UDP send.";
socket->error = kSbSocketErrorFailed;
return -1;
if (socket->is_connected) {
ssize_t bytes_written =
send(socket->socket_fd, data, data_size, kSendFlags);
if (bytes_written >= 0) {
socket->error = kSbSocketOk;
return static_cast<int>(bytes_written);
}
sockaddr = sock_addr.sockaddr();
sockaddr_length = sock_addr.length;
}

ssize_t 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);
}
if (errno != EAGAIN && errno != EWOULDBLOCK) {
SB_LOG(ERROR) << "send failed, errno = " << errno;
}
socket->error = sbposix::TranslateSocketErrno(errno);
return -1;
} else {
sbposix::SockAddr sock_addr;
const sockaddr* sockaddr = NULL;
socklen_t sockaddr_length = 0;

if (errno != EAGAIN && errno != EWOULDBLOCK) {
SB_LOG(ERROR) << "sendto failed, errno = " << errno;
if (destination) {
if (!sock_addr.FromSbSocketAddress(destination)) {
SB_LOG(FATAL) << "Invalid destination passed to UDP send.";
socket->error = kSbSocketErrorFailed;
return -1;
}
sockaddr = sock_addr.sockaddr();
sockaddr_length = sock_addr.length;
}

ssize_t 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);
}

if (errno != EAGAIN && errno != EWOULDBLOCK) {
SB_LOG(ERROR) << "sendto failed, errno = " << errno;
}
socket->error = sbposix::TranslateSocketErrno(errno);
return -1;
}
socket->error = sbposix::TranslateSocketErrno(errno);
return -1;
}

SB_NOTREACHED() << "Unrecognized protocol: " << socket->protocol;
Expand Down
Loading