mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'master' into release to roll forward to cl/343785060.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/prng.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
|
||||
@@ -72,7 +73,7 @@ Exception WifiLanSocket::Close() {
|
||||
|
||||
WifiLanService* WifiLanSocket::GetRemoteWifiLanService() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return service_;
|
||||
return wifi_lan_service_;
|
||||
}
|
||||
|
||||
void WifiLanSocket::DoClose() {
|
||||
@@ -101,7 +102,7 @@ OutputStream& WifiLanSocket::GetLocalOutputStream() {
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept(
|
||||
WifiLanService* service) {
|
||||
WifiLanService* wifi_lan_service) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) return {};
|
||||
while (pending_sockets_.empty()) {
|
||||
@@ -112,7 +113,7 @@ std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept(
|
||||
auto* remote_socket =
|
||||
pending_sockets_.extract(pending_sockets_.begin()).value();
|
||||
CHECK(remote_socket);
|
||||
auto local_socket = std::make_unique<WifiLanSocket>(service);
|
||||
auto local_socket = std::make_unique<WifiLanSocket>(wifi_lan_service);
|
||||
local_socket->Connect(*remote_socket);
|
||||
remote_socket->Connect(*local_socket);
|
||||
cond_.SignalAll();
|
||||
@@ -170,22 +171,13 @@ Exception WifiLanServerSocket::DoClose() {
|
||||
}
|
||||
|
||||
WifiLanMedium::WifiLanMedium() {
|
||||
service_.SetMedium(this);
|
||||
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);
|
||||
service_.SetServiceAddress(ip_address, port);
|
||||
wifi_lan_service_.SetMedium(this);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMedium(*this);
|
||||
}
|
||||
|
||||
WifiLanMedium::~WifiLanMedium() {
|
||||
service_.SetMedium(nullptr);
|
||||
wifi_lan_service_.SetMedium(nullptr);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UnregisterWifiLanMedium(*this);
|
||||
|
||||
@@ -205,15 +197,17 @@ WifiLanMedium::~WifiLanMedium() {
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
const std::string& service_info_name,
|
||||
const std::string& endpoint_info_name) {
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, service_info_name=%s",
|
||||
service_id.c_str(), service_info_name.c_str());
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, nsd_service_info=%p, "
|
||||
"service_info_name=%s",
|
||||
service_id.c_str(), &nsd_service_info,
|
||||
nsd_service_info.GetServiceInfoName().c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
service_.SetServiceName(service_info_name);
|
||||
service_.SetTxtRecord("n", endpoint_info_name);
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, true);
|
||||
NsdServiceInfo local_nsd_service_info{nsd_service_info};
|
||||
SetWifiLanService(nsd_service_info);
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id,
|
||||
true);
|
||||
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (server_socket_ != nullptr) server_socket_.release();
|
||||
@@ -223,7 +217,7 @@ bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
accept_loops_runner_.Execute([&env, this, service_id]() mutable {
|
||||
if (!accept_loops_runner_.InShutdown()) {
|
||||
while (true) {
|
||||
auto client_socket = server_socket_->Accept(&service_);
|
||||
auto client_socket = server_socket_->Accept(&wifi_lan_service_);
|
||||
if (client_socket == nullptr) break;
|
||||
env.CallWifiLanAcceptedConnectionCallback(
|
||||
*this, *(client_socket.release()), service_id);
|
||||
@@ -250,7 +244,8 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
}
|
||||
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, false);
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id,
|
||||
false);
|
||||
accept_loops_runner_.Shutdown();
|
||||
if (server_socket_ == nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan StopAdvertising: failed to find WifiLan "
|
||||
@@ -322,39 +317,46 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
api::WifiLanService& remote_service, const std::string& service_id) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect: medium=%p, service=%p, service_info_name=%s, "
|
||||
"service_id=%s",
|
||||
this, &service_, remote_service.GetServiceName().c_str(),
|
||||
service_id.c_str());
|
||||
api::WifiLanService& remote_wifi_lan_service,
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"G3 WifiLan Connect: medium=%p, wifi_lan_service=%p, "
|
||||
"service_info_name=%s, service_id=%s",
|
||||
this, &wifi_lan_service_,
|
||||
remote_wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str(),
|
||||
service_id.c_str());
|
||||
// First, find an instance of remote medium, that exposed this service.
|
||||
auto* medium = static_cast<WifiLanService&>(remote_service).GetMedium();
|
||||
auto* remote_medium =
|
||||
static_cast<WifiLanService&>(remote_wifi_lan_service).GetMedium();
|
||||
|
||||
if (!medium) return {}; // Can't find medium. Bail out.
|
||||
if (!remote_medium) return {}; // Can't find medium. Bail out.
|
||||
|
||||
WifiLanServerSocket* remote_server_socket = nullptr;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect [peer]: medium=%p, service=%p, "
|
||||
"service_info_name=%s, service_id=%s",
|
||||
medium, &remote_service, remote_service.GetServiceName().c_str(),
|
||||
service_id.c_str());
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"G3 WifiLan Connect [peer]: remote_wifi_lan_service=%p, "
|
||||
"remote_service_info_name=%s, service_id=%s",
|
||||
&remote_wifi_lan_service,
|
||||
remote_wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str(),
|
||||
service_id.c_str());
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&medium->mutex_);
|
||||
remote_server_socket = medium->server_socket_.get();
|
||||
absl::MutexLock medium_lock(&remote_medium->mutex_);
|
||||
remote_server_socket = remote_medium->server_socket_.get();
|
||||
if (remote_server_socket == nullptr) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"G3 WifiLan Connect: Failed to find WifiLan Server socket: "
|
||||
"service_id=%s",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "G3 WifiLan Connect: Failed to find remote WifiLan Server socket: "
|
||||
"service_id="
|
||||
<< service_id;
|
||||
// Fall through for server socket not found.
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
WifiLanService service = static_cast<WifiLanService&>(remote_service);
|
||||
auto socket = std::make_unique<WifiLanSocket>(&service);
|
||||
WifiLanService wifi_lan_service =
|
||||
static_cast<WifiLanService&>(remote_wifi_lan_service);
|
||||
auto socket = std::make_unique<WifiLanSocket>(&wifi_lan_service);
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!remote_server_socket->Connect(*socket)) {
|
||||
NEARBY_LOG(ERROR,
|
||||
@@ -368,17 +370,38 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
return socket;
|
||||
}
|
||||
|
||||
api::WifiLanService* WifiLanMedium::FindRemoteService(
|
||||
api::WifiLanService* WifiLanMedium::GetRemoteService(
|
||||
const std::string& ip_address, int port) {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
return env.FindWifiLanService(ip_address, port);
|
||||
return env.GetWifiLanService(ip_address, port);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan GetServiceAddress: service_id="
|
||||
<< service_id;
|
||||
return service_.GetServiceAddress();
|
||||
return wifi_lan_service_.GetServiceInfo().GetServiceAddress();
|
||||
}
|
||||
|
||||
void WifiLanMedium::SetWifiLanService(const NsdServiceInfo& nsd_service_info) {
|
||||
NsdServiceInfo local_nsd_service_info{nsd_service_info};
|
||||
auto service_address = GetFakeServiceAddress();
|
||||
local_nsd_service_info.SetServiceAddress(service_address.first,
|
||||
service_address.second);
|
||||
wifi_lan_service_.SetServiceInfo(local_nsd_service_info);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetFakeServiceAddress() 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
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/output_stream.h"
|
||||
#include "platform/impl/g3/multi_thread_executor.h"
|
||||
#include "platform/impl/g3/pipe.h"
|
||||
@@ -35,39 +36,18 @@ namespace g3 {
|
||||
|
||||
class WifiLanMedium;
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains packed
|
||||
// |WifiLanServiceInfo| string name.
|
||||
// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|.
|
||||
class WifiLanService : public api::WifiLanService {
|
||||
public:
|
||||
explicit WifiLanService(std::string service_info_name)
|
||||
: service_info_name_(std::move(service_info_name)) {}
|
||||
WifiLanService() = default;
|
||||
explicit WifiLanService(NsdServiceInfo nsd_service_info)
|
||||
: nsd_service_info_(std::move(nsd_service_info)) {}
|
||||
~WifiLanService() override = default;
|
||||
|
||||
std::string GetServiceName() const override { return service_info_name_; }
|
||||
NsdServiceInfo GetServiceInfo() const override { return nsd_service_info_; }
|
||||
|
||||
void SetServiceName(std::string service_info_name) {
|
||||
service_info_name_ = std::move(service_info_name);
|
||||
}
|
||||
|
||||
std::string GetTxtRecord(const std::string& txt_record_key) const override {
|
||||
if (txt_records_.empty()) return {};
|
||||
auto record = txt_records_.find(txt_record_key);
|
||||
if (record == txt_records_.end()) return {};
|
||||
return record->second;
|
||||
}
|
||||
|
||||
void SetTxtRecord(const std::string& txt_record_key,
|
||||
const std::string& txt_record_value) {
|
||||
txt_records_.emplace(txt_record_key, txt_record_value);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> GetServiceAddress() const override {
|
||||
return std::make_pair(ip_address_, port_);
|
||||
}
|
||||
|
||||
void SetServiceAddress(const std::string& ip_address, int port) {
|
||||
ip_address_ = ip_address;
|
||||
port_ = port;
|
||||
void SetServiceInfo(NsdServiceInfo nsd_service_info) {
|
||||
nsd_service_info_ = std::move(nsd_service_info);
|
||||
}
|
||||
|
||||
WifiLanMedium* GetMedium() { return medium_; }
|
||||
@@ -75,17 +55,15 @@ class WifiLanService : public api::WifiLanService {
|
||||
void SetMedium(WifiLanMedium* medium) { medium_ = medium; }
|
||||
|
||||
private:
|
||||
std::string service_info_name_;
|
||||
absl::flat_hash_map<std::string, std::string> txt_records_;
|
||||
NsdServiceInfo nsd_service_info_;
|
||||
WifiLanMedium* medium_ = nullptr;
|
||||
std::string ip_address_;
|
||||
int port_;
|
||||
};
|
||||
|
||||
class WifiLanSocket : public api::WifiLanSocket {
|
||||
public:
|
||||
WifiLanSocket() = default;
|
||||
explicit WifiLanSocket(WifiLanService* service) : service_(service) {}
|
||||
explicit WifiLanSocket(WifiLanService* wifi_lan_service)
|
||||
: wifi_lan_service_(wifi_lan_service) {}
|
||||
~WifiLanSocket() override;
|
||||
|
||||
// Connect to another WifiLanSocket, to form a functional low-level channel.
|
||||
@@ -138,7 +116,7 @@ class WifiLanSocket : public api::WifiLanSocket {
|
||||
std::shared_ptr<Pipe> output_{new Pipe};
|
||||
std::shared_ptr<Pipe> input_;
|
||||
mutable absl::Mutex mutex_;
|
||||
WifiLanService* service_;
|
||||
WifiLanService* wifi_lan_service_;
|
||||
WifiLanSocket* remote_socket_ ABSL_GUARDED_BY(mutex_) = nullptr;
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
@@ -196,8 +174,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
~WifiLanMedium() override;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& service_info_name,
|
||||
const std::string& endpoint_info_name) override
|
||||
const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising(const std::string& service_id) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -226,11 +203,11 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanSocket> Connect(
|
||||
api::WifiLanService& remote_service,
|
||||
api::WifiLanService& remote_wifi_lan_service,
|
||||
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
api::WifiLanService* FindRemoteService(const std::string& ip_address,
|
||||
int port) override;
|
||||
api::WifiLanService* GetRemoteService(const std::string& ip_address,
|
||||
int port) override;
|
||||
|
||||
std::pair<std::string, int> GetServiceAddress(
|
||||
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -252,8 +229,11 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
std::string service_id;
|
||||
};
|
||||
|
||||
void SetWifiLanService(const NsdServiceInfo& nsd_service_info);
|
||||
std::pair<std::string, int> GetFakeServiceAddress() const;
|
||||
|
||||
absl::Mutex mutex_;
|
||||
WifiLanService service_{"unknown G3 WifiLan service"};
|
||||
WifiLanService wifi_lan_service_;
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAdvertising().
|
||||
|
||||
Reference in New Issue
Block a user