converted linux stream to just have simple integer backed fds. I don't understand the ownership heirarchy to safely use smart pointers

This commit is contained in:
Lasan Mahaliyana
2026-06-25 16:03:47 +05:30
parent c77a146edc
commit 2c8a7d8414
6 changed files with 70 additions and 44 deletions
@@ -21,6 +21,7 @@
#include <memory>
#include <optional>
#include <string>
#include <unistd.h>
#include "absl/functional/any_invocable.h"
#include "absl/synchronization/mutex.h"
@@ -37,7 +38,7 @@ class BleL2capSocket;
class BleL2capInputStream : public nearby::InputStream {
public:
BleL2capInputStream(sdbus::UnixFd fd) : stream_(fd) {}
explicit BleL2capInputStream(int fd) : stream_(fd) {}
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override {
@@ -57,7 +58,7 @@ class BleL2capInputStream : public nearby::InputStream {
class BleL2capOutputStream : public nearby::OutputStream {
public:
BleL2capOutputStream(sdbus::UnixFd fd) : stream_(fd) {}
explicit BleL2capOutputStream(int fd) : stream_(fd) {}
Exception Write(absl::string_view data) override;
Exception Flush() override;
@@ -78,7 +79,7 @@ class BleL2capSocket final : public api::ble::BleL2capSocket {
public:
BleL2capSocket(int fd, api::ble::BlePeripheral::UniqueId peripheral_id,
std::string service_id = "")
: fd_(sdbus::UnixFd(fd)),
: fd_(fd),
output_stream_(fd_),
input_stream_(fd_),
peripheral_id_(peripheral_id) {};
@@ -88,6 +89,10 @@ class BleL2capSocket final : public api::ble::BleL2capSocket {
Exception Close() override {
input_stream_.Close();
output_stream_.Close();
if (fd_ >= 0) {
close(fd_);
fd_ = -1;
}
return Exception{Exception::kSuccess};
};
@@ -96,7 +101,7 @@ class BleL2capSocket final : public api::ble::BleL2capSocket {
}
private:
sdbus::UnixFd fd_;
int fd_;
BleL2capOutputStream output_stream_;
BleL2capInputStream input_stream_;
api::ble::BlePeripheral::UniqueId peripheral_id_;
@@ -21,6 +21,7 @@
#include <sdbus-c++/Types.h>
#include <sys/poll.h>
#include <unistd.h>
#include <systemd/sd-bus.h>
#include "absl/synchronization/mutex.h"
@@ -38,20 +39,27 @@ class BluetoothSocket final : public api::BluetoothSocket {
public:
BluetoothSocket(std::shared_ptr<BluetoothDevice> device,
sdbus::UnixFd fd)
:fd_(fd), device_(std::move(device)), output_stream_(fd_), input_stream_(fd_) {}
: fd_(fd.release()),
device_(std::move(device)),
output_stream_(fd_),
input_stream_(fd_) {}
InputStream &GetInputStream() override { return input_stream_; }
OutputStream &GetOutputStream() override { return output_stream_; }
Exception Close() override {
input_stream_.Close();
output_stream_.Close();
if (fd_ >= 0) {
close(fd_);
fd_ = -1;
}
return Exception{Exception::kSuccess};
}
api::BluetoothDevice *GetRemoteDevice() override { return device_.get(); };
private:
sdbus::UnixFd fd_;
int fd_;
std::shared_ptr<BluetoothDevice> device_;
OutputStream output_stream_;
InputStream input_stream_;
@@ -15,10 +15,9 @@
#include <sys/poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <unistd.h>
#include <array>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include "absl/strings/escaping.h"
#include "internal/platform/byte_array.h"
@@ -34,7 +33,7 @@ ExceptionOr<ByteArray> InputStream::Read(std::int64_t size) {
return ExceptionOr<ByteArray>(ByteArray(std::string()));
}
if (!fd_ || !fd_->isValid()) {
if (closed_ || fd_ < 0) {
return {Exception::kIo};
}
@@ -43,7 +42,7 @@ ExceptionOr<ByteArray> InputStream::Read(std::int64_t size) {
while (true) {
pollfd pfd{};
pfd.fd = fd_->get();
pfd.fd = fd_;
pfd.events = POLLIN;
int poll_result = poll(&pfd, 1, -1);
@@ -64,7 +63,7 @@ ExceptionOr<ByteArray> InputStream::Read(std::int64_t size) {
}
if (pfd.revents & (POLLIN | POLLHUP)) {
ssize_t bytes_read = recv(fd_->get(), buffer.data(), buffer.size(), 0);
ssize_t bytes_read = recv(fd_, buffer.data(), buffer.size(), 0);
if (bytes_read > 0) {
buffer.resize(static_cast<std::size_t>(bytes_read));
@@ -93,17 +92,18 @@ ExceptionOr<ByteArray> InputStream::Read(std::int64_t size) {
}
Exception InputStream::Close() {
if (!fd_->isValid()) return Exception{Exception::kIo};
fd_.reset();
return {};
if (closed_ || fd_ < 0) return Exception{Exception::kSuccess};
closed_ = true;
shutdown(fd_, SHUT_RD);
return Exception{Exception::kSuccess};
}
Exception OutputStream::Write(absl::string_view data) {
if (!fd_ || !fd_->isValid()) {
if (closed_ || fd_ < 0) {
return {Exception::kIo};
}
const int fd = fd_->get();
const int fd = fd_;
size_t sent = 0;
while (sent < data.size()) {
@@ -165,12 +165,10 @@ Exception OutputStream::Flush() {
}
Exception OutputStream::Close() {
if (!fd_->isValid()) return Exception{Exception::kIo};
auto ret = close(fd_->get()) < 0 ? Exception{Exception::kIo}
: Exception{Exception::kSuccess};
fd_.reset();
return ret;
if (closed_ || fd_ < 0) return Exception{Exception::kSuccess};
closed_ = true;
shutdown(fd_, SHUT_WR);
return Exception{Exception::kSuccess};
}
} // namespace linux
@@ -15,10 +15,6 @@
#ifndef PLATFORM_IMPL_LINUX_STREAM_H_
#define PLATFORM_IMPL_LINUX_STREAM_H_
#include <optional>
#include <sdbus-c++/Types.h>
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
@@ -26,26 +22,28 @@ namespace nearby {
namespace linux {
class InputStream : public nearby::InputStream {
public:
explicit InputStream(sdbus::UnixFd fd) : fd_(std::make_shared<sdbus::UnixFd>(fd)){};
explicit InputStream(int fd) : fd_(fd){};
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override;
private:
std::shared_ptr<sdbus::UnixFd> fd_;
int fd_;
bool closed_ = false;
};
class OutputStream : public nearby::OutputStream {
public:
explicit OutputStream(sdbus::UnixFd fd) : fd_(std::make_shared<sdbus::UnixFd>(fd)){};
explicit OutputStream(int fd) : fd_(fd){};
Exception Write(absl::string_view data) override;
Exception Flush() override;
Exception Close() override;
private:
std::shared_ptr<sdbus::UnixFd> fd_;
int fd_;
bool closed_ = false;
};
} // namespace linux
@@ -18,9 +18,10 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <atomic>
#include <cerrno>
#include <cstring>
#include <functional>
#include <sdbus-c++/Types.h>
#include <unistd.h>
#include "internal/platform/exception.h"
#include "internal/platform/implementation/linux/stream.h"
@@ -30,8 +31,8 @@ namespace nearby {
namespace linux {
class TCPSocket {
public:
explicit TCPSocket(const sdbus::UnixFd& fd)
: closed_(false), output_stream_(fd), input_stream_(fd) {}
explicit TCPSocket(int fd)
: fd_(fd), closed_(false), output_stream_(fd), input_stream_(fd) {}
static std::optional<TCPSocket> Connect(const std::string& ip_address,
int port) {
@@ -54,10 +55,11 @@ class TCPSocket {
if (ret < 0) {
LOG(ERROR) << __func__ << ": Error connecting to socket: "
<< std::strerror(errno);
close(sock);
return std::nullopt;
}
return TCPSocket(sdbus::UnixFd(sock));
return TCPSocket(sock);
}
InputStream& GetInputStream() { return input_stream_; }
@@ -69,11 +71,16 @@ class TCPSocket {
closed_ = true;
input_stream_.Close();
output_stream_.Close();
if (fd_ >= 0) {
close(fd_);
fd_ = -1;
}
return {Exception::kSuccess};
};
private:
int fd_;
bool closed_;
OutputStream output_stream_;
@@ -125,19 +132,23 @@ class TCPServerSocket {
socklen_t len = sizeof(addr);
auto conn =
accept(fd_.get(), reinterpret_cast<struct sockaddr*>(&addr), &len);
accept(fd_, reinterpret_cast<struct sockaddr*>(&addr), &len);
if (conn < 0) {
LOG(ERROR) << __func__
<< ": Error accepting incoming connections on socket "
<< fd_.get() << ": " << std::strerror(errno);
<< fd_ << ": " << std::strerror(errno);
return std::nullopt;
}
return TCPSocket(sdbus::UnixFd(conn));
return TCPSocket(conn);
};
Exception Close() {
int fd = fd_.release();
if (fd_ < 0) {
return {Exception::kSuccess};
}
int fd = fd_;
fd_ = -1;
shutdown(fd, SHUT_RDWR);
auto ret = close(fd);
if (ret < 0) {
@@ -153,11 +164,11 @@ class TCPServerSocket {
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
auto ret =
getsockname(fd_.get(), reinterpret_cast<struct sockaddr*>(&sin), &len);
getsockname(fd_, reinterpret_cast<struct sockaddr*>(&sin), &len);
if (ret < 0) {
LOG(ERROR) << __func__
<< ": Error getting information for socket "
<< fd_.get() << ": " << std::strerror(errno);
<< fd_ << ": " << std::strerror(errno);
return 0;
}
@@ -165,7 +176,7 @@ class TCPServerSocket {
}
private:
sdbus::UnixFd fd_;
int fd_;
};
} // namespace linux
} // namespace nearby
@@ -18,12 +18,14 @@
#include "internal/platform/implementation/linux/stream.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include <unistd.h>
namespace nearby {
namespace linux {
class WifiHotspotSocket : public api::WifiHotspotSocket {
public:
explicit WifiHotspotSocket(int connection_fd)
: fd_(sdbus::UnixFd(connection_fd)),
: fd_(connection_fd),
output_stream_(fd_),
input_stream_(fd_) {}
@@ -32,12 +34,16 @@ class WifiHotspotSocket : public api::WifiHotspotSocket {
Exception Close() override {
input_stream_.Close();
output_stream_.Close();
if (fd_ >= 0) {
close(fd_);
fd_ = -1;
}
return Exception{Exception::kSuccess};
};
private:
sdbus::UnixFd fd_;
int fd_;
OutputStream output_stream_;
InputStream input_stream_;
};