diff --git a/internal/platform/implementation/linux/bluetooth_classic_socket.h b/internal/platform/implementation/linux/bluetooth_classic_socket.h index d69676a4..b7e49606 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_socket.h +++ b/internal/platform/implementation/linux/bluetooth_classic_socket.h @@ -34,7 +34,12 @@ class BluetoothSocket final : public api::BluetoothSocket { nearby::InputStream &GetInputStream() override { return input_stream_; } nearby::OutputStream &GetOutputStream() override { return output_stream_; } - Exception Close() override; + Exception Close() override { + input_stream_.Close(); + output_stream_.Close(); + + return Exception{Exception::kSuccess}; + } api::BluetoothDevice *GetRemoteDevice() override { return &device_; }; private: diff --git a/internal/platform/implementation/linux/bluetooth_classic_socket.cc b/internal/platform/implementation/linux/stream.cc similarity index 61% rename from internal/platform/implementation/linux/bluetooth_classic_socket.cc rename to internal/platform/implementation/linux/stream.cc index 35c4d921..2f389fab 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/linux/stream.cc @@ -19,42 +19,40 @@ #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/linux/bluetooth_classic_socket.h" +#include "internal/platform/implementation/linux/stream.h" namespace nearby { namespace linux { ExceptionOr InputStream::Read(std::int64_t size) { - if (!fd_.has_value()) return Exception::kIo; + if (!fd_.isValid()) return {Exception::kIo}; - char *data = new char[size]; - ssize_t ret = read(fd_->get(), data, size); + std::string buffer; + buffer.resize(size); + ssize_t ret = read(fd_.get(), buffer.data(), buffer.size()); if (ret == 0) { - delete[] data; return ExceptionOr(ByteArray()); - } else if (ret < 0) { - delete[] data; - return Exception::kIo; } + if (ret < 0) { + return {Exception::kIo}; + } + buffer.resize(ret); - return ExceptionOr(ByteArray(data, size)); + return ExceptionOr(ByteArray(std::move(buffer))); } Exception InputStream::Close() { - if (!fd_.has_value()) return Exception{Exception::kIo}; - - auto ret = close(fd_->get()) < 0 ? Exception{Exception::kIo} - : Exception{Exception::kSuccess}; + if (!fd_.isValid()) return Exception{Exception::kIo}; fd_.reset(); - return ret; + return {}; } Exception OutputStream::Write(const ByteArray &data) { - if (!fd_.has_value()) return Exception{Exception::kIo}; + if (!fd_.isValid()) return Exception{Exception::kIo}; size_t written = 0; while (written < data.size()) { - ssize_t ret = write(fd_->get(), data.data(), data.size()); + ssize_t ret = write(fd_.get(), data.data(), data.size()); if (ret < 1) { return Exception{Exception::kIo}; } @@ -66,20 +64,13 @@ Exception OutputStream::Write(const ByteArray &data) { Exception OutputStream::Flush() { return Exception{Exception::kSuccess}; } Exception OutputStream::Close() { - if (!fd_.has_value()) return Exception{Exception::kIo}; + if (!fd_.isValid()) return Exception{Exception::kIo}; - auto ret = close(fd_->get()) < 0 ? Exception{Exception::kIo} + auto ret = close(fd_.get()) < 0 ? Exception{Exception::kIo} : Exception{Exception::kSuccess}; fd_.reset(); return ret; } -Exception BluetoothSocket::Close() { - input_stream_.Close(); - output_stream_.Close(); - - return Exception{Exception::kSuccess}; -} - } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/stream.h b/internal/platform/implementation/linux/stream.h index a205faee..8034fa87 100644 --- a/internal/platform/implementation/linux/stream.h +++ b/internal/platform/implementation/linux/stream.h @@ -26,26 +26,26 @@ namespace nearby { namespace linux { class InputStream : public nearby::InputStream { public: - InputStream(sdbus::UnixFd &fd) : fd_(fd){}; + explicit InputStream(sdbus::UnixFd fd) : fd_(std::move(fd)){}; ExceptionOr Read(std::int64_t size) override; Exception Close() override; private: - std::optional fd_; + sdbus::UnixFd fd_; }; class OutputStream : public nearby::OutputStream { public: - OutputStream(sdbus::UnixFd &fd) : fd_(fd){}; + explicit OutputStream(sdbus::UnixFd fd) : fd_(std::move(fd)){}; Exception Write(const ByteArray &data) override; Exception Flush() override; Exception Close() override; private: - std::optional fd_; + sdbus::UnixFd fd_; }; } // namespace linux