Skip to content

Commit

Permalink
WIP: Deprecate SbSocket and migrate to socket APIs
Browse files Browse the repository at this point in the history
SbSocketClearLastError -> errno
SbSocketGetLastError   -> errno

Replace SbSocketPrivate by SbSocketWrapper (temporarily)

b/302701164

Change-Id: Ie1c099743a8cec011c890c87cb0b23ec945ca1a0
  • Loading branch information
maxz-lab committed Jan 18, 2024
1 parent fc372aa commit 06ef809
Show file tree
Hide file tree
Showing 31 changed files with 375 additions and 90 deletions.
7 changes: 7 additions & 0 deletions net/base/net_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#include "base/logging.h"
#include "net/base/net_export.h"
#if defined(STARBOARD)
#if SB_API_VERSION < 16
#include "starboard/common/socket.h"
#else
#include <errno.h>
#endif
#include "starboard/system.h"
#endif

Expand Down Expand Up @@ -57,13 +61,16 @@ NET_EXPORT bool IsDnsError(int error);
NET_EXPORT Error MapSystemError(logging::SystemErrorCode os_error);

#if defined(STARBOARD)

#if SB_API_VERSION < 16
// Map socket error code to Error.
NET_EXPORT Error MapSocketError(SbSocketError error);

// Gets the last socket error as a net error.
static SB_C_INLINE Error MapLastSocketError(SbSocket socket) {
return MapSocketError(SbSocketGetLastError(socket));
}
#endif // SB_API_VERSION < 16

// Gets the last system error as a net error.
static SB_C_INLINE Error MapLastSystemError() {
Expand Down
2 changes: 2 additions & 0 deletions net/base/net_errors_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Error MapSystemError(logging::SystemErrorCode error) {
return ERR_FAILED;
}

#if SB_API_VERSION < 16
Error MapSocketError(SbSocketError error) {
if (error != kSbSocketOk)
DVLOG(2) << "Error " << error;
Expand All @@ -55,5 +56,6 @@ Error MapSocketError(SbSocketError error) {
return ERR_FAILED;
}
}
#endif // SB_API_VERSION < 16

} // namespace net
93 changes: 91 additions & 2 deletions net/socket/tcp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <memory>
#include <utility>
#include <netinet/in.h>
#include <sys/socket.h>

#include "base/callback_helpers.h"
#include "base/time/time.h"
Expand All @@ -24,6 +26,8 @@
#include "net/socket/socket_net_log_params.h"
#include "net/socket/socket_options.h"
#include "starboard/configuration_constants.h"
#include "starboard/socket.h"


namespace net {

Expand All @@ -50,7 +54,16 @@ TCPSocketStarboard::~TCPSocketStarboard() {
int TCPSocketStarboard::Open(AddressFamily family) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!SbSocketIsValid(socket_));
#if SB_API_VERSION < 16
socket_ = SbSocketCreate(ConvertAddressFamily(family), kSbSocketProtocolTcp);
#else
int socket_fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (socket_fd < 0) {
socket_= kSbSocketInvalid;
} else {
socket_ = new SbSocketWrapper(ConvertAddressFamily(family), kSbSocketProtocolTcp, socket_fd);
}
#endif
if (!SbSocketIsValid(socket_)) {
return MapLastSystemError();
}
Expand Down Expand Up @@ -98,7 +111,11 @@ int TCPSocketStarboard::Bind(const IPEndPoint& address) {
SbSocketError error = SbSocketBind(socket_, &storage);
if (error != kSbSocketOk) {
DLOG(ERROR) << "SbSocketBind() returned an error";
#if SB_API_VERSION < 16
return MapLastSocketError(socket_);
#else
return errno;
#endif
}

local_address_.reset(new IPEndPoint(address));
Expand All @@ -115,7 +132,11 @@ int TCPSocketStarboard::Listen(int backlog) {
SbSocketError error = SbSocketListen(socket_);
if (error != kSbSocketOk) {
DLOG(ERROR) << "SbSocketListen() returned an error";
#if SB_API_VERSION < 16
int rv = MapLastSocketError(socket_);
#else
int rv = errno;
#endif
Close();
return rv;
}
Expand All @@ -142,7 +163,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr<TCPSocketStarboard>* socket,
socket_, true, base::MessageLoopCurrentForIO::WATCH_READ,
&socket_watcher_, this)) {
DLOG(ERROR) << "WatchSocket failed on read";
#if SB_API_VERSION < 16
return MapLastSocketError(socket_);
#else
return errno;
#endif
}

accept_socket_ = socket;
Expand All @@ -156,7 +181,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr<TCPSocketStarboard>* socket,
int TCPSocketStarboard::SetDefaultOptionsForServer() {
DCHECK(SbSocketIsValid(socket_));
if (!SbSocketSetReuseAddress(socket_, true)) {
#if SB_API_VERSION < 16
return MapLastSocketError(socket_);
#else
return errno;
#endif
}
return OK;
}
Expand All @@ -177,9 +206,20 @@ void TCPSocketStarboard::SetDefaultOptionsForClient() {
int TCPSocketStarboard::AcceptInternal(
std::unique_ptr<TCPSocketStarboard>* socket,
IPEndPoint* address) {
SbSocket new_socket = SbSocketAccept(socket_);
SbSocket new_socket;
#if SB_API_VERSION < 16
new_socket = SbSocketAccept(socket_);
if (!SbSocketIsValid(new_socket)) {
int net_error = MapLastSocketError(socket_);
#else
if(!SbSocketIsValid(socket_)) {
return errno;
}
new_socket->socket_fd = accept(socket_->socket_fd, NULL, NULL);
if (new_socket->socket_fd < 0) {
int net_error = errno;
socket_->error = TranslateSocketErrnoToSbError(errno);
#endif
if (net_error != ERR_IO_PENDING) {
net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error);
}
Expand All @@ -191,7 +231,11 @@ int TCPSocketStarboard::AcceptInternal(
// We use ReceiveFrom to get peer address of the newly connected socket.
int received = SbSocketReceiveFrom(new_socket, &unused_byte, 0, &sb_address);
if (received != 0) {
#if SB_API_VERSION < 16
int net_error = MapLastSocketError(new_socket);
#else
int net_error = errno;
#endif
if (net_error != OK && net_error != ERR_IO_PENDING) {
SbSocketDestroy(new_socket);
net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error);
Expand Down Expand Up @@ -312,7 +356,9 @@ void TCPSocketStarboard::OnSocketReadyToWrite(SbSocket socket) {
int TCPSocketStarboard::Connect(const IPEndPoint& address,
CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

DCHECK(SbSocketIsValid(socket_));

DCHECK(!peer_address_);
DCHECK(!connect_pending());

Expand All @@ -330,9 +376,35 @@ int TCPSocketStarboard::Connect(const IPEndPoint& address,

peer_address_.reset(new IPEndPoint(address));

#if SB_API_VERSION < 16
SbSocketError result = SbSocketConnect(socket_, &storage);

int rv = MapLastSocketError(socket_);
#else
SockAddr sock_addr;
if (!sock_addr.FromSbSocketAddress(&storage)) {
SB_DLOG(ERROR) << __FUNCTION__ << ": Invalid address";
return errno;
}
SB_DCHECK(socket_->socket_fd >= 0);
if (storage.type != socket_->address_type) {
SB_DLOG(ERROR) << __FUNCTION__ << ": Incompatible addresses: "
<< "socket type = " << socket_->address_type
<< ", argument type = " << storage.type;
return (int)kSbSocketErrorFailed;
}
int result = HANDLE_EINTR_WRAPPER(
connect(socket_->socket_fd, sock_addr.sockaddr(), sock_addr.length));
if (result != 0 && errno == EINPROGRESS) {
return errno;
}

if (result != 0) {
SB_DLOG(ERROR) << __FUNCTION__ << ": connect failed: " << errno;
return errno;
}

int rv = errno;
#endif
if (rv != ERR_IO_PENDING) {
return HandleConnectCompleted(rv);
}
Expand Down Expand Up @@ -502,10 +574,17 @@ int TCPSocketStarboard::DoRead(IOBuffer* buf, int buf_len) {
return bytes_read;
} else {
// If |bytes_read| < 0, some kind of error occurred.
#if SB_API_VERSION < 16
SbSocketError starboard_error = SbSocketGetLastError(socket_);
int rv = MapSocketError(starboard_error);
net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR,
CreateNetLogSocketErrorCallback(rv, starboard_error));
#else
int rv = errno;
net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR,
CreateNetLogSocketErrorCallback(rv, errno));
#endif

if (rv != ERR_IO_PENDING) {
DLOG(ERROR) << __FUNCTION__ << "[" << this << "]: Error: " << rv;
}
Expand Down Expand Up @@ -559,10 +638,16 @@ int TCPSocketStarboard::DoWrite(IOBuffer* buf, int buf_len) {

return bytes_sent;
} else {
#if SB_API_VERSION < 16
SbSocketError starboard_error = SbSocketGetLastError(socket_);
int rv = MapSocketError(starboard_error);
net_log_.AddEvent(NetLogEventType::SOCKET_WRITE_ERROR,
CreateNetLogSocketErrorCallback(rv, starboard_error));
#else
int rv = errno;
net_log_.AddEvent(NetLogEventType::SOCKET_WRITE_ERROR,
CreateNetLogSocketErrorCallback(rv, errno));
#endif
if (rv != ERR_IO_PENDING) {
DLOG(ERROR) << __FUNCTION__ << "[" << this << "]: Error: " << rv;
}
Expand Down Expand Up @@ -620,8 +705,12 @@ int TCPSocketStarboard::GetLocalAddress(IPEndPoint* address) const {

SbSocketAddress sb_address;
if (!SbSocketGetLocalAddress(socket_, &sb_address)) {
#if SB_API_VERSION < 16
SbSocketError starboard_error = SbSocketGetLastError(socket_);
return MapSocketError(starboard_error);
#else
return errno;
#endif
}

if (!address->FromSbSocketAddress(&sb_address)) {
Expand Down
51 changes: 50 additions & 1 deletion net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ int UDPSocketStarboard::GetLocalAddress(IPEndPoint* address) const {
if (!local_address_.get()) {
SbSocketAddress address;
if (!SbSocketGetLocalAddress(socket_, &address))
#if SB_API_VERSION < 16
return MapLastSocketError(socket_);
#else
return errno;
#endif
std::unique_ptr<IPEndPoint> endpoint(new IPEndPoint());
if (!endpoint->FromSbSocketAddress(&address))
return ERR_FAILED;
Expand Down Expand Up @@ -158,7 +162,11 @@ int UDPSocketStarboard::RecvFrom(IOBuffer* buf,
socket_, true, base::MessageLoopCurrentForIO::WATCH_READ,
&socket_watcher_, this)) {
PLOG(ERROR) << "WatchSocket failed on read";
#if SB_API_VERSION < 16
Error result = MapLastSocketError(socket_);
#else
int result = errno;
#endif
if (result == ERR_IO_PENDING) {
// Watch(...) might call SbSocketWaiterAdd() which does not guarantee
// setting system error on failure, but we need to treat this as an
Expand Down Expand Up @@ -209,9 +217,15 @@ int UDPSocketStarboard::SendToOrWrite(IOBuffer* buf,
if (!base::MessageLoopForIO::current()->Watch(
socket_, true, base::MessageLoopCurrentForIO::WATCH_WRITE,
&socket_watcher_, this)) {
#if SB_API_VERSION < 16
DVLOG(1) << "Watch failed on write, error "
<< SbSocketGetLastError(socket_);
Error result = MapLastSocketError(socket_);
#else
DVLOG(1) << "Watch failed on write, error "
<< errno;
int result = errno;
#endif
LogWrite(result, NULL, NULL);
return result;
}
Expand Down Expand Up @@ -284,10 +298,18 @@ int UDPSocketStarboard::SetReceiveBufferSize(int32_t size) {

int result = OK;
if (!SbSocketSetReceiveBufferSize(socket_, size)) {
#if SB_API_VERSION < 16
result = MapLastSocketError(socket_);
#else
result =errno;
#endif
}
DCHECK_EQ(result, OK) << "Could not " << __FUNCTION__ << ": "
#if SB_API_VERSION < 16
<< SbSocketGetLastError(socket_);
#else
<< errno;
#endif
return result;
}

Expand All @@ -297,10 +319,18 @@ int UDPSocketStarboard::SetSendBufferSize(int32_t size) {

int result = OK;
if (!SbSocketSetSendBufferSize(socket_, size)) {
#if SB_API_VERSION < 16
result = MapLastSocketError(socket_);
#else
result = errno;
#endif
}
DCHECK_EQ(result, OK) << "Could not " << __FUNCTION__ << ": "
#if SB_API_VERSION < 16
<< SbSocketGetLastError(socket_);
#else
<< errno;
#endif
return result;
}

Expand Down Expand Up @@ -435,7 +465,11 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf,
result = ERR_ADDRESS_INVALID;
}
} else {
#if SB_API_VERSION < 16
result = MapLastSocketError(socket_);
#else
result = errno;
#endif
}

if (result != ERR_IO_PENDING) {
Expand Down Expand Up @@ -463,8 +497,11 @@ int UDPSocketStarboard::InternalSendTo(IOBuffer* buf,
int result = SbSocketSendTo(socket_, buf->data(), buf_len, &sb_address);

if (result < 0)
#if SB_API_VERSION < 16
result = MapLastSocketError(socket_);

#else
result = errno;
#endif
if (result != ERR_IO_PENDING)
LogWrite(result, buf->data(), address);

Expand Down Expand Up @@ -497,7 +534,11 @@ int UDPSocketStarboard::JoinGroup(const IPAddress& group_address) const {

if (!SbSocketJoinMulticastGroup(socket_, &sb_address)) {
LOG(WARNING) << "SbSocketJoinMulticastGroup failed on UDP socket.";
#if SB_API_VERSION < 16
return MapLastSocketError(socket_);
#else
return errno;
#endif
}
return OK;
}
Expand Down Expand Up @@ -571,7 +612,11 @@ SendResult UDPSocketStarboardSender::InternalSendBuffers(
for (auto& buffer : buffers) {
int result = Send(socket, buffer->data(), buffer->length(), address);
if (result < 0) {
#if SB_API_VERSION < 16
rv = MapLastSocketError(socket);
#else
rv = errno;
#endif
break;
}
write_count++;
Expand Down Expand Up @@ -774,7 +819,11 @@ void UDPSocketStarboard::DidSendBuffers(SendResult send_result) {
if (last_async_result_ == ERR_IO_PENDING) {
DVLOG(2) << __func__ << " WatchSocket start";
if (!WatchSocket()) {
#if SB_API_VERSION < 16
last_async_result_ = MapLastSocketError(socket_);
#else
last_async_result_ = errno;
#endif
DVLOG(1) << "WatchSocket failed on write, error: " << last_async_result_;
LogWrite(last_async_result_, NULL, NULL);
} else {
Expand Down
Loading

0 comments on commit 06ef809

Please sign in to comment.