Reorganize hotspot header files.

PiperOrigin-RevId: 825127350
This commit is contained in:
Francis Tsui
2025-10-28 11:22:22 -07:00
committed by Copybara-Service
parent 13dd4d7696
commit 5c067f4811
7 changed files with 232 additions and 141 deletions
@@ -228,6 +228,8 @@ cc_library(
"wifi_direct_service.h",
"wifi_hotspot.h",
"wifi_hotspot_native.h",
"wifi_hotspot_server_socket.h",
"wifi_hotspot_socket.h",
"wifi_intel.h",
"wifi_lan.h",
"wifi_lan_mdns.h",
@@ -24,33 +24,27 @@
#include <cstddef>
#include <cstdint>
#include <exception>
#include <list>
#include <memory>
#include <string>
#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"
#include "absl/synchronization/mutex.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/implementation/windows/nearby_client_socket.h"
#include "internal/platform/implementation/windows/nearby_server_socket.h"
#include "internal/platform/implementation/windows/scheduled_executor.h"
#include "internal/platform/implementation/windows/socket_address.h"
#include "internal/platform/implementation/windows/wifi_hotspot_native.h"
#include "internal/platform/implementation/windows/wifi_hotspot_server_socket.h"
// WinRT headers
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.WiFiDirect.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h"
#include "internal/platform/implementation/windows/generated/winrt/base.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/wifi_credential.h"
namespace nearby::windows {
@@ -64,136 +58,6 @@ using ::winrt::Windows::Devices::WiFiDirect::
WiFiDirectConnectionRequestedEventArgs;
using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectDevice;
// WifiHotspotSocket wraps the socket functions to read and write stream.
// In WiFi HOTSPOT, A WifiHotspotSocket will be passed to
// StartAcceptingConnections's callback when Winsock Server Socket receives a
// new connection. When call API to connect to remote WiFi Hotspot service, also
// will return a WifiHotspotSocket to caller.
class WifiHotspotSocket : public api::WifiHotspotSocket {
public:
WifiHotspotSocket();
explicit WifiHotspotSocket(
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
WifiHotspotSocket(WifiHotspotSocket&&) = default;
~WifiHotspotSocket() override;
WifiHotspotSocket& operator=(WifiHotspotSocket&&) = default;
// Returns the InputStream 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.
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 { return output_stream_; }
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override { return client_socket_->Close(); }
bool Connect(const SocketAddress& server_address) {
return client_socket_->Connect(server_address);
}
private:
// A simple wrapper to handle input stream of socket
class SocketInputStream : public InputStream {
public:
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketInputStream() override = default;
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* absl_nonnull const client_socket_;
};
// A simple wrapper to handle output stream of socket
class SocketOutputStream : public OutputStream {
public:
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketOutputStream() override = default;
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* absl_nonnull const 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
// socket accepts connection from clients.
class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
public:
explicit WifiHotspotServerSocket(int port = 0);
WifiHotspotServerSocket(const WifiHotspotServerSocket&) = default;
WifiHotspotServerSocket(WifiHotspotServerSocket&&) = default;
~WifiHotspotServerSocket() override;
WifiHotspotServerSocket& operator=(const WifiHotspotServerSocket&) = default;
WifiHotspotServerSocket& operator=(WifiHotspotServerSocket&&) = default;
std::string GetIPAddress() const override;
int GetPort() const override;
// Blocks until either:
// - at least one incoming connection request is available, or
// - ServerSocket is closed.
// On success, returns connected socket, ready to exchange data.
// Returns nullptr on error.
// Once error is reported, it is permanent, and ServerSocket has to be closed.
std::unique_ptr<api::WifiHotspotSocket> Accept() override;
// Called by the server side of a connection before passing ownership of
// WifiHotspotServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
// Binds to local port
bool Listen(bool dual_stack);
NearbyServerSocket server_socket_;
private:
// Retrieves hotspot IP address from local machine
std::string GetHotspotIpAddress() const;
const int port_;
mutable absl::Mutex mutex_;
// Close notifier
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
// Cache socket not be picked by upper layer
std::string hotspot_ipaddr_ = {};
bool closed_ = false;
};
// Container of operations that can be performed over the WifiHotspot medium.
class WifiHotspotMedium : public api::WifiHotspotMedium {
public:
@@ -40,6 +40,8 @@
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/implementation/windows/wifi_hotspot.h"
#include "internal/platform/implementation/windows/wifi_hotspot_server_socket.h"
#include "internal/platform/implementation/windows/wifi_hotspot_socket.h"
#include "internal/platform/implementation/windows/wifi_intel.h"
#include "internal/platform/logging.h"
#include "internal/platform/wifi_credential.h"
@@ -36,7 +36,8 @@
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "internal/platform/implementation/windows/socket_address.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/implementation/windows/wifi_hotspot.h"
#include "internal/platform/implementation/windows/wifi_hotspot_server_socket.h"
#include "internal/platform/implementation/windows/wifi_hotspot_socket.h"
#include "internal/platform/logging.h"
namespace nearby::windows {
@@ -0,0 +1,97 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SERVER_SOCKET_H_
#define PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SERVER_SOCKET_H_
// Windows headers
#include <windows.h>
#include <winsock2.h>
#include <wlanapi.h>
// Standard C/C++ headers
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
#include <string>
#include <utility>
// Nearby connections headers
#include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/implementation/windows/nearby_server_socket.h"
namespace nearby::windows {
// WifiHotspotServerSocket provides the support to server socket, this server
// socket accepts connection from clients.
class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
public:
explicit WifiHotspotServerSocket(int port = 0);
WifiHotspotServerSocket(const WifiHotspotServerSocket&) = default;
WifiHotspotServerSocket(WifiHotspotServerSocket&&) = default;
~WifiHotspotServerSocket() override;
WifiHotspotServerSocket& operator=(const WifiHotspotServerSocket&) = default;
WifiHotspotServerSocket& operator=(WifiHotspotServerSocket&&) = default;
std::string GetIPAddress() const override;
int GetPort() const override;
// Blocks until either:
// - at least one incoming connection request is available, or
// - ServerSocket is closed.
// On success, returns connected socket, ready to exchange data.
// Returns nullptr on error.
// Once error is reported, it is permanent, and ServerSocket has to be closed.
std::unique_ptr<api::WifiHotspotSocket> Accept() override;
// Called by the server side of a connection before passing ownership of
// WifiHotspotServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
// Binds to local port
bool Listen(bool dual_stack);
NearbyServerSocket server_socket_;
private:
// Retrieves hotspot IP address from local machine
std::string GetHotspotIpAddress() const;
const int port_;
mutable absl::Mutex mutex_;
// Close notifier
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
// Cache socket not be picked by upper layer
std::string hotspot_ipaddr_ = {};
bool closed_ = false;
};
} // namespace nearby::windows
#endif // PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SERVER_SOCKET_H_
@@ -17,7 +17,7 @@
#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/implementation/windows/wifi_hotspot_socket.h"
namespace nearby {
namespace windows {
@@ -0,0 +1,125 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SOCKET_H_
#define PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SOCKET_H_
// Windows headers
#include <windows.h>
#include <winsock2.h>
#include <wlanapi.h>
// Standard C/C++ headers
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
#include <string>
#include <utility>
// Nearby connections headers
#include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/implementation/windows/nearby_client_socket.h"
#include "internal/platform/implementation/windows/socket_address.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
namespace nearby::windows {
// WifiHotspotSocket wraps the socket functions to read and write stream.
// In WiFi HOTSPOT, A WifiHotspotSocket will be passed to
// StartAcceptingConnections's callback when Winsock Server Socket receives a
// new connection. When call API to connect to remote WiFi Hotspot service, also
// will return a WifiHotspotSocket to caller.
class WifiHotspotSocket : public api::WifiHotspotSocket {
public:
WifiHotspotSocket();
explicit WifiHotspotSocket(
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
WifiHotspotSocket(WifiHotspotSocket&&) = default;
~WifiHotspotSocket() override;
WifiHotspotSocket& operator=(WifiHotspotSocket&&) = default;
// Returns the InputStream 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.
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 { return output_stream_; }
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override { return client_socket_->Close(); }
bool Connect(const SocketAddress& server_address) {
return client_socket_->Connect(server_address);
}
private:
// A simple wrapper to handle input stream of socket
class SocketInputStream : public InputStream {
public:
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketInputStream() override = default;
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* absl_nonnull const client_socket_;
};
// A simple wrapper to handle output stream of socket
class SocketOutputStream : public OutputStream {
public:
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
~SocketOutputStream() override = default;
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* absl_nonnull const client_socket_;
};
absl_nonnull std::unique_ptr<NearbyClientSocket> client_socket_;
SocketInputStream input_stream_;
SocketOutputStream output_stream_;
};
} // namespace nearby::windows
#endif // PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_SOCKET_H_