Skip to content

Commit

Permalink
[#264] Expose file descriptor in C++ bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Nov 25, 2024
1 parent 3d5f942 commit 2e31e7a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "iox/function.hpp"
#include "iox/optional.hpp"
#include "iox2/event_id.hpp"
#include "iox2/file_descriptor.hpp"
#include "iox2/internal/iceoryx2.hpp"
#include "iox2/listener_error.hpp"
#include "iox2/service_type.hpp"
Expand Down Expand Up @@ -80,6 +81,33 @@ class Listener {
/// in detail.
auto blocking_wait_one() -> iox::expected<iox::optional<EventId>, ListenerWaitError>;

private:
class Unsafe {
public:
Unsafe(const Unsafe&) noexcept = delete;
Unsafe(Unsafe&&) noexcept = delete;
Unsafe& operator=(const Unsafe&) noexcept = delete;
Unsafe& operator=(Unsafe&&) noexcept = delete;

~Unsafe() noexcept = default;

/// Returns a file descriptor to the underlying Listener. The file descriptor must not be closed and only be
/// used for event multiplexing!
auto file_descriptor() && -> iox::optional<FileDescriptor>;

private:
friend class Listener;
Unsafe(Listener& listener);

private:
const Listener& m_self;
};

public:
/// This function gives access to unsafe operations on the Listener. The returned object must never be stored but
/// just used to access the unsafe functions.
auto unsafe() -> Unsafe;

private:
template <ServiceType>
friend class PortFactoryListener;
Expand Down
16 changes: 16 additions & 0 deletions iceoryx2-ffi/cxx/src/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ auto Listener<S>::blocking_wait_one() -> iox::expected<iox::optional<EventId>, L
return iox::err(iox::into<ListenerWaitError>(result));
}

template <ServiceType S>
auto Listener<S>::unsafe() -> Unsafe {
return Unsafe(*this);
}

template <ServiceType S>
Listener<S>::Unsafe::Unsafe(Listener& listener)
: m_self { listener } {
}

template <ServiceType S>
auto Listener<S>::Unsafe::file_descriptor() && -> iox::optional<iox2::FileDescriptor> {
auto fd = iox2_listener_get_file_descriptor(&m_self.m_handle);
return FileDescriptor::create_non_owning(fd->value);
}

template class Listener<ServiceType::Ipc>;
template class Listener<ServiceType::Local>;
} // namespace iox2

0 comments on commit 2e31e7a

Please sign in to comment.