Add nullability annotations.

PiperOrigin-RevId: 803587404
This commit is contained in:
Francis Tsui
2025-09-05 13:46:01 -07:00
committed by Copybara-Service
parent 296b135f54
commit 822ce6caf9
4 changed files with 54 additions and 206 deletions
@@ -302,6 +302,7 @@ cc_library(
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"//third_party/webrtc/files/stable/webrtc/rtc_base:threading",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/memory",
@@ -21,6 +21,7 @@
#include <wlanapi.h>
// Standard C/C++ headers
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
@@ -29,6 +30,7 @@
#include <utility>
// Nearby connections headers
#include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
@@ -71,11 +73,10 @@ using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectDevice;
class WifiHotspotSocket : public api::WifiHotspotSocket {
public:
WifiHotspotSocket();
explicit WifiHotspotSocket(std::unique_ptr<NearbyClientSocket> socket);
WifiHotspotSocket(const WifiHotspotSocket&) = default;
explicit WifiHotspotSocket(
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
WifiHotspotSocket(WifiHotspotSocket&&) = default;
~WifiHotspotSocket() override;
WifiHotspotSocket& operator=(const WifiHotspotSocket&) = default;
WifiHotspotSocket& operator=(WifiHotspotSocket&&) = default;
// Returns the InputStream of the WifiHotspotSocket.
@@ -83,54 +84,62 @@ class WifiHotspotSocket : public api::WifiHotspotSocket {
//
// The returned object is not owned by the caller, and can be invalidated once
// the WifiHotspotSocket object is destroyed.
InputStream& GetInputStream() override;
InputStream& GetInputStream() override { return input_stream_; }
// Returns the OutputStream of the WifiHotspotSocket.
// On error, returned stream will report Exception::kIo on any operation.
//
// The returned object is not owned by the caller, and can be invalidated once
// the WifiHotspotSocket object is destroyed.
OutputStream& GetOutputStream() override;
OutputStream& GetOutputStream() override { return output_stream_; }
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
Exception Close() override { return client_socket_->Close(); }
bool Connect(const std::string& ip_address, int port);
bool Connect(const std::string& ip_address, int port) {
return client_socket_->Connect(ip_address, port);
}
private:
// A simple wrapper to handle input stream of socket
class SocketInputStream : public InputStream {
public:
explicit SocketInputStream(NearbyClientSocket* client_socket);
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketInputStream() override = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
ExceptionOr<size_t> Skip(size_t offset) override;
Exception Close() override;
ExceptionOr<ByteArray> Read(std::int64_t size) override {
return client_socket_->Read(size);
}
ExceptionOr<size_t> Skip(size_t offset) override {
return client_socket_->Skip(offset);
}
Exception Close() override { return client_socket_->Close(); }
private:
NearbyClientSocket* client_socket_{nullptr};
NearbyClientSocket* absl_nonnull const client_socket_;
};
// A simple wrapper to handle output stream of socket
class SocketOutputStream : public OutputStream {
public:
explicit SocketOutputStream(NearbyClientSocket* client_socket);
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketOutputStream() override = default;
Exception Write(const ByteArray& data) override;
Exception Flush() override;
Exception Close() override;
Exception Write(const ByteArray& data) override {
return client_socket_->Write(data);
}
Exception Flush() override { return client_socket_->Flush(); }
Exception Close() override { return client_socket_->Close(); }
private:
NearbyClientSocket* client_socket_{nullptr};
NearbyClientSocket* absl_nonnull const client_socket_;
};
// Internal properties
SocketInputStream input_stream_{nullptr};
SocketOutputStream output_stream_{nullptr};
std::unique_ptr<NearbyClientSocket> client_socket_;
absl_nonnull std::unique_ptr<NearbyClientSocket> client_socket_;
SocketInputStream input_stream_;
SocketOutputStream output_stream_;
};
// WifiHotspotServerSocket provides the support to server socket, this server
@@ -146,7 +155,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
std::string GetIPAddress() const override;
int GetPort() const override;
void SetPort(int port) { port_ = port; }
// Blocks until either:
// - at least one incoming connection request is available, or
@@ -170,13 +178,10 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
NearbyServerSocket server_socket_;
private:
static constexpr int kSocketEventsCount = 2;
static constexpr int kSocketEventListen = 0;
static constexpr int kSocketEventClose = 1;
// Retrieves hotspot IP address from local machine
std::string GetHotspotIpAddress() const;
const int port_;
mutable absl::Mutex mutex_;
// Close notifier
@@ -187,7 +192,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
// Cache socket not be picked by upper layer
std::string hotspot_ipaddr_ = {};
int port_ = 0;
bool closed_ = false;
};
@@ -74,35 +74,20 @@ void WifiHotspotServerSocket::SetCloseNotifier(
}
Exception WifiHotspotServerSocket::Close() {
try {
absl::MutexLock lock(&mutex_);
if (closed_) {
return {Exception::kSuccess};
}
server_socket_.Close();
closed_ = true;
if (close_notifier_ != nullptr) {
close_notifier_();
}
LOG(INFO) << __func__ << ": Close completed succesfully.";
absl::MutexLock lock(&mutex_);
if (closed_) {
return {Exception::kSuccess};
} catch (std::exception exception) {
closed_ = true;
LOG(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error &error) {
closed_ = true;
LOG(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
return {Exception::kIo};
} catch (...) {
closed_ = true;
LOG(ERROR) << __func__ << ": Unknown exception.";
return {Exception::kIo};
}
server_socket_.Close();
closed_ = true;
if (close_notifier_ != nullptr) {
close_notifier_();
}
LOG(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess};
}
bool WifiHotspotServerSocket::listen() {
@@ -12,170 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include <cstring>
#include <exception>
#include <memory>
#include <string>
#include <utility>
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "absl/base/nullability.h"
#include "internal/platform/implementation/windows/nearby_client_socket.h"
#include "internal/platform/implementation/windows/wifi_hotspot.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/logging.h"
#include "internal/platform/output_stream.h"
namespace nearby {
namespace windows {
namespace {
int recv_sync(SOCKET s, char* buf, int len, int flags) {
int result;
struct fd_set read_fds;
result = recv(s, buf, len, flags);
if (result >= 0) {
return result;
}
while (WSAGetLastError() == WSAEWOULDBLOCK) {
FD_ZERO(&read_fds);
FD_SET(s, &read_fds);
if ((select(/*nfds=*/0, /*readfds=*/&read_fds, /*writefds=*/nullptr,
/*exceptfds=*/nullptr, /*timeout=*/nullptr) > 0) &&
FD_ISSET(s, &read_fds)) {
result = recv(s, buf, len, flags);
if (result >= 0) {
return result;
}
}
}
return -1;
}
int send_sync(SOCKET s, const char* buf, int len, int flags) {
int result;
struct fd_set write_fds;
result = send(s, buf, len, flags);
if (result >= 0) {
return result;
}
while (WSAGetLastError() == WSAEWOULDBLOCK) {
FD_ZERO(&write_fds);
FD_SET(s, &write_fds);
if ((select(/*nfds=*/0, /*readfds=*/nullptr, /*writefds=*/&write_fds,
/*exceptfds=*/nullptr, /*timeout=*/nullptr) > 0) &&
FD_ISSET(s, &write_fds)) {
result = send(s, buf, len, flags);
if (result >= 0) {
return result;
}
}
}
return -1;
}
} // namespace
WifiHotspotSocket::WifiHotspotSocket() {
client_socket_ = std::make_unique<NearbyClientSocket>();
input_stream_ = SocketInputStream(client_socket_.get());
output_stream_ = SocketOutputStream(client_socket_.get());
}
WifiHotspotSocket::WifiHotspotSocket()
: client_socket_(std::make_unique<NearbyClientSocket>()),
input_stream_(client_socket_.get()),
output_stream_(client_socket_.get()) {}
WifiHotspotSocket::WifiHotspotSocket(
std::unique_ptr<NearbyClientSocket> socket) {
client_socket_ = std::move(socket);
input_stream_ = SocketInputStream(client_socket_.get());
output_stream_ = SocketOutputStream(client_socket_.get());
}
absl_nonnull std::unique_ptr<NearbyClientSocket> socket)
: client_socket_(std::move(socket)),
input_stream_(client_socket_.get()),
output_stream_(client_socket_.get()) {}
WifiHotspotSocket::~WifiHotspotSocket() { Close(); }
InputStream& WifiHotspotSocket::GetInputStream() { return input_stream_; }
OutputStream& WifiHotspotSocket::GetOutputStream() { return output_stream_; }
Exception WifiHotspotSocket::Close() {
if (client_socket_ != nullptr) {
return client_socket_->Close();
}
return {Exception::kSuccess};
}
bool WifiHotspotSocket::Connect(const std::string& ip_address, int port) {
return client_socket_->Connect(ip_address, port);
}
WifiHotspotSocket::SocketInputStream::SocketInputStream(
NearbyClientSocket* client_socket) {
client_socket_ = client_socket;
}
ExceptionOr<ByteArray> WifiHotspotSocket::SocketInputStream::Read(
std::int64_t size) {
if (client_socket_ == nullptr) {
LOG(ERROR) << "Failed to read data due to no client socket.";
return {Exception::kIo};
}
return client_socket_->Read(size);
}
ExceptionOr<size_t> WifiHotspotSocket::SocketInputStream::Skip(size_t offset) {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Skip(offset);
}
Exception WifiHotspotSocket::SocketInputStream::Close() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Close();
}
// SocketOutputStream
WifiHotspotSocket::SocketOutputStream::SocketOutputStream(
NearbyClientSocket* client_socket) {
client_socket_ = client_socket;
}
Exception WifiHotspotSocket::SocketOutputStream::Write(const ByteArray& data) {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Write(data);
}
Exception WifiHotspotSocket::SocketOutputStream::Flush() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Flush();
}
Exception WifiHotspotSocket::SocketOutputStream::Close() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Close();
}
} // namespace windows
} // namespace nearby