Skip to content

Commit

Permalink
Guard starboard changes in posix files with macro STARBOARD
Browse files Browse the repository at this point in the history
To make the upstream easier, the changes made in _posix files
for socket API changes are clearly guarded by macro
defined(STARBOARD).

b/302741384
  • Loading branch information
maxz-lab committed Sep 25, 2024
1 parent ae05351 commit f0dc5d2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 31 deletions.
77 changes: 69 additions & 8 deletions net/socket/socket_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

namespace net {

#if SB_API_VERSION >= 16

namespace {

int MapAcceptError(int os_error) {
Expand Down Expand Up @@ -72,7 +70,13 @@ int MapConnectError(int os_error) {

SocketPosix::SocketPosix()
: socket_fd_(kInvalidSocket),
#if defined(STARBOARD)
socket_watcher_(FROM_HERE) {}
#else
accept_socket_watcher_(FROM_HERE),
read_socket_watcher_(FROM_HERE),
write_socket_watcher_(FROM_HERE) {}
#endif

SocketPosix::~SocketPosix() {
Close();
Expand Down Expand Up @@ -144,7 +148,7 @@ int SocketPosix::Bind(const SockaddrStorage& address) {

int rv = bind(socket_fd_, address.addr, address.addr_len);
if (rv < 0) {
PLOG(ERROR) << "bind() failed -errno- :" << errno;
PLOG(ERROR) << "bind() failed";
return MapSystemError(errno);
}

Expand Down Expand Up @@ -179,7 +183,11 @@ int SocketPosix::Accept(std::unique_ptr<SocketPosix>* socket,

if (!base::CurrentIOThread::Get()->WatchFileDescriptor(
socket_fd_, true, base::MessagePumpForIO::WATCH_READ,
#if defined(STARBOARD)
&socket_watcher_, this)) {
#else
&accept_socket_watcher_, this)) {
#endif
PLOG(ERROR) << "WatchFileDescriptor failed on accept";
return MapSystemError(errno);
}
Expand All @@ -204,7 +212,11 @@ int SocketPosix::Connect(const SockaddrStorage& address,

if (!base::CurrentIOThread::Get()->WatchFileDescriptor(
socket_fd_, true, base::MessagePumpForIO::WATCH_WRITE,
#if defined(STARBOARD)
&socket_watcher_, this)) {
#else
&write_socket_watcher_, this)) {
#endif
PLOG(ERROR) << "WatchFileDescriptor failed on connect";
return MapSystemError(errno);
}
Expand All @@ -224,7 +236,11 @@ int SocketPosix::Connect(const SockaddrStorage& address,

rv = MapConnectError(errno);
if (rv != OK && rv != ERR_IO_PENDING) {
#if defined(STARBOARD)
ClearWatcherIfOperationsNotPending();
#else
write_socket_watcher_.StopWatchingFileDescriptor();
#endif
return rv;
}

Expand Down Expand Up @@ -300,7 +316,11 @@ int SocketPosix::ReadIfReady(IOBuffer* buf,

if (!base::CurrentIOThread::Get()->WatchFileDescriptor(
socket_fd_, true, base::MessagePumpForIO::WATCH_READ,
#if defined(STARBOARD)
&socket_watcher_, this)) {
#else
&read_socket_watcher_, this)) {
#endif
PLOG(ERROR) << "WatchFileDescriptor failed on read";
return MapSystemError(errno);
}
Expand All @@ -312,7 +332,11 @@ int SocketPosix::ReadIfReady(IOBuffer* buf,
int SocketPosix::CancelReadIfReady() {
DCHECK(read_if_ready_callback_);

#if defined(STARBOARD)
bool ok = ClearWatcherIfOperationsNotPending();
#else
bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
#endif
DCHECK(ok);

read_if_ready_callback_.Reset();
Expand Down Expand Up @@ -350,7 +374,11 @@ int SocketPosix::WaitForWrite(IOBuffer* buf,

if (!base::CurrentIOThread::Get()->WatchFileDescriptor(
socket_fd_, true, base::MessagePumpForIO::WATCH_WRITE,
#if defined(STARBOARD)
&socket_watcher_, this)) {
#else
&write_socket_watcher_, this)) {
#endif
PLOG(ERROR) << "WatchFileDescriptor failed on write";
return MapSystemError(errno);
}
Expand Down Expand Up @@ -414,7 +442,12 @@ void SocketPosix::OnFileCanReadWithoutBlocking(int fd) {
"SocketPosix::OnFileCanReadWithoutBlocking");
if (!accept_callback_.is_null()) {
AcceptCompleted();
} else if (!read_if_ready_callback_.is_null()){
#if defined(STARBOARD)
} else if (!read_if_ready_callback_.is_null()) {
#else
} else {
DCHECK(!read_if_ready_callback_.is_null());
#endif
ReadCompleted();
}
}
Expand Down Expand Up @@ -451,7 +484,11 @@ void SocketPosix::AcceptCompleted() {
if (rv == ERR_IO_PENDING)
return;

#if defined(STARBOARD)
bool ok = ClearWatcherIfOperationsNotPending();
#else
bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
#endif
DCHECK(ok);
accept_socket_ = nullptr;
std::move(accept_callback_).Run(rv);
Expand All @@ -478,14 +515,22 @@ void SocketPosix::ConnectCompleted() {
if (rv == ERR_IO_PENDING)
return;

#if defined(STARBOARD)
bool ok = socket_watcher_.StopWatchingFileDescriptor();
#else
bool ok = write_socket_watcher_.StopWatchingFileDescriptor();
#endif
DCHECK(ok);
waiting_connect_ = false;
std::move(write_callback_).Run(rv);
}

int SocketPosix::DoRead(IOBuffer* buf, int buf_len) {
#if defined(STARBOARD)
int rv = HANDLE_EINTR(recv(socket_fd_, buf->data(), buf_len, 0));
#else
int rv = HANDLE_EINTR(read(socket_fd_, buf->data(), buf_len, 0));
#endif
return rv >= 0 ? rv : MapSystemError(errno);
}

Expand All @@ -509,7 +554,11 @@ void SocketPosix::RetryRead(int rv) {
void SocketPosix::ReadCompleted() {
DCHECK(read_if_ready_callback_);

#if defined(STARBOARD)
bool ok = socket_watcher_.StopWatchingFileDescriptor();
#else
bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
#endif
DCHECK(ok);
std::move(read_if_ready_callback_).Run(OK);
}
Expand All @@ -529,8 +578,7 @@ int SocketPosix::DoWrite(IOBuffer* buf, int buf_len) {
int rv = HANDLE_EINTR(send(socket_fd_, buf->data(), buf_len, kSendFlags));
#else
int rv = HANDLE_EINTR(write(socket_fd_, buf->data(), buf_len));
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) ||\
// defined(STARBOARD)
#endif
return rv >= 0 ? rv : MapSystemError(errno);
}

Expand All @@ -539,24 +587,39 @@ void SocketPosix::WriteCompleted() {
if (rv == ERR_IO_PENDING)
return;

#if defined(STARBOARD)
bool ok = ClearWatcherIfOperationsNotPending();
#else
bool ok = write_socket_watcher_.StopWatchingFileDescriptor();
#endif
DCHECK(ok);
write_buf_.reset();
write_buf_len_ = 0;
std::move(write_callback_).Run(rv);
}

#if defined(STARBOARD)
bool SocketPosix::ClearWatcherIfOperationsNotPending() {
bool ok = true;
if (!read_pending() && !write_pending() && !accept_pending()) {
ok = socket_watcher_.StopWatchingFileDescriptor();
}
return ok;
}
#endif

void SocketPosix::StopWatchingAndCleanUp(bool close_socket) {
#if defined(STARBOARD)
bool ok = socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
#else
bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
ok = read_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
ok = write_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
#endif

// These needs to be done after the StopWatchingFileDescriptor() calls, but
// before deleting the write buffer.
Expand Down Expand Up @@ -591,6 +654,4 @@ void SocketPosix::StopWatchingAndCleanUp(bool close_socket) {
peer_address_.reset();
}

#endif // SB_API_VERSION >= 16

} // namespace net
24 changes: 19 additions & 5 deletions net/socket/socket_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@

namespace net {

#if SB_API_VERSION >= 16

class IOBuffer;
struct SockaddrStorage;

// Socket class to provide asynchronous read/write operations on top of the
// posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses.
class NET_EXPORT_PRIVATE SocketPosix
#if defined(STARBOARD)
: public base::MessagePumpForIO::Watcher {
#else
: public base::MessagePumpForIO::FdWatcher {
#endif
public:
SocketPosix();

Expand Down Expand Up @@ -109,7 +111,7 @@ class NET_EXPORT_PRIVATE SocketPosix
SocketDescriptor socket_fd() const { return socket_fd_; }

private:
// base::MessagePumpForIO::Watcher methods.
// base::MessagePumpForIO::FdWatcher methods.
void OnFileCanReadWithoutBlocking(int fd) override;
void OnFileCanWriteWithoutBlocking(int fd) override;

Expand All @@ -126,22 +128,32 @@ class NET_EXPORT_PRIVATE SocketPosix
int DoWrite(IOBuffer* buf, int buf_len);
void WriteCompleted();

#if defined(STARBOARD)
bool read_pending() const { return !read_if_ready_callback_.is_null(); }
bool write_pending() const {
return !write_callback_.is_null() && !waiting_connect_;
}
bool accept_pending() const { return !accept_callback_.is_null(); }

bool ClearWatcherIfOperationsNotPending();
#endif

// |close_socket| indicates whether the socket should also be closed.
void StopWatchingAndCleanUp(bool close_socket);

SocketDescriptor socket_fd_;

#if !defined(STARBOARD)
base::MessagePumpForIO::FdWatchController accept_socket_watcher_;
#endif

raw_ptr<std::unique_ptr<SocketPosix>> accept_socket_;
CompletionOnceCallback accept_callback_;

#if !defined(STARBOARD)
base::MessagePumpForIO::FdWatchController read_socket_watcher_;
#endif

// Non-null when a Read() is in progress.
scoped_refptr<IOBuffer> read_buf_;
int read_buf_len_ = 0;
Expand All @@ -150,7 +162,11 @@ class NET_EXPORT_PRIVATE SocketPosix
// Non-null when a ReadIfReady() is in progress.
CompletionOnceCallback read_if_ready_callback_;

#if defined(STARBOARD)
base::MessagePumpForIO::SocketWatcher socket_watcher_;
#else
base::MessagePumpForIO::FdWatchController write_socket_watcher_;
#endif
scoped_refptr<IOBuffer> write_buf_;
int write_buf_len_ = 0;
// External callback; called when write or connect is complete.
Expand All @@ -165,8 +181,6 @@ class NET_EXPORT_PRIVATE SocketPosix
base::ThreadChecker thread_checker_;
};

#endif // SB_API_VERSION >= 16

} // namespace net

#endif // NET_SOCKET_SOCKET_POSIX_H_
10 changes: 6 additions & 4 deletions net/socket/tcp_socket_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@

namespace net {

#if SB_API_VERSION >= 16

namespace {

// SetTCPKeepAlive sets SO_KEEPALIVE.
Expand Down Expand Up @@ -239,6 +237,7 @@ int TCPSocketPosix::Bind(const IPEndPoint& address) {
SockaddrStorage storage;
if (!address.ToSockAddr(storage.addr, &storage.addr_len))
return ERR_ADDRESS_INVALID;

return socket_->Bind(storage);
}

Expand All @@ -252,11 +251,16 @@ int TCPSocketPosix::Accept(std::unique_ptr<TCPSocketPosix>* tcp_socket,
CompletionOnceCallback callback) {
DCHECK(tcp_socket);
DCHECK(!callback.is_null());
#if !defined(STARBOARD)
DCHECK(socket_);
#endif
DCHECK(!accept_socket_);

#if defined(STARBOARD)
if ((!socket_)) {
return MapSystemError(errno);
}
#endif

net_log_.BeginEvent(NetLogEventType::TCP_ACCEPT);

Expand Down Expand Up @@ -715,6 +719,4 @@ bool TCPSocketPosix::GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const {
#endif // defined(TCP_INFO)
}

#endif // SB_API_VERSION >= 16

} // namespace net
4 changes: 0 additions & 4 deletions net/socket/tcp_socket_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class TimeDelta;

namespace net {

#if SB_API_VERSION >= 16

class AddressList;
class IOBuffer;
class IPEndPoint;
Expand Down Expand Up @@ -229,8 +227,6 @@ class NET_EXPORT TCPSocketPosix {
SocketTag tag_;
};

#endif // SB_API_VERSION >= 16

} // namespace net

#endif // NET_SOCKET_TCP_SOCKET_POSIX_H_
Loading

0 comments on commit f0dc5d2

Please sign in to comment.