Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support blocking socket in Wi-Fi LAN medium #3162

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions internal/platform/implementation/windows/wifi_lan.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
#include "internal/platform/exception.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/implementation/windows/nearby_client_socket.h"
#include "internal/platform/implementation/windows/nearby_server_socket.h"
#include "internal/platform/implementation/windows/scheduled_executor.h"
#include "internal/platform/implementation/windows/wifi_lan_mdns.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/mutex.h"
#include "internal/platform/nsd_service_info.h"
Expand Down Expand Up @@ -98,7 +101,9 @@ using winrt::Windows::Storage::Streams::IOutputStream;
// remote WiFi LAN service, also will return a WifiLanSocket to caller.
class WifiLanSocket : public api::WifiLanSocket {
public:
WifiLanSocket();
explicit WifiLanSocket(StreamSocket socket);
explicit WifiLanSocket(std::unique_ptr<NearbyClientSocket> socket);
WifiLanSocket(WifiLanSocket&) = default;
WifiLanSocket(WifiLanSocket&&) = default;
~WifiLanSocket() override;
Expand All @@ -122,40 +127,51 @@ class WifiLanSocket : public api::WifiLanSocket {
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;

bool Connect(const std::string& ip_address, int port);

private:
// A simple wrapper to handle input stream of socket
class SocketInputStream : public InputStream {
public:
SocketInputStream(IInputStream input_stream);
explicit SocketInputStream(IInputStream input_stream);
explicit SocketInputStream(NearbyClientSocket* client_socket);
~SocketInputStream() = default;

ExceptionOr<ByteArray> Read(std::int64_t size) override;
ExceptionOr<size_t> Skip(size_t offset) override;
Exception Close() override;

private:
bool enable_blocking_socket_ = false;
IInputStream input_stream_{nullptr};
Buffer read_buffer_{nullptr};
NearbyClientSocket* client_socket_{nullptr};
};

// A simple wrapper to handle output stream of socket
class SocketOutputStream : public OutputStream {
public:
SocketOutputStream(IOutputStream output_stream);
explicit SocketOutputStream(IOutputStream output_stream);
explicit SocketOutputStream(NearbyClientSocket* client_socket);
~SocketOutputStream() = default;

Exception Write(const ByteArray& data) override;
Exception Flush() override;
Exception Close() override;

private:
bool enable_blocking_socket_ = false;
IOutputStream output_stream_{nullptr};
NearbyClientSocket* client_socket_{nullptr};
};

// Internal properties
StreamSocket stream_soket_{nullptr};
SocketInputStream input_stream_{nullptr};
SocketOutputStream output_stream_{nullptr};

bool enable_blocking_socket_ = false;
std::unique_ptr<NearbyClientSocket> client_socket_;
};

// WifiLanServerSocket provides the support to server socket, this server socket
Expand Down Expand Up @@ -222,6 +238,10 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
// Cache socket not be picked by upper layer
int port_ = 0;
bool closed_ = false;

// Flag to enable blocking socket.
bool enable_blocking_socket_ = false;
NearbyServerSocket server_socket_;
};

// Container of operations that can be performed over the WifiLan medium.
Expand Down Expand Up @@ -344,6 +364,9 @@ class WifiLanMedium : public api::WifiLanMedium {
// Used to keep the service name is advertising.
std::string service_name_;

// mDNS service
WifiLanMdns wifi_lan_mdns_;

// Keep the server sockets listener pointer
absl::flat_hash_map<int /* port number of the WifiLanServerSocket*/,
WifiLanServerSocket*>
Expand Down
Loading
Loading