Skip to content

Commit

Permalink
Use socket_starboard on Win32
Browse files Browse the repository at this point in the history
Detect "_MSC_VER" on win32/xbox.

b/302741384
  • Loading branch information
maxz-lab committed Oct 15, 2024
1 parent 61fca10 commit c674799
Show file tree
Hide file tree
Showing 44 changed files with 172 additions and 152 deletions.
34 changes: 17 additions & 17 deletions base/message_loop/message_pump_io_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "base/message_loop/message_pump_io_starboard.h"

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
Expand All @@ -34,7 +34,7 @@ namespace base {
MessagePumpIOStarboard::SocketWatcher::SocketWatcher(const Location& from_here)
: created_from_location_(from_here),
interests_(kSbSocketWaiterInterestNone),
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
socket_(-1),
#else
socket_(kSbSocketInvalid),
Expand All @@ -44,7 +44,7 @@ MessagePumpIOStarboard::SocketWatcher::SocketWatcher(const Location& from_here)
weak_factory_(this) {}

MessagePumpIOStarboard::SocketWatcher::~SocketWatcher() {
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
if (socket_ >= 0) {
StopWatchingFileDescriptor();
}
Expand All @@ -55,7 +55,7 @@ MessagePumpIOStarboard::SocketWatcher::~SocketWatcher() {
#endif
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool MessagePumpIOStarboard::SocketWatcher::StopWatchingFileDescriptor() {
watcher_ = nullptr;
interests_ = kSbSocketWaiterInterestNone;
Expand Down Expand Up @@ -94,7 +94,7 @@ bool MessagePumpIOStarboard::SocketWatcher::StopWatchingSocket() {
}
#endif

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void MessagePumpIOStarboard::SocketWatcher::Init(int socket,
bool persistent) {
DCHECK(socket >= 0);
Expand All @@ -109,7 +109,7 @@ void MessagePumpIOStarboard::SocketWatcher::Init(SbSocket socket,
persistent_ = persistent;
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
int MessagePumpIOStarboard::SocketWatcher::Release() {
int socket = socket_;
socket_ = -1;
Expand All @@ -123,7 +123,7 @@ SbSocket MessagePumpIOStarboard::SocketWatcher::Release() {
}
#endif

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void MessagePumpIOStarboard::SocketWatcher::OnFileCanReadWithoutBlocking(
int socket,
MessagePumpIOStarboard* pump) {
Expand Down Expand Up @@ -176,7 +176,7 @@ MessagePumpIOStarboard::~MessagePumpIOStarboard() {
SbSocketWaiterDestroy(waiter_);
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool MessagePumpIOStarboard::WatchFileDescriptor(int socket,
bool persistent,
int mode,
Expand Down Expand Up @@ -206,7 +206,7 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket,
interests |= kSbSocketWaiterInterestWrite;
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
int old_socket = controller->Release();
if (old_socket >= 0) {
#else
Expand All @@ -229,23 +229,23 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket,
interests |= old_interest_mask;

// Must disarm the event before we can reuse it.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
SbPosixSocketWaiterRemove(waiter_, old_socket);
#else
SbSocketWaiterRemove(waiter_, old_socket);
#endif // SB_API_VERSION >= 16
#endif // SB_API_VERSION >= 16 && !defined(_MSC_VER)
}

// Set current interest mask and waiter for this event.
bool result = false;
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
result = SbPosixSocketWaiterAdd(waiter_, socket, controller,
OnPosixSocketWaiterNotification, interests, persistent);

#else
result = SbSocketWaiterAdd(waiter_, socket, controller,
OnSocketWaiterNotification, interests, persistent);
#endif // SB_API_VERSION >= 16
#endif // SB_API_VERSION >= 16 && !defined(_MSC_VER)
if (result == false) {
return false;
}
Expand All @@ -257,15 +257,15 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket,
return true;
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool MessagePumpIOStarboard::StopWatchingFileDescriptor(int socket) {
return SbPosixSocketWaiterRemove(waiter_, socket);
}
#else
bool MessagePumpIOStarboard::StopWatching(SbSocket socket) {
return SbSocketWaiterRemove(waiter_, socket);
}
#endif // SB_API_VERSION >= 16
#endif // SB_API_VERSION >= 16 && !defined(_MSC_VER)

void MessagePumpIOStarboard::AddIOObserver(IOObserver* obs) {
io_observers_.AddObserver(obs);
Expand Down Expand Up @@ -358,7 +358,7 @@ void MessagePumpIOStarboard::DidProcessIOEvent() {
}
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)

// static
void MessagePumpIOStarboard::OnPosixSocketWaiterNotification(SbSocketWaiter waiter,
Expand Down Expand Up @@ -415,5 +415,5 @@ void MessagePumpIOStarboard::OnSocketWaiterNotification(SbSocketWaiter waiter,
}
}

#endif // SB_API_VERSION >= 16
#endif // SB_API_VERSION >= 16 && !defined(_MSC_VER)
} // namespace base
14 changes: 7 additions & 7 deletions base/message_loop/message_pump_io_starboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
public:
// These methods are called from MessageLoop::Run when a socket can be
// interacted with without blocking.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
virtual void OnFileCanReadWithoutBlocking(int socket) {}
virtual void OnFileCanWriteWithoutBlocking(int socket) {}
#else
Expand All @@ -77,7 +77,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {

// Stops watching the socket, always safe to call. No-op if there's nothing
// to do.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool StopWatchingFileDescriptor();
#else
bool StopWatchingSocket();
Expand All @@ -90,7 +90,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
friend class MessagePumpIOStarboardTest;

// Called by MessagePumpIOStarboard.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void Init(int socket, bool persistent);
int Release();
#else
Expand All @@ -106,7 +106,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {

void set_watcher(Watcher* watcher) { watcher_ = watcher; }

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanReadWithoutBlocking(int socket, MessagePumpIOStarboard* pump);
void OnFileCanWriteWithoutBlocking(int socket, MessagePumpIOStarboard* pump);
#else
Expand All @@ -116,7 +116,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {

const Location created_from_location_;
int interests_;
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
int socket_;
#else
SbSocket socket_;
Expand Down Expand Up @@ -148,7 +148,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
// If an error occurs while calling this method in a cumulative fashion, the
// event previously attached to |controller| is aborted. Returns true on
// success. Must be called on the same thread the message_pump is running on.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool WatchFileDescriptor(int socket,
bool persistent,
int mode,
Expand Down Expand Up @@ -185,7 +185,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {

// Called by SbSocketWaiter to tell us a registered socket can be read and/or
// written to.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
static void OnPosixSocketWaiterNotification(SbSocketWaiter waiter,
int socket,
void* context,
Expand Down
34 changes: 17 additions & 17 deletions base/message_loop/message_pump_io_starboard_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <memory>

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -48,7 +48,7 @@ class MessagePumpIOStarboardTest : public testing::Test {
void SetUp() override {
Thread::Options options(MessagePumpType::IO, 0);
ASSERT_TRUE(io_thread_.StartWithOptions(std::move(options)));
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#else
socket_ = SbSocketCreate(SbSocketAddressType::kSbSocketAddressTypeIpv4, SbSocketProtocol::kSbSocketProtocolTcp);
Expand All @@ -62,7 +62,7 @@ class MessagePumpIOStarboardTest : public testing::Test {
// pipe.
io_thread_.Stop();

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
close(socket_);
#else
SbSocketDestroy(socket_);
Expand All @@ -73,7 +73,7 @@ class MessagePumpIOStarboardTest : public testing::Test {
return std::make_unique<MessagePumpIOStarboard>();
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
int socket() {
return socket_;
}
Expand All @@ -88,7 +88,7 @@ class MessagePumpIOStarboardTest : public testing::Test {
}

void SimulateIOEvent(MessagePumpIOStarboard::SocketWatcher* controller) {
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
MessagePumpIOStarboard::OnPosixSocketWaiterNotification(nullptr,
-1,
controller,
Expand All @@ -107,7 +107,7 @@ class MessagePumpIOStarboardTest : public testing::Test {

private:
Thread io_thread_;
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
int socket_;
#else
SbSocket socket_;
Expand All @@ -123,7 +123,7 @@ class StupidWatcher : public MessagePumpIOStarboard::Watcher {
~StupidWatcher() override = default;

// MessagePumpIOStarboard::Watcher interface
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanReadWithoutBlocking(int socket) override {}
void OnFileCanWriteWithoutBlocking(int socket) override {}
#else
Expand All @@ -144,7 +144,7 @@ class BaseWatcher : public MessagePumpIOStarboard::Watcher {
~BaseWatcher() override = default;

// MessagePumpIOStarboard::Watcher interface
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanReadWithoutBlocking(int socket) override { NOTREACHED(); }
void OnFileCanWriteWithoutBlocking(int socket) override { NOTREACHED(); }
#else
Expand All @@ -165,7 +165,7 @@ class DeleteWatcher : public BaseWatcher {
return controller_.get();
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanWriteWithoutBlocking(int socket) override {
#else
void OnSocketReadyToWrite(SbSocket socket) override {
Expand All @@ -183,7 +183,7 @@ TEST_F(MessagePumpIOStarboardTest, DISABLED_DeleteWatcher) {
DeleteWatcher delegate(
std::make_unique<MessagePumpIOStarboard::SocketWatcher>(FROM_HERE));
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
pump->WatchFileDescriptor(socket(),
#else
pump->Watch(socket(),
Expand All @@ -202,7 +202,7 @@ class StopWatcher : public BaseWatcher {

~StopWatcher() override = default;

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanWriteWithoutBlocking(int socket) override {
controller_->StopWatchingFileDescriptor();
}
Expand All @@ -222,7 +222,7 @@ TEST_F(MessagePumpIOStarboardTest, DISABLED_StopWatcher) {
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
MessagePumpIOStarboard::SocketWatcher controller(FROM_HERE);
StopWatcher delegate(&controller);
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
pump->WatchFileDescriptor(socket(),
#else
pump->Watch(socket(),
Expand All @@ -248,7 +248,7 @@ class NestedPumpWatcher : public MessagePumpIOStarboard::Watcher {
NestedPumpWatcher() = default;
~NestedPumpWatcher() override = default;

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanReadWithoutBlocking(int socket) override {
#else
void OnSocketReadyToRead(SbSocket socket) override {
Expand All @@ -259,7 +259,7 @@ class NestedPumpWatcher : public MessagePumpIOStarboard::Watcher {
runloop.Run();
}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanWriteWithoutBlocking(int socket) override {}
};
#else
Expand All @@ -272,7 +272,7 @@ TEST_F(MessagePumpIOStarboardTest, DISABLED_NestedPumpWatcher) {
NestedPumpWatcher delegate;
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
MessagePumpIOStarboard::SocketWatcher controller(FROM_HERE);
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
pump->WatchFileDescriptor(socket(),
#else
pump->Watch(socket(),
Expand All @@ -293,7 +293,7 @@ class QuitWatcher : public BaseWatcher {
QuitWatcher(base::OnceClosure quit_closure)
: quit_closure_(std::move(quit_closure)) {}

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
void OnFileCanReadWithoutBlocking(int socket) override {
#else
void OnSocketReadyToRead(SbSocket socket) override {
Expand Down Expand Up @@ -331,7 +331,7 @@ TEST_F(MessagePumpIOStarboardTest, DISABLED_QuitWatcher) {
std::unique_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher);

// Tell the pump to watch the pipe.
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
pump->WatchFileDescriptor(socket(),
#else
pump->Watch(socket(),
Expand Down
2 changes: 1 addition & 1 deletion base/task/current_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ MessagePumpForIO* CurrentIOThread::GetMessagePumpForIO() const {
#if !BUILDFLAG(IS_NACL)

#if defined(STARBOARD)
#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool CurrentIOThread::WatchFileDescriptor(int socket,
bool persistent,
int mode,
Expand Down
2 changes: 1 addition & 1 deletion base/task/current_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class BASE_EXPORT CurrentIOThread : public CurrentThread {
WATCH_WRITE = base::MessagePumpIOStarboard::WATCH_WRITE,
WATCH_READ_WRITE = base::MessagePumpIOStarboard::WATCH_READ_WRITE};

#if SB_API_VERSION >= 16
#if SB_API_VERSION >= 16 && !defined(_MSC_VER)
bool WatchFileDescriptor(int socket,
bool persistent,
int mode,
Expand Down
7 changes: 1 addition & 6 deletions cobalt/webdriver/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,7 @@ WebDriverServer::WebDriverServer(int port, const std::string& listen_ip,
// Create http server
std::unique_ptr<net::ServerSocket> server_socket =
std::make_unique<net::TCPServerSocket>(nullptr, net::NetLogSource());
int rv =
server_socket->ListenWithAddressAndPort(listen_ip, port, 1 /*backlog*/);
if (rv != 0) {
LOG(ERROR) << "Listen WebDriver failed " << rv << " errno " << errno;
return;
}
server_socket->ListenWithAddressAndPort(listen_ip, port, 1 /*backlog*/);
server_ = std::make_unique<net::HttpServer>(std::move(server_socket), this);
net::IPEndPoint ip_addr;
int result = server_->GetLocalInterfaceAddress(&ip_addr);
Expand Down
2 changes: 1 addition & 1 deletion net/base/address_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ AddressFamily GetAddressFamily(const IPAddress& address) {
}
}

#if defined(STARBOARD) && SB_API_VERSION <= 15
#if defined(STARBOARD) && (SB_API_VERSION <= 15 || defined(_MSC_VER))
SbSocketAddressType ConvertAddressFamily(AddressFamily address_family) {
switch (address_family) {
case ADDRESS_FAMILY_IPV4:
Expand Down
Loading

0 comments on commit c674799

Please sign in to comment.