mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fixed the deadlock in BluetoothServerSocket
PiperOrigin-RevId: 460724338
This commit is contained in:
committed by
Copybara-Service
parent
e4a0f280e2
commit
bff4051015
@@ -12,6 +12,14 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/implementation/windows/wifi_lan.h"
|
||||
#include "internal/platform/logging.h"
|
||||
@@ -19,12 +27,17 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
namespace {
|
||||
|
||||
using ::winrt::Windows::Networking::Sockets::SocketQualityOfService;
|
||||
|
||||
}
|
||||
|
||||
WifiLanServerSocket::WifiLanServerSocket(int port) : port_(port) {}
|
||||
|
||||
WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
|
||||
|
||||
// Returns ip address.
|
||||
// Returns the first IP address.
|
||||
std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
return {};
|
||||
@@ -40,7 +53,7 @@ std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
return ip_addresses_.front();
|
||||
}
|
||||
|
||||
// Returns port.
|
||||
// Returns socket port.
|
||||
int WifiLanServerSocket::GetPort() const {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
return 0;
|
||||
@@ -57,6 +70,8 @@ int WifiLanServerSocket::GetPort() const {
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Accept is called.";
|
||||
|
||||
while (!closed_ && pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
}
|
||||
@@ -64,6 +79,8 @@ std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
|
||||
StreamSocket wifi_lan_socket = pending_sockets_.front();
|
||||
pending_sockets_.pop_front();
|
||||
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Accepted a remote connection.";
|
||||
return std::make_unique<WifiLanSocket>(wifi_lan_socket);
|
||||
}
|
||||
|
||||
@@ -75,6 +92,8 @@ void WifiLanServerSocket::SetCloseNotifier(std::function<void()> notifier) {
|
||||
Exception WifiLanServerSocket::Close() {
|
||||
try {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Close is called.";
|
||||
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
@@ -83,27 +102,33 @@ Exception WifiLanServerSocket::Close() {
|
||||
stream_socket_listener_.Close();
|
||||
stream_socket_listener_ = nullptr;
|
||||
|
||||
if (!pending_sockets_.empty()) {
|
||||
auto it = pending_sockets_.begin();
|
||||
while (it != pending_sockets_.end()) {
|
||||
it->Close();
|
||||
}
|
||||
for (const auto& pending_socket : pending_sockets_) {
|
||||
pending_socket.Close();
|
||||
}
|
||||
|
||||
cond_.SignalAll();
|
||||
pending_sockets_ = {};
|
||||
}
|
||||
|
||||
closed_ = true;
|
||||
cond_.SignalAll();
|
||||
|
||||
if (close_notifier_ != nullptr) {
|
||||
close_notifier_();
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully.";
|
||||
return {Exception::kSuccess};
|
||||
} catch (...) {
|
||||
closed_ = true;
|
||||
cond_.SignalAll();
|
||||
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket.";
|
||||
return {Exception::kIo};
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiLanServerSocket::listen() {
|
||||
// Check IP address
|
||||
// Get current IP addresses of the device.
|
||||
ip_addresses_ = GetIpAddresses();
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
@@ -112,10 +137,15 @@ bool WifiLanServerSocket::listen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Save connection callback
|
||||
// Setup stream socket listener.
|
||||
stream_socket_listener_ = StreamSocketListener();
|
||||
|
||||
// Setup callback
|
||||
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});
|
||||
|
||||
@@ -135,7 +165,8 @@ bool WifiLanServerSocket::listen() {
|
||||
|
||||
try {
|
||||
stream_socket_listener_.BindServiceNameAsync({}).get();
|
||||
// need to save the port information
|
||||
|
||||
// Need to save the port information.
|
||||
port_ =
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
return true;
|
||||
@@ -151,6 +182,7 @@ fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived(
|
||||
StreamSocketListener listener,
|
||||
StreamSocketListenerConnectionReceivedEventArgs const& args) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Received connection.";
|
||||
|
||||
if (closed_) {
|
||||
return fire_and_forget{};
|
||||
@@ -161,7 +193,7 @@ fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived(
|
||||
return fire_and_forget{};
|
||||
}
|
||||
|
||||
// Retrieves IP addresses from local machine
|
||||
// Retrieves IP addresses from local machine.
|
||||
std::vector<std::string> WifiLanServerSocket::GetIpAddresses() const {
|
||||
std::vector<std::string> result{};
|
||||
auto host_names = NetworkInformation::GetHostNames();
|
||||
@@ -170,7 +202,7 @@ std::vector<std::string> WifiLanServerSocket::GetIpAddresses() const {
|
||||
host_name.IPInformation().NetworkAdapter() != nullptr &&
|
||||
host_name.Type() == HostNameType::Ipv4) {
|
||||
std::string ipv4_s = winrt::to_string(host_name.ToString());
|
||||
// Converts ip address from x.x.x.x to 4 bytes format
|
||||
// Converts ip address from x.x.x.x to 4 bytes format.
|
||||
in_addr address;
|
||||
address.S_un.S_addr = inet_addr(ipv4_s.c_str());
|
||||
char ipv4_b[5];
|
||||
|
||||
Reference in New Issue
Block a user