Skip to content

Commit

Permalink
Using socket_posix to replace socket_starboard
Browse files Browse the repository at this point in the history
b/302741384
  • Loading branch information
maxz-lab committed Jul 27, 2024
1 parent e25e899 commit a6ac9da
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 28 deletions.
8 changes: 4 additions & 4 deletions base/message_loop/message_pump_for_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/message_loop/ios_cronet_buildflags.h"
#include "build/build_config.h"

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
#include "base/message_loop/message_pump_io_starboard.h"
#elif BUILDFLAG(IS_WIN)
#include "base/message_loop/message_pump_win.h"
Expand All @@ -23,13 +23,13 @@
#include "base/message_loop/message_pump_default.h"
#elif BUILDFLAG(IS_FUCHSIA)
#include "base/message_loop/message_pump_fuchsia.h"
#elif BUILDFLAG(IS_POSIX)
#elif BUILDFLAG(IS_POSIX) || SB_API_VERSION >= 16
#include "base/message_loop/message_pump_libevent.h"
#endif

namespace base {

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
using MessagePumpForIO = MessagePumpIOStarboard;
#elif BUILDFLAG(IS_WIN)
// Windows defines it as-is.
Expand All @@ -42,7 +42,7 @@ using MessagePumpForIO = MessagePumpKqueue;
using MessagePumpForIO = MessagePumpDefault;
#elif BUILDFLAG(IS_FUCHSIA)
using MessagePumpForIO = MessagePumpFuchsia;
#elif BUILDFLAG(IS_POSIX)
#elif BUILDFLAG(IS_POSIX) || SB_API_VERSION >= 16
using MessagePumpForIO = MessagePumpLibevent;
#else
#error Platform does not define MessagePumpForIO
Expand Down
4 changes: 4 additions & 0 deletions base/message_loop/message_pump_io_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace base {

#if SB_API_VERSION <= 15

MessagePumpIOStarboard::SocketWatcher::SocketWatcher(const Location& from_here)
: created_from_location_(from_here),
interests_(kSbSocketWaiterInterestNone),
Expand Down Expand Up @@ -279,4 +281,6 @@ void MessagePumpIOStarboard::OnSocketWaiterNotification(SbSocketWaiter waiter,
}
}

#endif // SB_API_VERSION <= 15

} // namespace base
4 changes: 4 additions & 0 deletions base/message_loop/message_pump_io_starboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace base {

#if SB_API_VERSION <= 15

// Class to monitor sockets and issue callbacks when sockets are ready for I/O.
class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
public:
Expand Down Expand Up @@ -172,6 +174,8 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {

using MessagePumpForIO = MessagePumpIOStarboard;

#endif // SB_API_VERSION <= 15

} // namespace base

#endif // BASE_MESSAGE_PUMP_IO_STARBOARD_H_
4 changes: 2 additions & 2 deletions base/task/current_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ MessagePumpForIO* CurrentIOThread::GetMessagePumpForIO() const {

#if !BUILDFLAG(IS_NACL)

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
bool CurrentIOThread::Watch(SbSocket socket,
bool persistent,
int mode,
Expand All @@ -234,7 +234,7 @@ bool CurrentIOThread::RegisterJobObject(HANDLE job,
return GetMessagePumpForIO()->RegisterJobObject(job, handler);
}

#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
bool CurrentIOThread::WatchFileDescriptor(
int fd,
bool persistent,
Expand Down
4 changes: 2 additions & 2 deletions base/task/current_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class BASE_EXPORT CurrentIOThread : public CurrentThread {

#if !BUILDFLAG(IS_NACL)

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
typedef base::MessagePumpIOStarboard::Watcher Watcher;
typedef base::MessagePumpIOStarboard::SocketWatcher SocketWatcher;
typedef base::MessagePumpIOStarboard::IOObserver IOObserver;
Expand All @@ -288,7 +288,7 @@ class BASE_EXPORT CurrentIOThread : public CurrentThread {
// Please see MessagePumpWin for definitions of these methods.
HRESULT RegisterIOHandler(HANDLE file, MessagePumpForIO::IOHandler* handler);
bool RegisterJobObject(HANDLE job, MessagePumpForIO::IOHandler* handler);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// Please see WatchableIOMessagePumpPosix for definition.
// Prefer base::FileDescriptorWatcher for non-critical IO.
bool WatchFileDescriptor(int fd,
Expand Down
12 changes: 12 additions & 0 deletions net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1171,10 +1171,22 @@ component("net") {
if (is_starboard && is_starboardized_toolchain) {
sources += [
"cert/test_root_certs_builtin.cc",

"base/file_stream_context_posix.cc",
"base/sockaddr_util_posix.cc",
"base/sockaddr_util_posix.h",
"socket/socket_posix.cc",
"socket/socket_posix.h",

"socket/tcp_socket_starboard.cc",
"socket/tcp_socket_starboard.h",
"socket/tcp_socket_posix.cc",
"socket/tcp_socket_posix.h",

"socket/udp_socket_starboard.cc",
"socket/udp_socket_starboard.h",
"socket/udp_socket_posix.cc",
"socket/udp_socket_posix.h",
]
deps += [ "//starboard:starboard_group" ]
}
Expand Down
4 changes: 2 additions & 2 deletions net/base/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ NET_EXPORT extern const base::FeatureParam<base::TimeDelta>
NET_EXPORT BASE_DECLARE_FEATURE(kDocumentReporting);
#endif // BUILDFLAG(ENABLE_REPORTING)

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// When enabled, UDPSocketPosix increments the global counter of bytes received
// every time bytes are received, instead of using a timer to batch updates.
// This should reduce the number of wake ups and improve battery consumption.
// TODO(https://crbug.com/1189805): Cleanup the feature after verifying that it
// doesn't negatively affect performance.
NET_EXPORT BASE_DECLARE_FEATURE(kUdpSocketPosixAlwaysUpdateBytesReceived);
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16

// When this feature is enabled, redirected requests will be considered
// cross-site for the purpose of SameSite cookies if any redirect hop was
Expand Down
8 changes: 4 additions & 4 deletions net/socket/socket_descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

#include "build/build_config.h"

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
#include "starboard/socket.h"
#include "base/notreached.h"
#elif BUILDFLAG(IS_WIN)
#include <ws2tcpip.h>
#include "net/base/winsock_init.h"
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include <sys/socket.h>
#include <sys/types.h>
#endif
Expand All @@ -24,7 +24,7 @@
namespace net {

SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
NOTREACHED();
return kSbSocketInvalid;
#elif BUILDFLAG(IS_WIN)
Expand All @@ -40,7 +40,7 @@ SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) {
}
}
return result;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
SocketDescriptor result = ::socket(family, type, protocol);
#if BUILDFLAG(IS_APPLE)
// Disable SIGPIPE on this socket. Although Chromium globally disables
Expand Down
4 changes: 2 additions & 2 deletions net/socket/socket_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

namespace net {

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
typedef SbSocket SocketDescriptor;
#elif BUILDFLAG(IS_WIN)
typedef UINT_PTR SocketDescriptor;
const SocketDescriptor kInvalidSocket = (SocketDescriptor)(~0);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
typedef int SocketDescriptor;
const SocketDescriptor kInvalidSocket = -1;
#endif
Expand Down
24 changes: 12 additions & 12 deletions net/socket/socket_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include "build/build_config.h"
#include "net/base/net_errors.h"

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
#include "base/notreached.h"
#elif BUILDFLAG(IS_WIN)
#include <winsock2.h>
#include <ws2tcpip.h>
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
Expand All @@ -23,12 +23,12 @@
namespace net {

int SetTCPNoDelay(SocketDescriptor fd, bool no_delay) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSocketSetTcpNoDelay(fd, no_delay) ? OK : ERR_FAILED;
#else
#if BUILDFLAG(IS_WIN)
BOOL on = no_delay ? TRUE : FALSE;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
int on = no_delay ? 1 : 0;
#endif
int rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
Expand All @@ -38,7 +38,7 @@ int SetTCPNoDelay(SocketDescriptor fd, bool no_delay) {
}

int SetReuseAddr(SocketDescriptor fd, bool reuse) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSocketSetReuseAddress(fd, reuse) ? OK : ERR_FAILED;
#else
// SO_REUSEADDR is useful for server sockets to bind to a recently unbound
Expand All @@ -56,7 +56,7 @@ int SetReuseAddr(SocketDescriptor fd, bool reuse) {
// SO_REUSEPORT is provided in MacOS X and iOS.
#if BUILDFLAG(IS_WIN)
BOOL boolean_value = reuse ? TRUE : FALSE;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
int boolean_value = reuse ? 1 : 0;
#endif
int rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
Expand All @@ -67,14 +67,14 @@ int SetReuseAddr(SocketDescriptor fd, bool reuse) {
}

int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSocketSetReceiveBufferSize(fd, size) ? OK : ERR_FAILED;
#else
int rv = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
#if BUILDFLAG(IS_WIN)
int os_error = WSAGetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
int os_error = errno;
#endif
int net_error = (rv == -1) ? MapSystemError(os_error) : OK;
Expand All @@ -86,14 +86,14 @@ int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size) {
}

int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSocketSetSendBufferSize(fd, size) ? OK : ERR_FAILED;
#else
int rv = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
#if BUILDFLAG(IS_WIN)
int os_error = WSAGetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
int os_error = errno;
#endif
int net_error = (rv == -1) ? MapSystemError(os_error) : OK;
Expand All @@ -105,13 +105,13 @@ int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size) {
}

int SetIPv6Only(SocketDescriptor fd, bool ipv6_only) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
NOTREACHED();
return -1;
#else
#if BUILDFLAG(IS_WIN)
DWORD on = ipv6_only ? 1 : 0;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
int on = ipv6_only ? 1 : 0;
#endif
int rv = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
Expand Down
8 changes: 8 additions & 0 deletions net/socket/tcp_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include "net/socket/socket_descriptor.h"

#if defined(STARBOARD)
#if SB_API_VERSION >= 16
#include "net/socket/tcp_socket_posix.h"
#else
#include "net/socket/tcp_socket_starboard.h"
#endif // SB_API_VERSION >= 16
#elif BUILDFLAG(IS_WIN)
#include "net/socket/tcp_socket_win.h"
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand All @@ -26,7 +30,11 @@ namespace net {
// not suitable for your use case (e.g., a socket needs to be created and bound
// before you know whether it is a client or server socket).
#if defined(STARBOARD)
#if SB_API_VERSION >= 16
typedef TCPSocketPosix TCPSocket;
#else
typedef TCPSocketStarboard TCPSocket;
#endif // SB_API_VERSION >= 16
#elif BUILDFLAG(IS_WIN)
typedef TCPSocketWin TCPSocket;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand Down
4 changes: 4 additions & 0 deletions net/socket/tcp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace net {

#if SB_API_VERSION <= 15

TCPSocketStarboard::TCPSocketStarboard(
std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
NetLog* net_log,
Expand Down Expand Up @@ -683,4 +685,6 @@ int TCPSocketStarboard::SetIPv6Only(bool ipv6_only) {
return 0;
}

#endif // SB_API_VERSION <= 15

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

namespace net {

#if SB_API_VERSION <= 15

class NET_EXPORT TCPSocketStarboard : public base::MessagePumpIOStarboard::Watcher {
public:
TCPSocketStarboard(
Expand Down Expand Up @@ -213,6 +215,8 @@ class NET_EXPORT TCPSocketStarboard : public base::MessagePumpIOStarboard::Watch
// DISALLOW_COPY_AND_ASSIGN(TCPSocketStarboard);
};

#endif // SB_API_VERSION <= 15

} // namespace net

#endif // NET_SOCKET_TCP_SOCKET_STARBOARD_H_
8 changes: 8 additions & 0 deletions net/socket/udp_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include "build/build_config.h"

#if defined(STARBOARD)
#if SB_API_VERSION >= 16
#include "net/socket/udp_socket_posix.h"
#else
#include "net/socket/udp_socket_starboard.h"
#endif // SB_API_VERSION >= 16
#elif BUILDFLAG(IS_WIN)
#include "net/socket/udp_socket_win.h"
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand Down Expand Up @@ -38,7 +42,11 @@ namespace net {
// // Writes need to be directed to a specific
// // address.
#if defined(STARBOARD)
#if SB_API_VERSION >= 16
typedef UDPSocketPosix UDPSocket;
#else
typedef UDPSocketStarboard UDPSocket;
#endif // SB_API_VERSION >= 16
#elif BUILDFLAG(IS_WIN)
typedef UDPSocketWin UDPSocket;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand Down
4 changes: 4 additions & 0 deletions net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace net {

#if SB_API_VERSION <= 15

UDPSocketStarboard::UDPSocketStarboard(DatagramSocket::BindType bind_type,
net::NetLog* net_log,
const net::NetLogSource& source)
Expand Down Expand Up @@ -900,4 +902,6 @@ int UDPSocketStarboard::ResetWrittenBytes() {
return bytes;
}

#endif // SB_API_VERSION <= 15

} // namespace net
Loading

0 comments on commit a6ac9da

Please sign in to comment.