mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
nearbyconnections : Implement WifiLanV2 Advertising functions for /medium, /public(wrapper), /g3.
PiperOrigin-RevId: 405547661
This commit is contained in:
committed by
Copybara-Service
parent
d0da545834
commit
32836a8357
@@ -134,15 +134,62 @@ Exception WifiLanServerSocketV2::Close() {
|
||||
|
||||
Exception WifiLanServerSocketV2::DoClose() { return {Exception::kSuccess}; }
|
||||
|
||||
WifiLanMediumV2::WifiLanMediumV2() {}
|
||||
WifiLanMediumV2::WifiLanMediumV2() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMediumV2(*this);
|
||||
}
|
||||
|
||||
WifiLanMediumV2::~WifiLanMediumV2() {}
|
||||
WifiLanMediumV2::~WifiLanMediumV2() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UnregisterWifiLanMediumV2(*this);
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartAdvertising: Can't start advertising because "
|
||||
"service_type="
|
||||
<< service_type << ", has started already.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
advertising_info_.Add(service_type);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising for service_type="
|
||||
<< service_type;
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -171,6 +218,19 @@ std::unique_ptr<api::WifiLanServerSocketV2> WifiLanMediumV2::ListenForService(
|
||||
return {};
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMediumV2::GetFakeCredentials() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
uint32_t raw_ip_addr = Prng().NextUint32();
|
||||
uint16_t port = Prng().NextUint32();
|
||||
ip_address[0] = static_cast<char>(raw_ip_addr >> 24);
|
||||
ip_address[1] = static_cast<char>(raw_ip_addr >> 16);
|
||||
ip_address[2] = static_cast<char>(raw_ip_addr >> 8);
|
||||
ip_address[3] = static_cast<char>(raw_ip_addr >> 0);
|
||||
|
||||
return std::make_pair(ip_address, port);
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -208,26 +208,26 @@ class WifiLanMediumV2 : public api::WifiLanMediumV2 {
|
||||
int port = 0) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMaxConcurrentAcceptLoops = 5;
|
||||
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
|
||||
std::string service_id;
|
||||
};
|
||||
|
||||
void SetWifiLanService(const NsdServiceInfo& nsd_service_info);
|
||||
std::pair<std::string, int> GetFakeCredentials() const;
|
||||
|
||||
absl::Mutex mutex_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
Reference in New Issue
Block a user