From f0dc5d2f677d534e425ea1509f134057dffc4443 Mon Sep 17 00:00:00 2001 From: Max Zhang Date: Wed, 25 Sep 2024 10:55:52 -0700 Subject: [PATCH] Guard starboard changes in posix files with macro STARBOARD To make the upstream easier, the changes made in _posix files for socket API changes are clearly guarded by macro defined(STARBOARD). b/302741384 --- net/socket/socket_posix.cc | 77 ++++++++++++++++++++++++++++++---- net/socket/socket_posix.h | 24 ++++++++--- net/socket/tcp_socket_posix.cc | 10 +++-- net/socket/tcp_socket_posix.h | 4 -- net/socket/udp_socket_posix.cc | 12 +++--- net/socket/udp_socket_posix.h | 17 ++++++-- 6 files changed, 113 insertions(+), 31 deletions(-) diff --git a/net/socket/socket_posix.cc b/net/socket/socket_posix.cc index 05fc339da581..77a3d2246fa2 100644 --- a/net/socket/socket_posix.cc +++ b/net/socket/socket_posix.cc @@ -33,8 +33,6 @@ namespace net { -#if SB_API_VERSION >= 16 - namespace { int MapAcceptError(int os_error) { @@ -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(); @@ -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); } @@ -179,7 +183,11 @@ int SocketPosix::Accept(std::unique_ptr* 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); } @@ -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); } @@ -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; } @@ -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); } @@ -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(); @@ -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); } @@ -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(); } } @@ -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); @@ -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); } @@ -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); } @@ -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); } @@ -539,13 +587,18 @@ 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()) { @@ -553,10 +606,20 @@ bool SocketPosix::ClearWatcherIfOperationsNotPending() { } 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. @@ -591,6 +654,4 @@ void SocketPosix::StopWatchingAndCleanUp(bool close_socket) { peer_address_.reset(); } -#endif // SB_API_VERSION >= 16 - } // namespace net diff --git a/net/socket/socket_posix.h b/net/socket/socket_posix.h index 36a7bfeb5396..ec85203d9b32 100644 --- a/net/socket/socket_posix.h +++ b/net/socket/socket_posix.h @@ -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(); @@ -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; @@ -126,6 +128,7 @@ 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_; @@ -133,15 +136,24 @@ class NET_EXPORT_PRIVATE SocketPosix 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> 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 read_buf_; int read_buf_len_ = 0; @@ -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 write_buf_; int write_buf_len_ = 0; // External callback; called when write or connect is complete. @@ -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_ diff --git a/net/socket/tcp_socket_posix.cc b/net/socket/tcp_socket_posix.cc index cd6362a1f569..e320be7513e3 100644 --- a/net/socket/tcp_socket_posix.cc +++ b/net/socket/tcp_socket_posix.cc @@ -62,8 +62,6 @@ namespace net { -#if SB_API_VERSION >= 16 - namespace { // SetTCPKeepAlive sets SO_KEEPALIVE. @@ -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); } @@ -252,11 +251,16 @@ int TCPSocketPosix::Accept(std::unique_ptr* 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); @@ -715,6 +719,4 @@ bool TCPSocketPosix::GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const { #endif // defined(TCP_INFO) } -#endif // SB_API_VERSION >= 16 - } // namespace net diff --git a/net/socket/tcp_socket_posix.h b/net/socket/tcp_socket_posix.h index a84cf61eb469..b1798a0494d2 100644 --- a/net/socket/tcp_socket_posix.h +++ b/net/socket/tcp_socket_posix.h @@ -26,8 +26,6 @@ class TimeDelta; namespace net { -#if SB_API_VERSION >= 16 - class AddressList; class IOBuffer; class IPEndPoint; @@ -229,8 +227,6 @@ class NET_EXPORT TCPSocketPosix { SocketTag tag_; }; -#endif // SB_API_VERSION >= 16 - } // namespace net #endif // NET_SOCKET_TCP_SOCKET_POSIX_H_ diff --git a/net/socket/udp_socket_posix.cc b/net/socket/udp_socket_posix.cc index c34d11d5ba5e..c69aa91056f5 100644 --- a/net/socket/udp_socket_posix.cc +++ b/net/socket/udp_socket_posix.cc @@ -64,8 +64,6 @@ namespace net { -#if SB_API_VERSION >= 16 - namespace { const int kBindRetries = 10; @@ -575,13 +573,13 @@ int UDPSocketPosix::SetDoNotFragment() { } void UDPSocketPosix::SetMsgConfirm(bool confirm) { -#if !BUILDFLAG(IS_APPLE) && defined(MSG_CONFIRM) +#if !BUILDFLAG(IS_APPLE) if (confirm) { sendto_flags_ |= MSG_CONFIRM; } else { sendto_flags_ &= ~MSG_CONFIRM; } -#endif // !BUILDFLAG(IS_APPLE) && defined(MSG_CONFIRM) +#endif // !BUILDFLAG(IS_APPLE) } int UDPSocketPosix::AllowAddressReuse() { @@ -854,10 +852,14 @@ int UDPSocketPosix::SetMulticastOptions() { if (rv < 0) return MapSystemError(errno); } +#if defined(STARBOARD) #if defined(IP_DEFAULT_MULTICAST_TTL) if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) { #elif defined(IP_MULTICAST_TTL) if (multicast_time_to_live_ != IP_MULTICAST_TTL) { +#endif +#else + if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) { #endif int rv; if (addr_family_ == AF_INET) { @@ -1100,6 +1102,4 @@ int UDPSocketPosix::SetIOSNetworkServiceType(int ios_network_service_type) { return OK; } -#endif // SB_API_VERSION >= 16 - } // namespace net diff --git a/net/socket/udp_socket_posix.h b/net/socket/udp_socket_posix.h index 44994e51ab0b..36d4e4e2e9be 100644 --- a/net/socket/udp_socket_posix.h +++ b/net/socket/udp_socket_posix.h @@ -33,8 +33,6 @@ namespace net { -#if SB_API_VERSION >= 16 - class IPAddress; class NetLog; struct NetLogSource; @@ -287,7 +285,11 @@ class NET_EXPORT UDPSocketPosix { SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 }; +#if defined(STARBOARD) class ReadWatcher : public base::MessagePumpForIO::Watcher { +#else + class ReadWatcher : public base::MessagePumpForIO::FdWatcher { +#endif public: explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} @@ -304,7 +306,11 @@ class NET_EXPORT UDPSocketPosix { const raw_ptr socket_; }; +#if defined(STARBOARD) class WriteWatcher : public base::MessagePumpForIO::Watcher { +#else + class WriteWatcher : public base::MessagePumpForIO::FdWatcher { +#endif public: explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} @@ -412,8 +418,13 @@ class NET_EXPORT UDPSocketPosix { mutable std::unique_ptr remote_address_; // The socket's posix wrappers +#if defined(STARBOARD) base::MessagePumpForIO::SocketWatcher read_socket_watcher_; base::MessagePumpForIO::SocketWatcher write_socket_watcher_; +#else + base::MessagePumpForIO::FdWatchController read_socket_watcher_; + base::MessagePumpForIO::FdWatchController write_socket_watcher_; +#endif // The corresponding watchers for reads and writes. ReadWatcher read_watcher_; @@ -467,8 +478,6 @@ class NET_EXPORT UDPSocketPosix { THREAD_CHECKER(thread_checker_); }; -#endif // SB_API_VERSION >= 16 - } // namespace net #endif // NET_SOCKET_UDP_SOCKET_POSIX_H_