mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Remove duplicate classes.
PiperOrigin-RevId: 828196042
This commit is contained in:
committed by
Copybara-Service
parent
2c6c0a2524
commit
5d8456b0d0
@@ -532,3 +532,34 @@ cc_test(
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "nearby_client_socket_test",
|
||||
size = "small",
|
||||
timeout = "short",
|
||||
srcs = [
|
||||
"nearby_client_socket_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":socket_address",
|
||||
":windows",
|
||||
"//internal/platform:base",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "nearby_server_socket_test",
|
||||
size = "small",
|
||||
timeout = "short",
|
||||
srcs = [
|
||||
"nearby_server_socket_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":socket_address",
|
||||
":windows",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -31,19 +31,21 @@
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
NearbyClientSocket::NearbyClientSocket() {
|
||||
NearbyClientSocket::NearbyClientSocket()
|
||||
: NearbyClientSocket(INVALID_SOCKET) {
|
||||
}
|
||||
|
||||
NearbyClientSocket::NearbyClientSocket(SOCKET socket) : socket_(socket) {
|
||||
WSADATA wsa_data;
|
||||
int result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
||||
if (result != 0) {
|
||||
LOG(WARNING) << "WSAStartup failed with error " << result;
|
||||
}
|
||||
|
||||
is_socket_initiated_ = (result == 0);
|
||||
}
|
||||
|
||||
NearbyClientSocket::NearbyClientSocket(SOCKET socket) : socket_(socket) {}
|
||||
|
||||
NearbyClientSocket::~NearbyClientSocket() {
|
||||
Close();
|
||||
if (is_socket_initiated_) {
|
||||
WSACleanup();
|
||||
}
|
||||
@@ -206,8 +208,8 @@ Exception NearbyClientSocket::Flush() {
|
||||
|
||||
Exception NearbyClientSocket::Close() {
|
||||
if (socket_ == INVALID_SOCKET) {
|
||||
LOG(WARNING) << "Trying to close an invalid socket.";
|
||||
return {Exception::kIo};
|
||||
VLOG(1) << "Socket already closed.";
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
shutdown(socket_, SD_BOTH);
|
||||
|
||||
@@ -20,15 +20,24 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "absl/base/nullability.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/windows/socket_address.h"
|
||||
#include "internal/platform/input_stream.h"
|
||||
#include "internal/platform/output_stream.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
// Socket wrapper class.
|
||||
// This class is thread-compatible.
|
||||
class NearbyClientSocket {
|
||||
public:
|
||||
// Creates a socket not bound to underlying platform implementation.
|
||||
// `Connect` must be called to create a platform socket.
|
||||
NearbyClientSocket();
|
||||
// Creates a socket with the given platform socket.
|
||||
// Calling `Connect` on this socket will fail.
|
||||
explicit NearbyClientSocket(SOCKET socket);
|
||||
~NearbyClientSocket();
|
||||
|
||||
@@ -44,6 +53,42 @@ class NearbyClientSocket {
|
||||
SOCKET socket_ = INVALID_SOCKET;
|
||||
};
|
||||
|
||||
// A simple wrapper to handle input stream of socket
|
||||
class SocketInputStream : public InputStream {
|
||||
public:
|
||||
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
~SocketInputStream() override = default;
|
||||
|
||||
ExceptionOr<ByteArray> Read(std::int64_t size) override {
|
||||
return client_socket_->Read(size);
|
||||
}
|
||||
ExceptionOr<size_t> Skip(size_t offset) override {
|
||||
return client_socket_->Skip(offset);
|
||||
}
|
||||
Exception Close() override { return client_socket_->Close(); }
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
// A simple wrapper to handle output stream of socket
|
||||
class SocketOutputStream : public OutputStream {
|
||||
public:
|
||||
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
~SocketOutputStream() override = default;
|
||||
|
||||
Exception Write(const ByteArray& data) override {
|
||||
return client_socket_->Write(data);
|
||||
}
|
||||
Exception Flush() override { return client_socket_->Flush(); }
|
||||
Exception Close() override { return client_socket_->Close(); }
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_NEARBY_CLIENT_SOCKET_H_
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
// Copyright 2025 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/windows/nearby_client_socket.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/windows/socket_address.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
namespace {
|
||||
|
||||
SOCKET CreateSocket(const SocketAddress& address) {
|
||||
SOCKET socket_handle = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
DWORD v6_only = 0;
|
||||
EXPECT_NE(
|
||||
setsockopt(socket_handle, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
reinterpret_cast<const char*>(&v6_only), sizeof(v6_only)),
|
||||
SOCKET_ERROR);
|
||||
EXPECT_NE(
|
||||
bind(socket_handle, address.address(), sizeof(sockaddr_storage)),
|
||||
SOCKET_ERROR);
|
||||
return socket_handle;
|
||||
}
|
||||
|
||||
SocketAddress GetLocalAddress(SOCKET socket_handle) {
|
||||
SocketAddress local_address(/*dual_stack=*/true);
|
||||
int address_length = sizeof(sockaddr_storage);
|
||||
EXPECT_NE(
|
||||
getsockname(socket_handle, local_address.address(), &address_length),
|
||||
SOCKET_ERROR);
|
||||
return local_address;
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, ConnectWithBoundSocketFails) {
|
||||
SOCKET socket_handle = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
NearbyClientSocket client_socket(socket_handle);
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", 8080);
|
||||
|
||||
EXPECT_FALSE(client_socket.Connect(server_address));
|
||||
|
||||
closesocket(socket_handle);
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, Connect) {
|
||||
SocketAddress local_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(local_address, "", 0);
|
||||
SOCKET socket_handle = CreateSocket(local_address);
|
||||
SocketAddress bound_address = GetLocalAddress(socket_handle);
|
||||
EXPECT_NE(listen(socket_handle, SOMAXCONN), SOCKET_ERROR);
|
||||
NearbyClientSocket client_socket;
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", bound_address.port());
|
||||
|
||||
EXPECT_TRUE(client_socket.Connect(server_address));
|
||||
|
||||
closesocket(socket_handle);
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, CloseNotOpened) {
|
||||
NearbyClientSocket client_socket;
|
||||
EXPECT_TRUE(client_socket.Close().Ok());
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, Read) {
|
||||
SocketAddress local_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(local_address, "", 0);
|
||||
SOCKET socket_handle = CreateSocket(local_address);
|
||||
SocketAddress bound_address = GetLocalAddress(socket_handle);
|
||||
EXPECT_NE(listen(socket_handle, SOMAXCONN), SOCKET_ERROR);
|
||||
NearbyClientSocket client_socket;
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", bound_address.port());
|
||||
EXPECT_TRUE(client_socket.Connect(server_address));
|
||||
SocketAddress peer_address;
|
||||
int peer_address_length = sizeof(sockaddr_storage);
|
||||
SOCKET accept_socket = accept(socket_handle, peer_address.address(),
|
||||
/*addrlen=*/&peer_address_length);
|
||||
EXPECT_NE(accept_socket, INVALID_SOCKET);
|
||||
EXPECT_NE(send(accept_socket, "hello", 5, 0), SOCKET_ERROR);
|
||||
|
||||
EXPECT_EQ(client_socket.Read(5).result(), ByteArray("hello"));
|
||||
|
||||
closesocket(socket_handle);
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, Skip) {
|
||||
SocketAddress local_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(local_address, "", 0);
|
||||
SOCKET socket_handle = CreateSocket(local_address);
|
||||
SocketAddress bound_address = GetLocalAddress(socket_handle);
|
||||
EXPECT_NE(listen(socket_handle, SOMAXCONN), SOCKET_ERROR);
|
||||
NearbyClientSocket client_socket;
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", bound_address.port());
|
||||
EXPECT_TRUE(client_socket.Connect(server_address));
|
||||
SocketAddress peer_address;
|
||||
int peer_address_length = sizeof(sockaddr_storage);
|
||||
SOCKET accept_socket = accept(socket_handle, peer_address.address(),
|
||||
/*addrlen=*/&peer_address_length);
|
||||
EXPECT_NE(accept_socket, INVALID_SOCKET);
|
||||
EXPECT_NE(send(accept_socket, "hello there", 11, 0), SOCKET_ERROR);
|
||||
|
||||
EXPECT_EQ(client_socket.Skip(6).result(), 6);
|
||||
|
||||
EXPECT_EQ(client_socket.Read(5).result(), ByteArray("there"));
|
||||
closesocket(socket_handle);
|
||||
}
|
||||
|
||||
TEST(NearbyClientSocketTest, Write) {
|
||||
SocketAddress local_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(local_address, "", 0);
|
||||
SOCKET socket_handle = CreateSocket(local_address);
|
||||
SocketAddress bound_address = GetLocalAddress(socket_handle);
|
||||
EXPECT_NE(listen(socket_handle, SOMAXCONN), SOCKET_ERROR);
|
||||
NearbyClientSocket client_socket;
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", bound_address.port());
|
||||
EXPECT_TRUE(client_socket.Connect(server_address));
|
||||
SocketAddress peer_address;
|
||||
int peer_address_length = sizeof(sockaddr_storage);
|
||||
SOCKET accept_socket = accept(socket_handle, peer_address.address(),
|
||||
/*addrlen=*/&peer_address_length);
|
||||
EXPECT_NE(accept_socket, INVALID_SOCKET);
|
||||
|
||||
EXPECT_TRUE(client_socket.Write(ByteArray("hello")).Ok());
|
||||
std::string buffer;
|
||||
buffer.resize(5);
|
||||
EXPECT_EQ(recv(accept_socket, buffer.data(), 5, 0), 5);
|
||||
EXPECT_EQ(buffer, "hello");
|
||||
|
||||
closesocket(socket_handle);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby::windows
|
||||
@@ -18,8 +18,10 @@
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/windows/nearby_client_socket.h"
|
||||
#include "internal/platform/implementation/windows/socket_address.h"
|
||||
#include "internal/platform/logging.h"
|
||||
@@ -38,6 +40,7 @@ NearbyServerSocket::NearbyServerSocket() {
|
||||
}
|
||||
|
||||
NearbyServerSocket::~NearbyServerSocket() {
|
||||
Close();
|
||||
if (is_socket_initiated_) {
|
||||
WSACleanup();
|
||||
}
|
||||
@@ -51,6 +54,7 @@ bool NearbyServerSocket::Listen(const SocketAddress& address) {
|
||||
return false;
|
||||
}
|
||||
|
||||
absl::MutexLock lock( mutex_ );
|
||||
socket_ = socket(address.dual_stack() ? AF_INET6 : AF_INET, SOCK_STREAM,
|
||||
IPPROTO_TCP);
|
||||
if (socket_ == INVALID_SOCKET) {
|
||||
@@ -92,6 +96,7 @@ bool NearbyServerSocket::Listen(const SocketAddress& address) {
|
||||
SOCKET_ERROR) {
|
||||
LOG(ERROR) << "Failed to bind socket with error " << WSAGetLastError();
|
||||
closesocket(socket_);
|
||||
socket_ = INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -101,6 +106,7 @@ bool NearbyServerSocket::Listen(const SocketAddress& address) {
|
||||
SOCKET_ERROR) {
|
||||
LOG(ERROR) << "Failed to get socket name with error " << WSAGetLastError();
|
||||
closesocket(socket_);
|
||||
socket_ = INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -111,6 +117,7 @@ bool NearbyServerSocket::Listen(const SocketAddress& address) {
|
||||
if (::listen(socket_, /*backlog=*/SOMAXCONN) == SOCKET_ERROR) {
|
||||
LOG(ERROR) << "Failed to listen socket with error " << WSAGetLastError();
|
||||
closesocket(socket_);
|
||||
socket_ = INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -119,15 +126,21 @@ bool NearbyServerSocket::Listen(const SocketAddress& address) {
|
||||
|
||||
std::unique_ptr<NearbyClientSocket> NearbyServerSocket::Accept() {
|
||||
LOG(INFO) << "Accept is called on NearbyServerSocket.";
|
||||
if (!is_socket_initiated_) {
|
||||
LOG(WARNING) << "Windows socket is not initiated";
|
||||
return nullptr;
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
{
|
||||
absl::MutexLock lock( mutex_ );
|
||||
if (!is_socket_initiated_ || socket_ == INVALID_SOCKET) {
|
||||
LOG(WARNING) << "Windows socket is not initiated";
|
||||
return nullptr;
|
||||
}
|
||||
socket = socket_;
|
||||
}
|
||||
|
||||
SocketAddress peer_address;
|
||||
int peer_address_length = sizeof(sockaddr_storage);
|
||||
|
||||
SOCKET client_socket = accept(socket_, peer_address.address(),
|
||||
// Release lock before calling accept. Otherwise the accept call blocks and
|
||||
// prevents other calls from using the socket.
|
||||
SOCKET client_socket = accept(socket, peer_address.address(),
|
||||
/*addrlen=*/&peer_address_length);
|
||||
if (client_socket == INVALID_SOCKET) {
|
||||
LOG(ERROR) << "Failed to accept socket with error: " << WSAGetLastError();
|
||||
@@ -140,23 +153,27 @@ std::unique_ptr<NearbyClientSocket> NearbyServerSocket::Accept() {
|
||||
}
|
||||
|
||||
bool NearbyServerSocket::Close() {
|
||||
bool result = true;
|
||||
if (socket_ != INVALID_SOCKET) {
|
||||
if (shutdown(/*s=*/socket_, /*how=*/SD_BOTH) == SOCKET_ERROR) {
|
||||
LOG(WARNING) << "Shutdown failed with error:" << WSAGetLastError();
|
||||
result = false;
|
||||
absl::AnyInvocable<void()> close_callback;
|
||||
{
|
||||
absl::MutexLock lock( mutex_ );
|
||||
if (socket_ != INVALID_SOCKET) {
|
||||
if (closesocket(/*s=*/socket_) == SOCKET_ERROR) {
|
||||
LOG(WARNING) << "Close socket failed with error:" << WSAGetLastError();
|
||||
}
|
||||
socket_ = INVALID_SOCKET;
|
||||
close_callback = std::move(close_notifier_);
|
||||
}
|
||||
|
||||
if (closesocket(/*s=*/socket_) == SOCKET_ERROR) {
|
||||
LOG(WARNING) << "Close socket failed with error:" << WSAGetLastError();
|
||||
result = false;
|
||||
}
|
||||
|
||||
socket_ = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (close_callback) {
|
||||
close_callback();
|
||||
}
|
||||
LOG(INFO) << "Closed NearbyServerSocket.";
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
void NearbyServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
|
||||
absl::MutexLock lock( mutex_ );
|
||||
close_notifier_ = std::move(notifier);
|
||||
}
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
@@ -18,8 +18,10 @@
|
||||
#include <winsock2.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/windows/nearby_client_socket.h"
|
||||
#include "internal/platform/implementation/windows/socket_address.h"
|
||||
|
||||
@@ -36,10 +38,15 @@ class NearbyServerSocket {
|
||||
|
||||
int GetPort() const { return port_; }
|
||||
|
||||
// Sets a callback to be called when the socket is closed.
|
||||
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
|
||||
|
||||
private:
|
||||
mutable absl::Mutex mutex_;
|
||||
bool is_socket_initiated_ = false;
|
||||
SOCKET socket_ = INVALID_SOCKET;
|
||||
SOCKET socket_ ABSL_GUARDED_BY(mutex_) = INVALID_SOCKET;
|
||||
int port_ = 0;
|
||||
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright 2025 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/windows/nearby_server_socket.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/implementation/windows/socket_address.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
namespace {
|
||||
|
||||
SOCKET CreateSocket() {
|
||||
SOCKET socket_handle = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
DWORD v6_only = 0;
|
||||
EXPECT_NE(
|
||||
setsockopt(socket_handle, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
reinterpret_cast<const char*>(&v6_only), sizeof(v6_only)),
|
||||
SOCKET_ERROR);
|
||||
return socket_handle;
|
||||
}
|
||||
|
||||
TEST(NearbyServerSocketTest, Listen) {
|
||||
NearbyServerSocket server_socket;
|
||||
SocketAddress address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(address, "", 0);
|
||||
EXPECT_TRUE(server_socket.Listen(address));
|
||||
EXPECT_NE(server_socket.GetPort(), 0);
|
||||
}
|
||||
|
||||
TEST(NearbyServerSocketTest, Accept) {
|
||||
NearbyServerSocket server_socket;
|
||||
SocketAddress address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(address, "", 0);
|
||||
EXPECT_TRUE(server_socket.Listen(address));
|
||||
SOCKET socket_handle = CreateSocket();
|
||||
EXPECT_NE(socket_handle, INVALID_SOCKET);
|
||||
SocketAddress server_address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(server_address, "::1", server_socket.GetPort());
|
||||
EXPECT_NE(connect(socket_handle, server_address.address(),
|
||||
sizeof(sockaddr_storage)),
|
||||
SOCKET_ERROR);
|
||||
|
||||
auto client_socket = server_socket.Accept();
|
||||
ASSERT_NE(client_socket, nullptr);
|
||||
EXPECT_TRUE(server_socket.Close());
|
||||
}
|
||||
|
||||
TEST(NearbyServerSocketTest, CloseNotifier) {
|
||||
NearbyServerSocket server_socket;
|
||||
SocketAddress address(/*dual_stack=*/true);
|
||||
SocketAddress::FromString(address, "", 0);
|
||||
EXPECT_TRUE(server_socket.Listen(address));
|
||||
bool is_closed = false;
|
||||
server_socket.SetCloseNotifier([&is_closed]() { is_closed = true; });
|
||||
EXPECT_TRUE(server_socket.Close());
|
||||
EXPECT_TRUE(is_closed);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby::windows
|
||||
@@ -28,7 +28,6 @@
|
||||
// Nearby connections headers
|
||||
#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_hotspot.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h"
|
||||
@@ -48,12 +47,6 @@ using ::winrt::Windows::Networking::HostNameType;
|
||||
using ::winrt::Windows::Networking::Sockets::SocketQualityOfService;
|
||||
} // namespace
|
||||
|
||||
WifiHotspotServerSocket::~WifiHotspotServerSocket() { Close(); }
|
||||
|
||||
int WifiHotspotServerSocket::GetPort() const {
|
||||
return server_socket_.GetPort();
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiHotspotSocket> WifiHotspotServerSocket::Accept() {
|
||||
auto client_socket = server_socket_.Accept();
|
||||
if (client_socket == nullptr) {
|
||||
@@ -64,33 +57,6 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotServerSocket::Accept() {
|
||||
return std::make_unique<WifiHotspotSocket>(std::move(client_socket));
|
||||
}
|
||||
|
||||
void WifiHotspotServerSocket::SetCloseNotifier(
|
||||
absl::AnyInvocable<void()> notifier) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
close_notifier_ = std::move(notifier);
|
||||
}
|
||||
|
||||
Exception WifiHotspotServerSocket::Close() {
|
||||
absl::AnyInvocable<void()> close_callback;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
server_socket_.Close();
|
||||
closed_ = true;
|
||||
close_callback = std::move(close_notifier_);
|
||||
}
|
||||
|
||||
if (close_callback) {
|
||||
close_callback();
|
||||
}
|
||||
|
||||
LOG(INFO) << __func__ << ": Close completed succesfully.";
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
void WifiHotspotServerSocket::PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) {
|
||||
// Get current IP addresses of the device.
|
||||
|
||||
@@ -46,10 +46,10 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
public:
|
||||
WifiHotspotServerSocket() = default;
|
||||
WifiHotspotServerSocket(WifiHotspotServerSocket&&) = default;
|
||||
~WifiHotspotServerSocket() override;
|
||||
~WifiHotspotServerSocket() override = default;
|
||||
WifiHotspotServerSocket& operator=(WifiHotspotServerSocket&&) = default;
|
||||
|
||||
int GetPort() const override;
|
||||
int GetPort() const override { return server_socket_.GetPort(); }
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
@@ -62,10 +62,15 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
// Called by the server side of a connection before passing ownership of
|
||||
// WifiHotspotServerSocker to user, to track validity of a pointer to this
|
||||
// server socket.
|
||||
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
|
||||
void SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
|
||||
server_socket_.SetCloseNotifier(std::move(notifier));
|
||||
}
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
Exception Close() override {
|
||||
server_socket_.Close();
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
void PopulateHotspotCredentials(
|
||||
HotspotCredentials& hotspot_credentials) override;
|
||||
@@ -76,13 +81,7 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
|
||||
private:
|
||||
// Retrieves hotspot IP address from local machine
|
||||
std::string GetHotspotIpAddress() const;
|
||||
|
||||
mutable absl::Mutex mutex_;
|
||||
NearbyServerSocket server_socket_;
|
||||
|
||||
// Close notifier
|
||||
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
@@ -33,7 +33,5 @@ WifiHotspotSocket::WifiHotspotSocket(
|
||||
input_stream_(client_socket_.get()),
|
||||
output_stream_(client_socket_.get()) {}
|
||||
|
||||
WifiHotspotSocket::~WifiHotspotSocket() { Close(); }
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
@@ -31,9 +31,6 @@
|
||||
// Nearby connections headers
|
||||
#include "absl/base/nullability.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/windows/nearby_client_socket.h"
|
||||
@@ -54,7 +51,7 @@ class WifiHotspotSocket : public api::WifiHotspotSocket {
|
||||
explicit WifiHotspotSocket(
|
||||
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
|
||||
WifiHotspotSocket(WifiHotspotSocket&&) = default;
|
||||
~WifiHotspotSocket() override;
|
||||
~WifiHotspotSocket() override = default;
|
||||
WifiHotspotSocket& operator=(WifiHotspotSocket&&) = default;
|
||||
|
||||
// Returns the InputStream of the WifiHotspotSocket.
|
||||
@@ -79,42 +76,6 @@ class WifiHotspotSocket : public api::WifiHotspotSocket {
|
||||
}
|
||||
|
||||
private:
|
||||
// A simple wrapper to handle input stream of socket
|
||||
class SocketInputStream : public InputStream {
|
||||
public:
|
||||
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
~SocketInputStream() override = default;
|
||||
|
||||
ExceptionOr<ByteArray> Read(std::int64_t size) override {
|
||||
return client_socket_->Read(size);
|
||||
}
|
||||
ExceptionOr<size_t> Skip(size_t offset) override {
|
||||
return client_socket_->Skip(offset);
|
||||
}
|
||||
Exception Close() override { return client_socket_->Close(); }
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
// A simple wrapper to handle output stream of socket
|
||||
class SocketOutputStream : public OutputStream {
|
||||
public:
|
||||
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
~SocketOutputStream() override = default;
|
||||
|
||||
Exception Write(const ByteArray& data) override {
|
||||
return client_socket_->Write(data);
|
||||
}
|
||||
Exception Flush() override { return client_socket_->Flush(); }
|
||||
Exception Close() override { return client_socket_->Close(); }
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
absl_nonnull std::unique_ptr<NearbyClientSocket> client_socket_;
|
||||
SocketInputStream input_stream_;
|
||||
SocketOutputStream output_stream_;
|
||||
|
||||
@@ -70,7 +70,7 @@ class WifiLanSocket : public api::WifiLanSocket {
|
||||
explicit WifiLanSocket(
|
||||
absl_nonnull std::unique_ptr<NearbyClientSocket> socket);
|
||||
WifiLanSocket(WifiLanSocket&&) = default;
|
||||
~WifiLanSocket() override;
|
||||
~WifiLanSocket() override = default;
|
||||
WifiLanSocket& operator=(WifiLanSocket&&) = default;
|
||||
|
||||
// Returns the InputStream of the WifiLanSocket.
|
||||
@@ -78,49 +78,23 @@ class WifiLanSocket : public api::WifiLanSocket {
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
InputStream& GetInputStream() override;
|
||||
InputStream& GetInputStream() override { return input_stream_; };
|
||||
|
||||
// Returns the OutputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
OutputStream& GetOutputStream() override;
|
||||
OutputStream& GetOutputStream() override { return output_stream_; };
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
Exception Close() override { return client_socket_->Close(); };
|
||||
|
||||
bool Connect(const SocketAddress& server_address);
|
||||
bool Connect(const SocketAddress& server_address) {
|
||||
return client_socket_->Connect(server_address);
|
||||
};
|
||||
|
||||
private:
|
||||
// A simple wrapper to handle input stream of socket
|
||||
class SocketInputStream : public InputStream {
|
||||
public:
|
||||
explicit SocketInputStream(NearbyClientSocket* absl_nonnull client_socket);
|
||||
~SocketInputStream() = default;
|
||||
|
||||
ExceptionOr<ByteArray> Read(std::int64_t size) override;
|
||||
ExceptionOr<size_t> Skip(size_t offset) override;
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
// A simple wrapper to handle output stream of socket
|
||||
class SocketOutputStream : public OutputStream {
|
||||
public:
|
||||
explicit SocketOutputStream(NearbyClientSocket* absl_nonnull client_socket);
|
||||
~SocketOutputStream() = default;
|
||||
|
||||
Exception Write(const ByteArray& data) override;
|
||||
Exception Flush() override;
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
NearbyClientSocket* absl_nonnull const client_socket_;
|
||||
};
|
||||
|
||||
// Internal properties
|
||||
absl_nonnull std::unique_ptr<NearbyClientSocket> client_socket_;
|
||||
SocketInputStream input_stream_;
|
||||
@@ -133,14 +107,14 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
public:
|
||||
WifiLanServerSocket() = default;
|
||||
WifiLanServerSocket(WifiLanServerSocket&&) = default;
|
||||
~WifiLanServerSocket() override;
|
||||
~WifiLanServerSocket() override = default;
|
||||
WifiLanServerSocket& operator=(WifiLanServerSocket&&) = default;
|
||||
|
||||
// Returns ip address.
|
||||
std::string GetIPAddress() const override;
|
||||
|
||||
// Returns port.
|
||||
int GetPort() const override;
|
||||
int GetPort() const override { return server_socket_.GetPort(); };
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
@@ -153,21 +127,20 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
// Called by the server side of a connection before passing ownership of
|
||||
// WifiLanServerSocker to user, to track validity of a pointer to this
|
||||
// server socket.
|
||||
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
|
||||
void SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
|
||||
server_socket_.SetCloseNotifier(std::move(notifier));
|
||||
};
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
Exception Close() override {
|
||||
server_socket_.Close();
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
// Binds to local port
|
||||
bool Listen(int port, bool dual_stack);
|
||||
|
||||
private:
|
||||
mutable absl::Mutex mutex_;
|
||||
// Close notifier
|
||||
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
|
||||
NearbyServerSocket server_socket_;
|
||||
};
|
||||
|
||||
@@ -202,8 +175,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) override;
|
||||
|
||||
std::unique_ptr<api::WifiLanServerSocket> ListenForService(
|
||||
int port) override;
|
||||
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override;
|
||||
|
||||
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
|
||||
override {
|
||||
|
||||
@@ -31,8 +31,6 @@
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
|
||||
|
||||
// Returns the first IP address.
|
||||
std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
// Just pick an IP address from the list of available addresses.
|
||||
@@ -44,9 +42,6 @@ std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
return ipaddr_dotdecimal_to_4bytes_string(ip_addresses.front());
|
||||
}
|
||||
|
||||
// Returns socket port.
|
||||
int WifiLanServerSocket::GetPort() const { return server_socket_.GetPort(); }
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
@@ -64,37 +59,6 @@ std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
return std::make_unique<WifiLanSocket>(std::move(client_socket));
|
||||
}
|
||||
|
||||
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() {
|
||||
absl::AnyInvocable<void()> close_callback;
|
||||
{
|
||||
absl::MutexLock lock(mutex_);
|
||||
VLOG(1) << __func__ << ": Close is called.";
|
||||
if (closed_) {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
LOG(INFO) << __func__ << ": closing blocking socket.";
|
||||
|
||||
server_socket_.Close();
|
||||
closed_ = true;
|
||||
close_callback = std::move(close_notifier_);
|
||||
}
|
||||
|
||||
if (close_callback) {
|
||||
close_callback();
|
||||
}
|
||||
|
||||
LOG(INFO) << __func__ << ": Close completed succesfully.";
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
bool WifiLanServerSocket::Listen(int port, bool dual_stack) {
|
||||
// Listen on all interfaces.
|
||||
SocketAddress address(dual_stack);
|
||||
|
||||
@@ -12,18 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/nullability.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/exception.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/output_stream.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
@@ -36,53 +30,4 @@ WifiLanSocket::WifiLanSocket(
|
||||
input_stream_(client_socket_.get()),
|
||||
output_stream_(client_socket_.get()) {}
|
||||
|
||||
WifiLanSocket::~WifiLanSocket() { Close(); }
|
||||
|
||||
InputStream& WifiLanSocket::GetInputStream() { return input_stream_; }
|
||||
|
||||
OutputStream& WifiLanSocket::GetOutputStream() { return output_stream_; }
|
||||
|
||||
Exception WifiLanSocket::Close() {
|
||||
return client_socket_->Close();
|
||||
}
|
||||
|
||||
bool WifiLanSocket::Connect(const SocketAddress& server_address) {
|
||||
return client_socket_->Connect(server_address);
|
||||
}
|
||||
|
||||
// SocketInputStream
|
||||
WifiLanSocket::SocketInputStream::SocketInputStream(
|
||||
NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
|
||||
ExceptionOr<ByteArray> WifiLanSocket::SocketInputStream::Read(
|
||||
std::int64_t size) {
|
||||
return client_socket_->Read(size);
|
||||
}
|
||||
|
||||
ExceptionOr<size_t> WifiLanSocket::SocketInputStream::Skip(size_t offset) {
|
||||
return client_socket_->Skip(offset);
|
||||
}
|
||||
|
||||
Exception WifiLanSocket::SocketInputStream::Close() {
|
||||
return client_socket_->Close();
|
||||
}
|
||||
|
||||
// SocketOutputStream
|
||||
WifiLanSocket::SocketOutputStream::SocketOutputStream(
|
||||
NearbyClientSocket* absl_nonnull client_socket)
|
||||
: client_socket_(client_socket) {}
|
||||
|
||||
Exception WifiLanSocket::SocketOutputStream::Write(const ByteArray& data) {
|
||||
return client_socket_->Write(data);
|
||||
}
|
||||
|
||||
Exception WifiLanSocket::SocketOutputStream::Flush() {
|
||||
return client_socket_->Flush();
|
||||
}
|
||||
|
||||
Exception WifiLanSocket::SocketOutputStream::Close() {
|
||||
return client_socket_->Close();
|
||||
}
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
Reference in New Issue
Block a user