mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Move stream implementation to its own file.
This commit is contained in:
@@ -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:
|
||||
|
||||
+16
-25
@@ -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<ByteArray> 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
|
||||
@@ -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<ByteArray> Read(std::int64_t size) override;
|
||||
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
std::optional<sdbus::UnixFd> 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<sdbus::UnixFd> fd_;
|
||||
sdbus::UnixFd fd_;
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
|
||||
Reference in New Issue
Block a user