diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-06-18 23:18:18 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-06-18 23:18:18 +0000 |
commit | d670923eae043ad4adcd0df862262da05b7d515e (patch) | |
tree | a915a4a5dc81fd942f3dbc7eff6bcef98df51bad /sysdeps_unix.cpp | |
parent | 24d212e26298c9bcf55a3f5e13370d1ad41fba6f (diff) | |
parent | eef71db2692aa7e0e323c4bb813db8457b82c94e (diff) | |
download | adb-sdk-release.tar.gz |
Snap for 11987826 from eef71db2692aa7e0e323c4bb813db8457b82c94e to sdk-releasesdk-release
Change-Id: I41ad24aa6becd9fb597462ccf5c4413d1224f9bf
Diffstat (limited to 'sysdeps_unix.cpp')
-rw-r--r-- | sysdeps_unix.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/sysdeps_unix.cpp b/sysdeps_unix.cpp index 66757cd9..db7c368b 100644 --- a/sysdeps_unix.cpp +++ b/sysdeps_unix.cpp @@ -18,6 +18,7 @@ #include <sys/utsname.h> +#include <android-base/logging.h> #include <android-base/stringprintf.h> bool set_tcp_keepalive(borrowed_fd fd, int interval_sec) { @@ -107,3 +108,21 @@ std::string GetOSVersion(void) { return android::base::StringPrintf("%s %s (%s)", name.sysname, name.release, name.machine); } + +std::optional<ssize_t> network_peek(borrowed_fd fd) { + ssize_t upper_bound_bytes; +#if defined(__APPLE__) + // Can't use recv(MSG_TRUNC) (not supported). + // Can't use ioctl(FIONREAD) (returns size in socket queue instead next message size). + socklen_t optlen = sizeof(upper_bound_bytes); + if (getsockopt(fd.get(), SOL_SOCKET, SO_NREAD, &upper_bound_bytes, &optlen) == -1) { + upper_bound_bytes = -1; + } +#else + upper_bound_bytes = recv(fd.get(), nullptr, 0, MSG_PEEK | MSG_TRUNC); +#endif + if (upper_bound_bytes == -1) { + PLOG(ERROR) << "network_peek error"; + } + return upper_bound_bytes == -1 ? std::nullopt : std::make_optional(upper_bound_bytes); +}
\ No newline at end of file |