Add GetDynamicPortRange api for Wifilan medium

PiperOrigin-RevId: 416669312
This commit is contained in:
edwinwu
2021-12-21 14:47:49 -08:00
committed by hai007
parent d29b05c367
commit 2fc61869f9
7 changed files with 51 additions and 2 deletions
+22 -2
View File
@@ -227,9 +227,16 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id,
return false;
}
// We can generate an exact port here on server socket; now we just assign 0
// to let platform medium decide it.
auto port_range = medium_.GetDynamicPortRange();
// Generate an exact port here on server socket; if platform doesn't provide
// range of port then assign 0 to let platform decide it.
int port = 0;
if (port_range.has_value() &&
(port_range->first > 0 && port_range->first <= 65535 &&
port_range->second > 0 && port_range->second <= 65535 &&
port_range->first <= port_range->second)) {
port = GeneratePort(service_id, port_range.value());
}
WifiLanServerSocket server_socket = medium_.ListenForService(port);
if (!server_socket.IsValid()) {
NEARBY_LOGS(INFO)
@@ -404,6 +411,19 @@ std::string WifiLan::GenerateServiceType(const std::string& service_id) {
service_id_hash_string);
}
int WifiLan::GeneratePort(const std::string& service_id,
std::pair<std::int32_t, std::int32_t> port_range) {
const std::string service_id_hash =
std::string(Utils::Sha256Hash(service_id, 4));
std::uint32_t uint_of_service_id_hash =
service_id_hash[0] << 24 | service_id_hash[1] << 16 |
service_id_hash[2] << 8 | service_id_hash[3];
return port_range.first +
(uint_of_service_id_hash % (port_range.second - port_range.first));
}
} // namespace connections
} // namespace nearby
} // namespace location
+4
View File
@@ -170,6 +170,10 @@ class WifiLan {
// Generates mDNS type.
std::string GenerateServiceType(const std::string& service_id);
// Generates port number based on port_range_.
int GeneratePort(const std::string& service_id,
std::pair<std::int32_t, std::int32_t> port_range);
mutable Mutex mutex_;
WifiLanMedium medium_ ABSL_GUARDED_BY(mutex_);
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
+4
View File
@@ -144,6 +144,10 @@ class WifiLanMedium {
// On error, returns nullptr.
virtual std::unique_ptr<WifiLanServerSocket> ListenForService(
int port = 0) = 0;
// Returns the port range as a pair of min and max port.
virtual absl::optional<std::pair<std::int32_t, std::int32_t>>
GetDynamicPortRange() = 0;
};
} // namespace api
+6
View File
@@ -233,6 +233,12 @@ class WifiLanMedium : public api::WifiLanMedium {
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns the port range as a pair of min and max port.
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
override {
return std::make_pair(49152, 65535);
}
private:
struct AdvertisingInfo {
bool Empty() const { return service_types.empty(); }
@@ -155,6 +155,10 @@ class WifiLanMedium : public api::WifiLanMedium {
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override
ABSL_LOCKS_EXCLUDED(mutex_);
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange() override {
return absl::nullopt;
}
private:
struct AdvertisingInfo {
bool Empty() const { return services.empty(); }
+6
View File
@@ -30,6 +30,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/optional.h"
#include "platform/api/wifi_lan.h"
#include "platform/base/exception.h"
#include "platform/base/input_stream.h"
@@ -247,6 +248,11 @@ class WifiLanMedium : public api::WifiLanMedium {
// continue.
void NotifyDnsServiceUnregistered(DWORD status);
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
override {
return absl::nullopt;
}
private:
// mDNS text attributes
static constexpr std::string_view KEY_ENDPOINT_INFO = "n";
+5
View File
@@ -187,6 +187,11 @@ class WifiLanMedium {
return WifiLanServerSocket(impl_->ListenForService(port));
}
// Returns the port range as a pair of min and max port.
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange() {
return impl_->GetDynamicPortRange();
}
bool IsValid() const { return impl_ != nullptr; }
api::WifiLanMedium& GetImpl() { return *impl_; }