Skip to content

Commit

Permalink
Use tcp_socket_posix to replace tcp_socket_starboard
Browse files Browse the repository at this point in the history
b/302741384
  • Loading branch information
maxz-lab committed Aug 14, 2024
1 parent d4f1e95 commit 4180280
Show file tree
Hide file tree
Showing 57 changed files with 779 additions and 214 deletions.
8 changes: 4 additions & 4 deletions base/files/file_descriptor_watcher_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class FileDescriptorWatcher::Controller::Watcher
friend class FileDescriptorWatcher;

// MessagePumpForIO::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override;
void OnFileCanWriteWithoutBlocking(int fd) override;
void OnSocketReadyToRead(int fd) override;
void OnSocketReadyToWrite(int fd) override;

// CurrentThread::DestructionObserver:
void WillDestroyCurrentMessageLoop() override;
Expand Down Expand Up @@ -122,7 +122,7 @@ void FileDescriptorWatcher::Controller::Watcher::StartWatching() {
}
}

void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking(
void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToRead(
int fd) {
DCHECK_EQ(fd_, fd);
DCHECK_EQ(MessagePumpForIO::WATCH_READ, mode_);
Expand All @@ -133,7 +133,7 @@ void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking(
FROM_HERE, BindOnce(&Controller::RunCallback, controller_));
}

void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking(
void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToWrite(
int fd) {
DCHECK_EQ(fd_, fd);
DCHECK_EQ(MessagePumpForIO::WATCH_WRITE, mode_);
Expand Down
11 changes: 11 additions & 0 deletions base/files/file_util_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ FilePath MakeAbsoluteFilePath(const FilePath& input) {
return input;
}

bool SetNonBlocking(int fd) {
const int flags = fcntl(fd, F_GETFL);
if (flags == -1)
return false;
if (flags & O_NONBLOCK)
return true;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
return false;
return true;
}

namespace internal {

bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
Expand Down
58 changes: 29 additions & 29 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef HANDLE FileHandle;
#include <mach/mach_time.h>
#include <os/log.h>

#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#if BUILDFLAG(IS_NACL)
#include <sys/time.h> // timespec doesn't seem to be in <time.h>
#endif
Expand All @@ -84,7 +84,7 @@ typedef HANDLE FileHandle;
#include <android/log.h>
#endif

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include <errno.h>
#include <paths.h>
#include <stdio.h>
Expand Down Expand Up @@ -130,7 +130,7 @@ typedef FILE* FileHandle;
#include "base/win/win_util.h"
#endif

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include "base/posix/safe_strerror.h"
#endif

Expand Down Expand Up @@ -293,7 +293,7 @@ base::stack<LogAssertHandlerFunction>& GetLogAssertHandlerStack() {
LogMessageHandlerFunction g_log_message_handler = nullptr;

uint64_t TickCount() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return starboard::CurrentMonotonicTime();
#elif BUILDFLAG(IS_WIN)
return GetTickCount();
Expand All @@ -307,7 +307,7 @@ uint64_t TickCount() {
// NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
// So we have to use clock() for now.
return clock();
#elif BUILDFLAG(IS_POSIX)
#elif BUILDFLAG(IS_POSIX) || SB_API_VERSION >= 16
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);

Expand All @@ -319,21 +319,21 @@ uint64_t TickCount() {
}

void DeleteFilePath(const PathString& log_name) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
unlink(log_name.c_str());
#elif BUILDFLAG(IS_WIN)
DeleteFile(log_name.c_str());
#elif BUILDFLAG(IS_NACL)
// Do nothing; unlink() isn't supported on NaCl.
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
unlink(log_name.c_str());
#else
#error Unsupported platform
#endif
}

PathString GetDefaultLogFile() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
// On Starboard, we politely ask for the log directory, like a civilized
// platform.
std::vector<char> path(kSbFileMaxPath + 1);
Expand All @@ -352,15 +352,15 @@ PathString GetDefaultLogFile() {
log_name.erase(last_backslash + 1);
log_name += FILE_PATH_LITERAL("debug.log");
return log_name;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// On other platforms we just use the current directory.
return PathString("debug.log");
#endif
}

// We don't need locks on Windows for atomically appending to files. The OS
// provides this functionality.
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16

// Provides a lock to synchronize appending to the log file across
// threads. This can be required to support NFS file systems even on OSes that
Expand All @@ -378,7 +378,7 @@ base::Lock& GetLoggingLock() {
return *lock;
}

#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16

// Called by logging functions to ensure that |g_log_file| is initialized
// and can be used for writing. Returns false if the file could not be
Expand All @@ -393,7 +393,7 @@ bool InitializeLogFileHandle() {
g_log_file_name = new PathString(GetDefaultLogFile());
}

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
// This seems to get called a lot with an empty filename, at least in
// base_unittests.
if (g_log_file_name->empty()) {
Expand Down Expand Up @@ -485,7 +485,7 @@ void CloseLogFileUnlocked() {
g_logging_destination &= ~LOG_TO_FILE;
}

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
SbLogPriority LogLevelToStarboardLogPriority(int level) {
switch (level) {
case LOG_INFO:
Expand All @@ -508,7 +508,7 @@ SbLogPriority LogLevelToStarboardLogPriority(int level) {
return kSbLogPriorityInfo;
}
}
#endif // defined(STARBOARD)
#endif // defined(STARBOARD) && SB_API_VERSION <= 15

#if BUILDFLAG(IS_FUCHSIA)
inline FuchsiaLogSeverity LogSeverityToFuchsiaLogSeverity(
Expand All @@ -534,7 +534,7 @@ inline FuchsiaLogSeverity LogSeverityToFuchsiaLogSeverity(
#endif // BUILDFLAG(IS_FUCHSIA)

void WriteToFd(int fd, const char* data, size_t length) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
if (length > 0) {
SbLogRaw(data);
if (data[length - 1] != '\n') {
Expand Down Expand Up @@ -651,7 +651,7 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
if ((g_logging_destination & LOG_TO_FILE) == 0)
return true;

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
base::AutoLock guard(GetLoggingLock());
#endif

Expand Down Expand Up @@ -700,7 +700,7 @@ bool ShouldCreateLogMessage(int severity) {
// set, or only LOG_TO_FILE is set, since that is useful for local development
// and debugging.
bool ShouldLogToStderr(int severity) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
// Don't SbLog to stderr if already logging to system debug log.
return false;
Expand Down Expand Up @@ -854,7 +854,7 @@ LogMessage::~LogMessage() {
}

if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
SbLog(LogLevelToStarboardLogPriority(severity_), str_newline.c_str());
#elif BUILDFLAG(IS_WIN)
OutputDebugStringA(str_newline.c_str());
Expand Down Expand Up @@ -994,15 +994,15 @@ LogMessage::~LogMessage() {
// LOG(ERROR) << "Something went wrong";
// free_something();
// }
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
SbLog(LogLevelToStarboardLogPriority(severity_), str_newline.c_str());
#else
WriteToFd(STDERR_FILENO, str_newline.data(), str_newline.size());
#endif
}

if ((g_logging_destination & LOG_TO_FILE) != 0) {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// If the client app did not call InitLogging() and the lock has not
// been created it will be done now on calling GetLoggingLock(). We do this
// on demand, but if two threads try to do this at the same time, there will
Expand Down Expand Up @@ -1104,7 +1104,7 @@ void LogMessage::Init(const char* file, int line) {
if (g_log_process_id)
stream_ << base::GetUniqueIdForProcess() << ':';
if (g_log_thread_id)
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
// Logging the thread name is added for Starboard logs.
stream_ << base::PlatformThread::GetName() << '/'
<< base::PlatformThread::CurrentId() << ":";
Expand Down Expand Up @@ -1181,17 +1181,17 @@ typedef DWORD SystemErrorCode;
#endif

SystemErrorCode GetLastSystemErrorCode() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSystemGetLastError();
#elif BUILDFLAG(IS_WIN)
return ::GetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
return errno;
#endif
}

BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
const int kErrorMessageBufferSize = 256;
char msgbuf[kErrorMessageBufferSize];

Expand All @@ -1216,7 +1216,7 @@ BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
}
return base::StringPrintf("Error (0x%lX) while retrieving error. (0x%lX)",
GetLastError(), error_code);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
return base::safe_strerror(error_code) +
base::StringPrintf(" (%d)", error_code);
#endif // BUILDFLAG(IS_WIN)
Expand Down Expand Up @@ -1267,7 +1267,7 @@ ErrnoLogMessage::~ErrnoLogMessage() {
#endif // BUILDFLAG(IS_WIN)

void CloseLogFile() {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
base::AutoLock guard(GetLoggingLock());
#endif
CloseLogFileUnlocked();
Expand Down Expand Up @@ -1319,7 +1319,7 @@ ScopedLoggingSettings::ScopedLoggingSettings()
ScopedLoggingSettings::~ScopedLoggingSettings() {
// Re-initialize logging via the normal path. This will clean up old file
// name and handle state, including re-initializing the VLOG internal state.
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
CHECK(InitLogging({
logging_destination_,
log_file_name_ ? log_file_name_->data() : nullptr,
Expand Down Expand Up @@ -1350,7 +1350,7 @@ void ScopedLoggingSettings::SetLogFormat(LogFormat log_format) const {

void RawLog(int level, const char* message) {
if (level >= g_min_log_level && message) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
SbLogRaw(message);
const size_t message_len = strlen(message);
if (message_len > 0 && message[message_len - 1] != '\n') {
Expand Down Expand Up @@ -1424,7 +1424,7 @@ void ScopedVmoduleSwitches::InitWithSwitches(
CHECK(!scoped_vlog_info_);
{
#if (defined(LEAK_SANITIZER) && !BUILDFLAG(IS_NACL)) || \
(defined(STARBOARD) && defined(ADDRESS_SANITIZER))
(defined(STARBOARD) && (SB_API_VERSION <= 15) && defined(ADDRESS_SANITIZER))
// See comments on |g_vlog_info|.
ScopedLeakSanitizerDisabler lsan_disabler;
#endif // defined(LEAK_SANITIZER)
Expand Down
12 changes: 6 additions & 6 deletions base/message_loop/fd_watch_controller_posix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class FdWatchControllerPosixTest : public testing::Test {

class TestHandler : public MessagePumpForIO::FdWatcher {
public:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
watcher_to_delete_ = nullptr;
is_readable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
}
void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
watcher_to_delete_ = nullptr;
is_writable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
Expand Down Expand Up @@ -102,7 +102,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher {
}

// base::WatchableIOMessagePumpPosix::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
// Empty the pipe buffer to reset the event. Otherwise libevent
// implementation of MessageLoop may call the event handler again even if
// |read_closure_| below quits the RunLoop.
Expand All @@ -118,7 +118,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher {
std::move(read_closure_).Run();
}

void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
ASSERT_FALSE(write_closure_.is_null());
std::move(write_closure_).Run();
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher {
ReaderWriterHandler& operator=(const ReaderWriterHandler&) = delete;

// base::WatchableIOMessagePumpPosix::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
if (when_ == kOnReadEvent) {
DoAction();
} else {
Expand All @@ -218,7 +218,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher {
}
}

void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
if (when_ == kOnWriteEvent) {
DoAction();
} else {
Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void MessagePumpFuchsia::FdWatchController::OnZxHandleSignalled(
// work that would touch |this|.
bool* was_stopped = was_stopped_;
if (filtered_events & FDIO_EVT_WRITABLE)
watcher_->OnFileCanWriteWithoutBlocking(fd_);
watcher_->OnSocketReadyToWrite(fd_);
if (!*was_stopped && (filtered_events & FDIO_EVT_READABLE))
watcher_->OnFileCanReadWithoutBlocking(fd_);
watcher_->OnSocketReadyToRead(fd_);

// Don't add additional work here without checking |*was_stopped_| again.
}
Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_glib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,14 @@ void MessagePumpGlib::FdWatchController::NotifyCanRead() {
if (!watcher_)
return;
DCHECK(poll_fd_);
watcher_->OnFileCanReadWithoutBlocking(poll_fd_->fd);
watcher_->OnSocketReadyToRead(poll_fd_->fd);
}

void MessagePumpGlib::FdWatchController::NotifyCanWrite() {
if (!watcher_)
return;
DCHECK(poll_fd_);
watcher_->OnFileCanWriteWithoutBlocking(poll_fd_->fd);
watcher_->OnSocketReadyToWrite(poll_fd_->fd);
}

bool MessagePumpGlib::WatchFileDescriptor(int fd,
Expand Down
Loading

0 comments on commit 4180280

Please sign in to comment.