mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Move hotspot credential construction into each platform.
PiperOrigin-RevId: 826272274
This commit is contained in:
committed by
Copybara-Service
parent
03474168b5
commit
ca1ab209b7
@@ -110,6 +110,13 @@ Exception WifiHotspotServerSocket::DoClose() {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
void WifiHotspotServerSocket::PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) {
|
||||
absl::MutexLock lock(mutex_);
|
||||
hotspot_credentials.SetGateway(ip_address_);
|
||||
hotspot_credentials.SetPort(port_);
|
||||
}
|
||||
|
||||
// Code for WifiHotspotMedium
|
||||
WifiHotspotMedium::WifiHotspotMedium() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
@@ -263,9 +270,10 @@ WifiHotspotMedium::ListenForService(int port) {
|
||||
dot_decimal_ip.pop_back();
|
||||
|
||||
server_socket->SetIPAddress(dot_decimal_ip);
|
||||
server_socket->SetPort(port == 0 ? env.GetFakePort() : port);
|
||||
std::string socket_name = WifiHotspotServerSocket::GetName(
|
||||
server_socket->GetIPAddress(), server_socket->GetPort());
|
||||
int port_to_use = port == 0 ? env.GetFakePort() : port;
|
||||
server_socket->SetPort(port_to_use);
|
||||
std::string socket_name =
|
||||
WifiHotspotServerSocket::GetName(dot_decimal_ip, port_to_use);
|
||||
server_socket->SetCloseNotifier([this, socket_name]() {
|
||||
absl::MutexLock lock(mutex_);
|
||||
server_sockets_.erase(socket_name);
|
||||
|
||||
@@ -20,13 +20,13 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/g3/multi_thread_executor.h"
|
||||
#include "internal/platform/implementation/g3/socket_base.h"
|
||||
#include "internal/platform/implementation/wifi_hotspot.h"
|
||||
#include "internal/platform/input_stream.h"
|
||||
#include "internal/platform/output_stream.h"
|
||||
#include "internal/platform/wifi_credential.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
@@ -65,11 +65,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
|
||||
static std::string GetName(absl::string_view ip_address, int port);
|
||||
|
||||
std::string GetIPAddress() const override ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return ip_address_;
|
||||
}
|
||||
|
||||
void SetIPAddress(const std::string& ip_address) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
ip_address_ = ip_address;
|
||||
@@ -113,6 +108,9 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
// Calls close_notifier if it was previously set, and marks socket as closed.
|
||||
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
void PopulateHotspotCredentials(HotspotCredentials& hotspot_credentials)
|
||||
override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
// Retrieves IP addresses from local machine
|
||||
std::vector<std::string> GetIpAddresses() const;
|
||||
|
||||
@@ -51,8 +51,6 @@ class WifiHotspotServerSocket {
|
||||
public:
|
||||
virtual ~WifiHotspotServerSocket() = default;
|
||||
|
||||
virtual std::string GetIPAddress() const = 0;
|
||||
|
||||
virtual int GetPort() const = 0;
|
||||
|
||||
// Blocks until either:
|
||||
@@ -65,6 +63,11 @@ class WifiHotspotServerSocket {
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception Close() = 0;
|
||||
|
||||
// Populates the hotspot credentials with the server socket's service
|
||||
// addresses and ports.
|
||||
virtual void PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) = 0;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiHotspot medium.
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "internal/platform/implementation/windows/wifi_hotspot_server_socket.h"
|
||||
#include "internal/platform/implementation/windows/wifi_hotspot_socket.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/wifi_credential.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
namespace {
|
||||
@@ -51,10 +52,6 @@ WifiHotspotServerSocket::WifiHotspotServerSocket(int port) : port_(port) {}
|
||||
|
||||
WifiHotspotServerSocket::~WifiHotspotServerSocket() { Close(); }
|
||||
|
||||
std::string WifiHotspotServerSocket::GetIPAddress() const {
|
||||
return hotspot_ipaddr_;
|
||||
}
|
||||
|
||||
int WifiHotspotServerSocket::GetPort() const {
|
||||
return server_socket_.GetPort();
|
||||
}
|
||||
@@ -91,6 +88,12 @@ Exception WifiHotspotServerSocket::Close() {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
void WifiHotspotServerSocket::PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) {
|
||||
hotspot_credentials.SetGateway(hotspot_ipaddr_);
|
||||
hotspot_credentials.SetPort(port_);
|
||||
}
|
||||
|
||||
bool WifiHotspotServerSocket::Listen(bool dual_stack) {
|
||||
// Get current IP addresses of the device.
|
||||
int64_t ip_address_max_retries = NearbyFlags::GetInstance().GetInt64Flag(
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/windows/nearby_server_socket.h"
|
||||
#include "internal/platform/wifi_credential.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
@@ -50,7 +51,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
WifiHotspotServerSocket& operator=(const WifiHotspotServerSocket&) = default;
|
||||
WifiHotspotServerSocket& operator=(WifiHotspotServerSocket&&) = default;
|
||||
|
||||
std::string GetIPAddress() const override;
|
||||
int GetPort() const override;
|
||||
|
||||
// Blocks until either:
|
||||
@@ -69,6 +69,9 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
|
||||
void PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) override;
|
||||
|
||||
// Binds to local port
|
||||
bool Listen(bool dual_stack);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user