Remove unused code.

PiperOrigin-RevId: 826748749
This commit is contained in:
Francis Tsui
2025-10-31 22:28:20 -07:00
committed by Copybara-Service
parent f353079f6f
commit e5de080c6a
5 changed files with 43 additions and 185 deletions
@@ -15,6 +15,7 @@
#ifndef PLATFORM_API_WIFI_LAN_H_
#define PLATFORM_API_WIFI_LAN_H_
#include <memory>
#include <string>
#include <vector>
@@ -23,14 +23,13 @@
// Standard C/C++ headers
#include <cstddef>
#include <cstdint>
#include <deque>
#include <exception>
#include <memory>
#include <optional>
#include <string>
#include <utility>
// Nearby connections headers
#include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
@@ -57,8 +56,6 @@
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.ServiceDiscovery.Dnssd.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Storage.Streams.h"
#include "internal/platform/implementation/windows/generated/winrt/base.h"
namespace nearby::windows {
@@ -71,12 +68,9 @@ class WifiLanSocket : public api::WifiLanSocket {
public:
WifiLanSocket();
explicit WifiLanSocket(
winrt::Windows::Networking::Sockets::StreamSocket socket);
explicit WifiLanSocket(std::unique_ptr<NearbyClientSocket> socket);
WifiLanSocket(WifiLanSocket&) = default;
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
WifiLanSocket(WifiLanSocket&&) = default;
~WifiLanSocket() override;
WifiLanSocket& operator=(const WifiLanSocket&) = default;
WifiLanSocket& operator=(WifiLanSocket&&) = default;
// Returns the InputStream of the WifiLanSocket.
@@ -102,9 +96,7 @@ class WifiLanSocket : public api::WifiLanSocket {
// A simple wrapper to handle input stream of socket
class SocketInputStream : public InputStream {
public:
explicit SocketInputStream(
winrt::Windows::Storage::Streams::IInputStream input_stream);
explicit SocketInputStream(NearbyClientSocket* client_socket);
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket);
~SocketInputStream() = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
@@ -112,17 +104,13 @@ class WifiLanSocket : public api::WifiLanSocket {
Exception Close() override;
private:
winrt::Windows::Storage::Streams::IInputStream input_stream_{nullptr};
winrt::Windows::Storage::Streams::Buffer read_buffer_{nullptr};
NearbyClientSocket* client_socket_{nullptr};
NearbyClientSocket* absl_nonnull const client_socket_;
};
// A simple wrapper to handle output stream of socket
class SocketOutputStream : public OutputStream {
public:
explicit SocketOutputStream(
winrt::Windows::Storage::Streams::IOutputStream output_stream);
explicit SocketOutputStream(NearbyClientSocket* client_socket);
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket);
~SocketOutputStream() = default;
Exception Write(const ByteArray& data) override;
@@ -130,27 +118,22 @@ class WifiLanSocket : public api::WifiLanSocket {
Exception Close() override;
private:
winrt::Windows::Storage::Streams::IOutputStream output_stream_{nullptr};
NearbyClientSocket* client_socket_{nullptr};
NearbyClientSocket* absl_nonnull const client_socket_;
};
// Internal properties
winrt::Windows::Networking::Sockets::StreamSocket stream_soket_{nullptr};
SocketInputStream input_stream_{nullptr};
SocketOutputStream output_stream_{nullptr};
std::unique_ptr<NearbyClientSocket> client_socket_;
absl_nonnull std::unique_ptr<NearbyClientSocket> client_socket_;
SocketInputStream input_stream_;
SocketOutputStream output_stream_;
};
// WifiLanServerSocket provides the support to server socket, this server socket
// accepts connection from clients.
class WifiLanServerSocket : public api::WifiLanServerSocket {
public:
explicit WifiLanServerSocket(int port);
WifiLanServerSocket(WifiLanServerSocket&) = default;
WifiLanServerSocket() = default;
WifiLanServerSocket(WifiLanServerSocket&&) = default;
~WifiLanServerSocket() override;
WifiLanServerSocket& operator=(const WifiLanServerSocket&) = default;
WifiLanServerSocket& operator=(WifiLanServerSocket&&) = default;
// Returns ip address.
@@ -159,14 +142,6 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
// Returns port.
int GetPort() const override;
// Sets port
void SetPort(int port) { port_ = port; }
winrt::Windows::Networking::Sockets::StreamSocketListener GetSocketListener()
const {
return stream_socket_listener_;
}
// Blocks until either:
// - at least one incoming connection request is available, or
// - ServerSocket is closed.
@@ -184,29 +159,14 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
Exception Close() override;
// Binds to local port
bool Listen(bool dual_stack);
bool Listen(int port, bool dual_stack);
private:
// The listener is accepting incoming connections
winrt::fire_and_forget Listener_ConnectionReceived(
winrt::Windows::Networking::Sockets::StreamSocketListener listener,
winrt::Windows::Networking::Sockets::
StreamSocketListenerConnectionReceivedEventArgs const& args);
mutable absl::Mutex mutex_;
absl::CondVar cond_;
std::deque<winrt::Windows::Networking::Sockets::StreamSocket> pending_sockets_
ABSL_GUARDED_BY(mutex_);
winrt::Windows::Networking::Sockets::StreamSocketListener
stream_socket_listener_{nullptr};
winrt::event_token listener_event_token_{};
// Close notifier
absl::AnyInvocable<void()> close_notifier_ = nullptr;
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
// Cache socket not be picked by upper layer
int port_ = 0;
bool closed_ = false;
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
NearbyServerSocket server_socket_;
};
@@ -446,13 +446,13 @@ std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
return nullptr;
}
std::unique_ptr<WifiLanServerSocket> server_socket =
std::make_unique<WifiLanServerSocket>(port);
std::make_unique<WifiLanServerSocket>();
WifiLanServerSocket* server_socket_ptr = server_socket.get();
bool dual_stack = NearbyFlags::GetInstance().GetBoolFlag(
platform::config_package_nearby::nearby_platform_feature::
kEnableIpv6DualStack);
if (server_socket->Listen(dual_stack)) {
if (server_socket->Listen(port, dual_stack)) {
int port = server_socket_ptr->GetPort();
LOG(INFO) << "started to listen serive on port: " << port;
port_to_server_socket_map_.insert({port, server_socket_ptr});
@@ -14,7 +14,6 @@
#include <windows.h>
#include <exception>
#include <memory>
#include <string>
#include <utility>
@@ -24,7 +23,6 @@
#include "absl/synchronization/mutex.h"
#include "internal/platform/exception.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/socket_address.h"
#include "internal/platform/implementation/windows/utils.h"
@@ -32,14 +30,6 @@
#include "internal/platform/logging.h"
namespace nearby::windows {
namespace {
using ::winrt::fire_and_forget;
using ::winrt::Windows::Networking::Sockets::StreamSocketListener;
using ::winrt::Windows::Networking::Sockets::
StreamSocketListenerConnectionReceivedEventArgs;
} // namespace
WifiLanServerSocket::WifiLanServerSocket(int port) : port_(port) {}
WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
@@ -76,12 +66,14 @@ std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
void WifiLanServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(mutex_);
close_notifier_ = std::move(notifier);
}
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception WifiLanServerSocket::Close() {
try {
absl::AnyInvocable<void()> close_callback;
{
absl::MutexLock lock(mutex_);
VLOG(1) << __func__ << ": Close is called.";
if (closed_) {
@@ -92,56 +84,26 @@ Exception WifiLanServerSocket::Close() {
server_socket_.Close();
closed_ = true;
if (close_notifier_ != nullptr) {
close_notifier_();
}
LOG(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess};
} catch (std::exception exception) {
closed_ = true;
cond_.SignalAll();
LOG(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
closed_ = true;
cond_.SignalAll();
LOG(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
return {Exception::kIo};
} catch (...) {
closed_ = true;
cond_.SignalAll();
LOG(ERROR) << __func__ << ": Unknown exeption.";
return {Exception::kIo};
close_callback = std::move(close_notifier_);
}
if (close_callback) {
close_callback();
}
LOG(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess};
}
bool WifiLanServerSocket::Listen(bool dual_stack) {
bool WifiLanServerSocket::Listen(int port, bool dual_stack) {
// Listen on all interfaces.
SocketAddress address(dual_stack);
SocketAddress::FromString(address, "", port_);
SocketAddress::FromString(address, "", port);
if (!server_socket_.Listen(address)) {
LOG(ERROR) << "Failed to listen socket at port:" << port_;
LOG(ERROR) << "Failed to listen socket at port:" << port;
return false;
}
return true;
}
fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived(
StreamSocketListener listener,
StreamSocketListenerConnectionReceivedEventArgs const& args) {
absl::MutexLock lock(mutex_);
LOG(INFO) << __func__ << ": Received connection.";
if (closed_) {
return fire_and_forget{};
}
pending_sockets_.push_back(args.Socket());
cond_.SignalAll();
return fire_and_forget{};
}
} // namespace nearby::windows
@@ -13,54 +13,28 @@
// limitations under the License.
#include <cstdint>
#include <cstring>
#include <exception>
#include <memory>
#include <string>
#include <utility>
#include "internal/flags/nearby_flags.h"
#include "absl/base/nullability.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Storage.Streams.h"
#include "internal/platform/implementation/windows/nearby_client_socket.h"
#include "internal/platform/implementation/windows/socket_address.h"
#include "internal/platform/implementation/windows/wifi_lan.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/logging.h"
#include "internal/platform/output_stream.h"
namespace nearby::windows {
namespace {
using ::winrt::Windows::Networking::Sockets::StreamSocket;
using ::winrt::Windows::Storage::Streams::IInputStream;
using ::winrt::Windows::Storage::Streams::IOutputStream;
} // namespace
WifiLanSocket::WifiLanSocket() {
client_socket_ = std::make_unique<NearbyClientSocket>();
input_stream_ = SocketInputStream(client_socket_.get());
output_stream_ = SocketOutputStream(client_socket_.get());
}
WifiLanSocket::WifiLanSocket()
: WifiLanSocket(std::make_unique<NearbyClientSocket>()) {}
WifiLanSocket::WifiLanSocket(StreamSocket socket) {
stream_soket_ = socket;
VLOG(1) << "Socket send buffer size: "
<< socket.Control().OutboundBufferSizeInBytes();
socket.Control().OutboundBufferSizeInBytes(4 * 1024 * 1024);
VLOG(1) << "Updated send buffer size to: "
<< socket.Control().OutboundBufferSizeInBytes();
input_stream_ = SocketInputStream(socket.InputStream());
output_stream_ = SocketOutputStream(socket.OutputStream());
}
WifiLanSocket::WifiLanSocket(std::unique_ptr<NearbyClientSocket> socket) {
client_socket_ = std::move(socket);
input_stream_ = SocketInputStream(client_socket_.get());
output_stream_ = SocketOutputStream(client_socket_.get());
}
WifiLanSocket::WifiLanSocket(
absl_nonnull std::unique_ptr<NearbyClientSocket> socket)
: client_socket_(std::move(socket)),
input_stream_(client_socket_.get()),
output_stream_(client_socket_.get()) {}
WifiLanSocket::~WifiLanSocket() { Close(); }
@@ -69,10 +43,7 @@ InputStream& WifiLanSocket::GetInputStream() { return input_stream_; }
OutputStream& WifiLanSocket::GetOutputStream() { return output_stream_; }
Exception WifiLanSocket::Close() {
if (client_socket_ != nullptr) {
return client_socket_->Close();
}
return {Exception::kSuccess};
return client_socket_->Close();
}
bool WifiLanSocket::Connect(const SocketAddress& server_address) {
@@ -80,73 +51,37 @@ bool WifiLanSocket::Connect(const SocketAddress& server_address) {
}
// SocketInputStream
WifiLanSocket::SocketInputStream::SocketInputStream(IInputStream input_stream) {
input_stream_ = input_stream;
}
WifiLanSocket::SocketInputStream::SocketInputStream(
NearbyClientSocket* client_socket) {
client_socket_ = client_socket;
}
NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
ExceptionOr<ByteArray> WifiLanSocket::SocketInputStream::Read(
std::int64_t size) {
if (client_socket_ == nullptr) {
LOG(ERROR) << "Failed to read data due to no client socket.";
return {Exception::kIo};
}
return client_socket_->Read(size);
}
ExceptionOr<size_t> WifiLanSocket::SocketInputStream::Skip(size_t offset) {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Skip(offset);
}
Exception WifiLanSocket::SocketInputStream::Close() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Close();
}
// SocketOutputStream
WifiLanSocket::SocketOutputStream::SocketOutputStream(
IOutputStream output_stream) {
output_stream_ = output_stream;
}
WifiLanSocket::SocketOutputStream::SocketOutputStream(
NearbyClientSocket* client_socket) {
client_socket_ = client_socket;
}
NearbyClientSocket* absl_nonnull client_socket)
: client_socket_(client_socket) {}
Exception WifiLanSocket::SocketOutputStream::Write(const ByteArray& data) {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Write(data);
}
Exception WifiLanSocket::SocketOutputStream::Flush() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Flush();
}
Exception WifiLanSocket::SocketOutputStream::Close() {
if (client_socket_ == nullptr) {
return {Exception::kIo};
}
return client_socket_->Close();
}