// Copyright 2021 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 #include #include #include #include #include #include "internal/flags/nearby_flags.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/nearby_client_socket.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 { namespace windows { WifiLanSocket::WifiLanSocket() { client_socket_ = std::make_unique(); input_stream_ = SocketInputStream(client_socket_.get()); output_stream_ = SocketOutputStream(client_socket_.get()); } 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 socket) { client_socket_ = std::move(socket); input_stream_ = SocketInputStream(client_socket_.get()); output_stream_ = SocketOutputStream(client_socket_.get()); } WifiLanSocket::~WifiLanSocket() { Close(); } 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}; } bool WifiLanSocket::Connect(const std::string& ip_address, int port) { return client_socket_->Connect(ip_address, port); } // SocketInputStream WifiLanSocket::SocketInputStream::SocketInputStream(IInputStream input_stream) { input_stream_ = input_stream; } WifiLanSocket::SocketInputStream::SocketInputStream( NearbyClientSocket* client_socket) { client_socket_ = client_socket; } ExceptionOr 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 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; } 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(); } } // namespace windows } // namespace nearby