diff --git a/connections/clients/windows/dart/core_adapter_dart.cc b/connections/clients/windows/dart/core_adapter_dart.cc index 95d34424..5534a37d 100644 --- a/connections/clients/windows/dart/core_adapter_dart.cc +++ b/connections/clients/windows/dart/core_adapter_dart.cc @@ -627,7 +627,7 @@ void SendPayloadDart(Core *pCore, const char *endpoint_id, NEARBY_LOG(INFO, "File name: %s, size %d", payload_dart.data, payload_dart.size); std::string file_name_str(payload_dart.data); - InputFileW input_file(file_name_str, payload_dart.size); + InputFileW input_file(file_name_str.c_str(), payload_dart.size); PayloadW payload(input_file); std::vector c_string_array; diff --git a/connections/clients/windows/file_w.cc b/connections/clients/windows/file_w.cc index a76ce35c..fc611995 100644 --- a/connections/clients/windows/file_w.cc +++ b/connections/clients/windows/file_w.cc @@ -13,9 +13,13 @@ // limitations under the License. #include "connections/clients/windows/file_w.h" +#include + #include "internal/platform/file.h" namespace location::nearby { +void InputFileDeleter::operator()(nearby::InputFile* p) { delete p; } +void OutputFileDeleter::operator()(nearby::OutputFile* p) { delete p; } namespace windows { InputFileW::InputFileW(InputFile* input_file) @@ -24,14 +28,19 @@ InputFileW::InputFileW(InputFile* input_file) InputFileW::InputFileW(PayloadId payload_id, size_t size) : impl_(std::unique_ptr( new nearby::InputFile(payload_id, size))) {} -InputFileW::InputFileW(std::string file_path, size_t size) +InputFileW::InputFileW(const char* file_path, size_t size) : impl_(std::unique_ptr( new nearby::InputFile(file_path, size))) {} InputFileW::InputFileW(InputFileW&& other) noexcept : impl_(std::move(other.impl_)) {} // Returns a string that uniquely identifies this file. -std::string InputFileW::GetFilePath() const { return impl_->GetFilePath(); } +// Caller allocates buffer[MAX_PATH] and is responsible +// for freeing. +void InputFileW::GetFilePath(char* file_path) const { + std::string fp = impl_->GetFilePath(); + strncpy(file_path, fp.c_str(), fp.length()); +} // Returns total size of this file in bytes. size_t InputFileW::GetTotalSize() const { return impl_->GetTotalSize(); } @@ -42,7 +51,7 @@ InputFileW::GetImpl() { } OutputFileW::OutputFileW(PayloadId payload_id) {} -OutputFileW::OutputFileW(std::string file_path) {} +OutputFileW::OutputFileW(const char* file_path) {} OutputFileW::OutputFileW(OutputFileW&&) noexcept {} OutputFileW& OutputFileW::operator=(OutputFileW&& other) noexcept { impl_ = std::move(other.impl_); diff --git a/connections/clients/windows/file_w.h b/connections/clients/windows/file_w.h index c1def01b..7225abc2 100644 --- a/connections/clients/windows/file_w.h +++ b/connections/clients/windows/file_w.h @@ -18,43 +18,69 @@ #include #include "connections/clients/windows/dll_config.h" -#include "internal/platform/file.h" #include "internal/platform/payload_id.h" -namespace location::nearby::windows { +namespace location { +namespace nearby { +class InputFile; +struct InputFileDeleter { + void operator()(InputFile* p); +}; + +class OutputFile; +struct OutputFileDeleter { + void operator()(OutputFile* p); +}; + +} // namespace nearby +} // namespace location + +namespace location { +namespace nearby { +namespace windows { class DLL_API InputFileW { public: - InputFileW(InputFile* input_file); - InputFileW(PayloadId payload_id, size_t size); - InputFileW(std::string file_path, size_t size); + explicit InputFileW(location::nearby::InputFile* input_file); + InputFileW(location::nearby::PayloadId payload_id, size_t size); + InputFileW(const char* file_path, size_t size); InputFileW(InputFileW&&) noexcept; // Returns a string that uniquely identifies this file. - std::string GetFilePath() const; + void GetFilePath(char* file_path) const; // Returns total size of this file in bytes. size_t GetTotalSize() const; - std::unique_ptr GetImpl(); + std::unique_ptr + GetImpl(); private: - std::unique_ptr impl_; + std::unique_ptr + impl_; }; class DLL_API OutputFileW { public: - explicit OutputFileW(PayloadId payload_id); - explicit OutputFileW(std::string file_path); + explicit OutputFileW(location::nearby::PayloadId payload_id); + explicit OutputFileW(const char* file_path); OutputFileW(OutputFileW&&) noexcept; OutputFileW& operator=(OutputFileW&&) noexcept; - std::unique_ptr GetImpl(); + std::unique_ptr + GetImpl(); private: - std::unique_ptr impl_; + std::unique_ptr + impl_; }; -} // namespace location::nearby::windows +} // namespace windows +} // namespace nearby +} // namespace location #endif // THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_FILE_W_H_ diff --git a/connections/clients/windows/input_stream_w.cc b/connections/clients/windows/input_stream_w.cc new file mode 100644 index 00000000..bf3b3e03 --- /dev/null +++ b/connections/clients/windows/input_stream_w.cc @@ -0,0 +1,52 @@ +// Copyright 2022 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 "connections/clients/windows/input_stream_w.h" + +#include "internal/platform/input_stream.h" + +namespace location::nearby { +void InputStreamDeleter::operator()(nearby::InputStream* p) { delete p; } +} // namespace location::nearby + +namespace location { +namespace nearby { +namespace windows { + +char* InputStreamW::Read(size_t size) { + auto result = impl_->Read(size); + if (result.ok()) { + return result.GetResult().data(); + } + return nullptr; +} + +int64_t InputStreamW::Skip(size_t offset) { + auto result = impl_->Skip(offset); + if (result.ok()) { + return result.GetResult(); + } + return -1; +} + +int64_t InputStreamW::Close() { + auto result = impl_->Close(); + if (result.Ok()) { + return 0; + } + return -1; +} + +} // namespace windows +} // namespace nearby +} // namespace location diff --git a/connections/clients/windows/input_stream_w.h b/connections/clients/windows/input_stream_w.h new file mode 100644 index 00000000..fbce8305 --- /dev/null +++ b/connections/clients/windows/input_stream_w.h @@ -0,0 +1,51 @@ +// Copyright 2022 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. +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_INPUT_STREAM_W_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_INPUT_STREAM_W_H_ + +#include + +namespace location { +namespace nearby { + +class InputStream; +struct InputStreamDeleter { + void operator()(InputStream* p); +}; +} // namespace nearby +} // namespace location + +namespace location { +namespace nearby { +namespace windows { + +class InputStreamW { + public: + char* Read(size_t size); + + // throws Exception::kIo + int64_t Skip(size_t offset); + + // throws Exception::kIo + int64_t Close(); + + private: + std::unique_ptr impl_; +}; + +} // namespace windows +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_INPUT_STREAM_W_H_ diff --git a/connections/clients/windows/payload_w.h b/connections/clients/windows/payload_w.h index 4b4e60d0..5b1a5cb2 100644 --- a/connections/clients/windows/payload_w.h +++ b/connections/clients/windows/payload_w.h @@ -25,16 +25,29 @@ #include "connections/payload_type.h" #include "internal/platform/payload_id.h" -namespace location::nearby { -// Forward declarations +namespace location { +namespace nearby { namespace connections { + class Payload; struct PayloadDeleter { void operator()(Payload* p); }; } // namespace connections +} // namespace nearby +} // namespace location + +namespace location { +namespace nearby { + class InputFile; class InputStream; + +} // namespace nearby +} // namespace location + +namespace location { +namespace nearby { namespace windows { extern "C" { @@ -78,7 +91,7 @@ class DLL_API PayloadW { int64_t GetId() const; // Returns Payload type. - const connections::PayloadType GetType() const; + const location::nearby::connections::PayloadType GetType() const; // Sets the payload offset in bytes void SetOffset(size_t offset); @@ -98,7 +111,9 @@ class DLL_API PayloadW { }; } // extern "C" + } // namespace windows -} // namespace location::nearby +} // namespace nearby +} // namespace location #endif // THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_PAYLOAD_W_H_ diff --git a/internal/platform/file.cc b/internal/platform/file.cc index 5e077515..c1516467 100644 --- a/internal/platform/file.cc +++ b/internal/platform/file.cc @@ -16,8 +16,6 @@ namespace location { namespace nearby { -void InputFileDeleter::operator()(nearby::InputFile* p) { delete p; } -void OutputFileDeleter::operator()(nearby::OutputFile* p) { delete p; } InputFile::InputFile(PayloadId id, std::int64_t size) : impl_(Platform::CreateInputFile(id, size)) {} diff --git a/internal/platform/file.h b/internal/platform/file.h index 84c1af69..ff08fbce 100644 --- a/internal/platform/file.h +++ b/internal/platform/file.h @@ -29,15 +29,6 @@ namespace location { namespace nearby { -class InputFile; -struct InputFileDeleter { - void operator()(InputFile* p); -}; - -class OutputFile; -struct OutputFileDeleter { - void operator()(nearby::OutputFile* p); -}; class InputFile final { public: