mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Support blocking socket in Wi-Fi LAN medium
PiperOrigin-RevId: 717979860
This commit is contained in:
committed by
Copybara-Service
parent
951f3b194d
commit
4b8ea5db74
@@ -17,13 +17,17 @@
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/flags/nearby_platform_feature_flags.h"
|
||||
#include "internal/platform/implementation/wifi_lan.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
|
||||
#include "internal/platform/implementation/windows/nearby_server_socket.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/implementation/windows/wifi_lan.h"
|
||||
#include "internal/platform/logging.h"
|
||||
@@ -36,32 +40,44 @@ using ::winrt::Windows::Networking::Sockets::SocketQualityOfService;
|
||||
|
||||
}
|
||||
|
||||
WifiLanServerSocket::WifiLanServerSocket(int port) : port_(port) {}
|
||||
WifiLanServerSocket::WifiLanServerSocket(int port) : port_(port) {
|
||||
enable_blocking_socket_ = NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::platform::config_package_nearby::nearby_platform_feature::
|
||||
kEnableBlockingSocket);
|
||||
}
|
||||
|
||||
WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
|
||||
|
||||
// Returns the first IP address.
|
||||
std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
LOG(ERROR) << "Failed to get IP address due to no server socket.";
|
||||
return "";
|
||||
}
|
||||
if (enable_blocking_socket_) {
|
||||
return ipaddr_dotdecimal_to_4bytes_string(server_socket_.GetIPAddress());
|
||||
} else {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
LOG(ERROR) << "Failed to get IP address due to no server socket.";
|
||||
return "";
|
||||
}
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
LOG(ERROR) << "Failed to get IP address due to no avaible IP addresses.";
|
||||
return "";
|
||||
}
|
||||
if (ip_addresses_.empty()) {
|
||||
LOG(ERROR) << "Failed to get IP address due to no avaible IP addresses.";
|
||||
return "";
|
||||
}
|
||||
|
||||
return ip_addresses_.front();
|
||||
return ip_addresses_.front();
|
||||
}
|
||||
}
|
||||
|
||||
// Returns socket port.
|
||||
int WifiLanServerSocket::GetPort() const {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
if (enable_blocking_socket_) {
|
||||
return server_socket_.GetPort();
|
||||
} else {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
return std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Blocks until either:
|
||||
@@ -71,19 +87,29 @@ int WifiLanServerSocket::GetPort() const {
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
LOG(INFO) << __func__ << ": Accept is called.";
|
||||
if (enable_blocking_socket_) {
|
||||
auto client_socket = server_socket_.Accept();
|
||||
if (client_socket == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
while (!closed_ && pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
LOG(INFO) << __func__ << ": Accepted a remote connection.";
|
||||
|
||||
return std::make_unique<WifiLanSocket>(std::move(client_socket));
|
||||
} else {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
LOG(INFO) << __func__ << ": Accept is called.";
|
||||
while (!closed_ && pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
}
|
||||
if (closed_) return {};
|
||||
|
||||
StreamSocket wifi_lan_socket = pending_sockets_.front();
|
||||
pending_sockets_.pop_front();
|
||||
|
||||
LOG(INFO) << __func__ << ": Accepted a remote connection.";
|
||||
return std::make_unique<WifiLanSocket>(wifi_lan_socket);
|
||||
}
|
||||
if (closed_) return {};
|
||||
|
||||
StreamSocket wifi_lan_socket = pending_sockets_.front();
|
||||
pending_sockets_.pop_front();
|
||||
|
||||
LOG(INFO) << __func__ << ": Accepted a remote connection.";
|
||||
return std::make_unique<WifiLanSocket>(wifi_lan_socket);
|
||||
}
|
||||
|
||||
void WifiLanServerSocket::SetCloseNotifier(
|
||||
@@ -96,27 +122,41 @@ Exception WifiLanServerSocket::Close() {
|
||||
try {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
LOG(INFO) << __func__ << ": Close is called.";
|
||||
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
if (stream_socket_listener_ != nullptr) {
|
||||
stream_socket_listener_.ConnectionReceived(listener_event_token_);
|
||||
stream_socket_listener_.Close();
|
||||
stream_socket_listener_ = nullptr;
|
||||
|
||||
for (const auto& pending_socket : pending_sockets_) {
|
||||
pending_socket.Close();
|
||||
if (enable_blocking_socket_) {
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
pending_sockets_ = {};
|
||||
}
|
||||
LOG(INFO) << __func__ << ": closing blocking socket.";
|
||||
|
||||
closed_ = true;
|
||||
cond_.SignalAll();
|
||||
server_socket_.Close();
|
||||
closed_ = true;
|
||||
|
||||
if (close_notifier_ != nullptr) {
|
||||
close_notifier_();
|
||||
if (close_notifier_ != nullptr) {
|
||||
close_notifier_();
|
||||
}
|
||||
} else {
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
if (stream_socket_listener_ != nullptr) {
|
||||
stream_socket_listener_.ConnectionReceived(listener_event_token_);
|
||||
stream_socket_listener_.Close();
|
||||
stream_socket_listener_ = nullptr;
|
||||
|
||||
for (const auto& pending_socket : pending_sockets_) {
|
||||
pending_socket.Close();
|
||||
}
|
||||
|
||||
pending_sockets_ = {};
|
||||
}
|
||||
|
||||
closed_ = true;
|
||||
cond_.SignalAll();
|
||||
|
||||
if (close_notifier_ != nullptr) {
|
||||
close_notifier_();
|
||||
}
|
||||
}
|
||||
|
||||
LOG(INFO) << __func__ << ": Close completed succesfully.";
|
||||
@@ -141,68 +181,78 @@ Exception WifiLanServerSocket::Close() {
|
||||
}
|
||||
|
||||
bool WifiLanServerSocket::listen() {
|
||||
// Get current IP addresses of the device.
|
||||
ip_addresses_ = Get4BytesIpv4Addresses();
|
||||
if (enable_blocking_socket_) {
|
||||
ip_addresses_ = GetIpv4Addresses();
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
LOG(WARNING) << "failed to start accepting connection without IP "
|
||||
"addresses configured on computer.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup stream socket listener.
|
||||
stream_socket_listener_ = StreamSocketListener();
|
||||
|
||||
stream_socket_listener_.Control().QualityOfService(
|
||||
SocketQualityOfService::LowLatency);
|
||||
|
||||
stream_socket_listener_.Control().KeepAlive(true);
|
||||
|
||||
// Setup socket event of ConnectionReceived.
|
||||
listener_event_token_ = stream_socket_listener_.ConnectionReceived(
|
||||
{this, &WifiLanServerSocket::Listener_ConnectionReceived});
|
||||
|
||||
try {
|
||||
stream_socket_listener_.BindServiceNameAsync(winrt::to_hstring(port_))
|
||||
.get();
|
||||
if (port_ == 0) {
|
||||
port_ =
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
if (!server_socket_.Listen(ip_addresses_.front(), port_)) {
|
||||
LOG(ERROR) << "Failed to listen socket at " << ip_addresses_.front()
|
||||
<< ":" << port_;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (std::exception exception) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Cannot accept connection on preferred port. Exception: "
|
||||
<< exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
LOG(ERROR)
|
||||
<< __func__
|
||||
<< ": Cannot accept connection on preferred port. WinRT exception: "
|
||||
<< error.code() << ": " << winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
LOG(ERROR) << __func__ << ": Unknown exeption.";
|
||||
} else {
|
||||
// Get current IP addresses of the device.
|
||||
ip_addresses_ = Get4BytesIpv4Addresses();
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
LOG(WARNING) << "failed to start accepting connection without IP "
|
||||
"addresses configured on computer.";
|
||||
return false;
|
||||
}
|
||||
// Setup stream socket listener.
|
||||
stream_socket_listener_ = StreamSocketListener();
|
||||
|
||||
stream_socket_listener_.Control().QualityOfService(
|
||||
SocketQualityOfService::LowLatency);
|
||||
|
||||
stream_socket_listener_.Control().KeepAlive(true);
|
||||
|
||||
// Setup socket event of ConnectionReceived.
|
||||
listener_event_token_ = stream_socket_listener_.ConnectionReceived(
|
||||
{this, &WifiLanServerSocket::Listener_ConnectionReceived});
|
||||
|
||||
try {
|
||||
stream_socket_listener_.BindServiceNameAsync(winrt::to_hstring(port_))
|
||||
.get();
|
||||
if (port_ == 0) {
|
||||
port_ = std::stoi(
|
||||
stream_socket_listener_.Information().LocalPort().c_str());
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (std::exception exception) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Cannot accept connection on preferred port. Exception: "
|
||||
<< exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
LOG(ERROR)
|
||||
<< __func__
|
||||
<< ": Cannot accept connection on preferred port. WinRT exception: "
|
||||
<< error.code() << ": " << winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
LOG(ERROR) << __func__ << ": Unknown exception.";
|
||||
}
|
||||
|
||||
try {
|
||||
stream_socket_listener_.BindServiceNameAsync({}).get();
|
||||
|
||||
// Need to save the port information.
|
||||
port_ =
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
return true;
|
||||
} catch (std::exception exception) {
|
||||
LOG(ERROR) << __func__ << ": Cannot bind to any port. Exception: "
|
||||
<< exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
LOG(ERROR) << __func__ << ": Cannot bind to any port. WinRT exception: "
|
||||
<< error.code() << ": " << winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
LOG(ERROR) << __func__ << ": Unknown exception.";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
stream_socket_listener_.BindServiceNameAsync({}).get();
|
||||
|
||||
// Need to save the port information.
|
||||
port_ =
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
return true;
|
||||
} catch (std::exception exception) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Cannot bind to any port. Exception: " << exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Cannot bind to any port. WinRT exception: " << error.code()
|
||||
<< ": " << winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
LOG(ERROR) << __func__ << ": Unknown exeption.";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived(
|
||||
|
||||
Reference in New Issue
Block a user