Skip to content

Commit 8b0cefe

Browse files
committed
reduced code duplication
1 parent c2c1e7c commit 8b0cefe

File tree

7 files changed

+135
-175
lines changed

7 files changed

+135
-175
lines changed

include/libcyphal/transport/can/delegate.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class IRxSessionDelegate
128128
///
129129
virtual void acceptRxTransfer(CanardMemory&& canard_memory,
130130
const TransferRxMetadata& rx_metadata,
131-
const CanardNodeID source_node_id) = 0;
131+
const NodeId source_node_id) = 0;
132132

133133
protected:
134134
IRxSessionDelegate() = default;
@@ -571,7 +571,7 @@ class TransportDelegate
571571

572572
void acceptRxTransfer(CanardMemory&& canard_memory,
573573
const TransferRxMetadata& rx_metadata,
574-
const CanardNodeID source_node_id) override
574+
const NodeId source_node_id) override
575575
{
576576
// This is where de-multiplexing happens: the transfer is forwarded to the appropriate session.
577577
// It's ok not to find the session delegate here - we drop unsolicited transfers.

include/libcyphal/transport/can/msg_rx_session.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ class MessageRxSession final : private IRxSessionDelegate, public IMessageRxSess
132132

133133
void acceptRxTransfer(CanardMemory&& canard_memory,
134134
const TransferRxMetadata& rx_metadata,
135-
const CanardNodeID source_node_id) override
135+
const NodeId source_node_id) override
136136
{
137-
const cetl::optional<NodeId> publisher_node_id = //
138-
source_node_id > CANARD_NODE_ID_MAX //
139-
? cetl::nullopt
140-
: cetl::make_optional<NodeId>(source_node_id);
137+
const cetl::optional<NodeId> publisher_node_id =
138+
source_node_id > CANARD_NODE_ID_MAX ? cetl::nullopt : cetl::make_optional(source_node_id);
141139

142140
const MessageRxMetadata meta{rx_metadata, publisher_node_id};
143141
MessageRxTransfer msg_rx_transfer{meta, ScatteredBuffer{std::move(canard_memory)}};

include/libcyphal/transport/can/svc_rx_sessions.hpp

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010

1111
#include "libcyphal/errors.hpp"
1212
#include "libcyphal/transport/errors.hpp"
13+
#include "libcyphal/transport/svc_rx_session_base.hpp"
1314
#include "libcyphal/transport/svc_sessions.hpp"
14-
#include "libcyphal/transport/types.hpp"
1515
#include "libcyphal/types.hpp"
1616

1717
#include <canard.h>
1818
#include <cetl/cetl.hpp>
1919
#include <cetl/pf17/cetlpf.hpp>
2020

2121
#include <chrono>
22-
#include <cstdint>
23-
#include <utility>
2422

2523
namespace libcyphal
2624
{
@@ -35,85 +33,11 @@ namespace can
3533
namespace detail
3634
{
3735

38-
/// @brief A base class to represent a service RX session.
39-
///
40-
template <typename Interface_, typename Params>
41-
class SvcRxSessionBase : public IRxSessionDelegate, public Interface_
42-
{
43-
public:
44-
SvcRxSessionBase(TransportDelegate& delegate, const Params& params)
45-
: delegate_{delegate}
46-
, params_{params}
47-
{
48-
}
49-
50-
SvcRxSessionBase(const SvcRxSessionBase&) = delete;
51-
SvcRxSessionBase(SvcRxSessionBase&&) noexcept = delete;
52-
SvcRxSessionBase& operator=(const SvcRxSessionBase&) = delete;
53-
SvcRxSessionBase& operator=(SvcRxSessionBase&&) noexcept = delete;
54-
55-
protected:
56-
~SvcRxSessionBase() = default;
57-
58-
TransportDelegate& delegate()
59-
{
60-
return delegate_;
61-
}
62-
63-
// MARK: Interface
64-
65-
CETL_NODISCARD Params getParams() const noexcept final
66-
{
67-
return params_;
68-
}
69-
70-
CETL_NODISCARD cetl::optional<ServiceRxTransfer> receive() final
71-
{
72-
if (last_rx_transfer_)
73-
{
74-
auto transfer = std::move(*last_rx_transfer_);
75-
last_rx_transfer_.reset();
76-
return transfer;
77-
}
78-
return cetl::nullopt;
79-
}
80-
81-
void setOnReceiveCallback(ISvcRxSession::OnReceiveCallback::Function&& function) final
82-
{
83-
on_receive_cb_fn_ = std::move(function);
84-
}
85-
86-
// MARK: IRxSessionDelegate
87-
88-
void acceptRxTransfer(CanardMemory&& canard_memory,
89-
const TransferRxMetadata& rx_metadata,
90-
const CanardNodeID source_node_id) final
91-
{
92-
const ServiceRxMetadata meta{rx_metadata, source_node_id};
93-
ServiceRxTransfer svc_rx_transfer{meta, ScatteredBuffer{std::move(canard_memory)}};
94-
if (on_receive_cb_fn_)
95-
{
96-
on_receive_cb_fn_(ISvcRxSession::OnReceiveCallback::Arg{svc_rx_transfer});
97-
return;
98-
}
99-
(void) last_rx_transfer_.emplace(std::move(svc_rx_transfer));
100-
}
101-
102-
private:
103-
// MARK: Data members:
104-
105-
TransportDelegate& delegate_;
106-
const Params params_;
107-
cetl::optional<ServiceRxTransfer> last_rx_transfer_;
108-
ISvcRxSession::OnReceiveCallback::Function on_receive_cb_fn_;
109-
110-
}; // SvcRxSessionBase
111-
112-
// MARK: -
113-
11436
/// @brief A concrete class to represent a service request RX session (aka server side).
11537
///
116-
class SvcRequestRxSession final : public SvcRxSessionBase<IRequestRxSession, RequestRxParams>
38+
class SvcRequestRxSession final
39+
: public transport::detail:: //
40+
SvcRxSessionBase<IRequestRxSession, IRxSessionDelegate, TransportDelegate, RequestRxParams, CanardMemory>
11741
{
11842
/// @brief Defines private specification for making interface unique ptr.
11943
///
@@ -191,7 +115,9 @@ class SvcRequestRxSession final : public SvcRxSessionBase<IRequestRxSession, Req
191115

192116
/// @brief A concrete class to represent a service response RX session (aka client side).
193117
///
194-
class SvcResponseRxSession final : public SvcRxSessionBase<IResponseRxSession, ResponseRxParams>
118+
class SvcResponseRxSession final
119+
: public transport::detail:: //
120+
SvcRxSessionBase<IResponseRxSession, IRxSessionDelegate, TransportDelegate, ResponseRxParams, CanardMemory>
195121
{
196122
/// @brief Defines private specification for making interface unique ptr.
197123
///
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/// @copyright
2+
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
3+
/// Copyright Amazon.com Inc. or its affiliates.
4+
/// SPDX-License-Identifier: MIT
5+
6+
#ifndef LIBCYPHAL_TRANSPORT_SVC_RX_SESSION_BASE_HPP_INCLUDED
7+
#define LIBCYPHAL_TRANSPORT_SVC_RX_SESSION_BASE_HPP_INCLUDED
8+
9+
#include "scattered_buffer.hpp"
10+
11+
#include <cetl/cetl.hpp>
12+
#include <cetl/pf17/cetlpf.hpp>
13+
14+
#include <utility>
15+
16+
namespace libcyphal
17+
{
18+
namespace transport
19+
{
20+
21+
/// Internal implementation details of a transport.
22+
/// Not supposed to be used directly by the users of the library.
23+
///
24+
namespace detail
25+
{
26+
27+
/// @brief A base template class to represent a service RX session.
28+
///
29+
/// Should be suitable for any transport.
30+
///
31+
template <typename Interface,
32+
typename IRxSessionDelegate,
33+
typename TransportDelegate,
34+
typename Params,
35+
typename LizardMemory>
36+
class SvcRxSessionBase : public IRxSessionDelegate, public Interface
37+
{
38+
public:
39+
SvcRxSessionBase(TransportDelegate& delegate, const Params& params)
40+
: delegate_{delegate}
41+
, params_{params}
42+
{
43+
}
44+
45+
SvcRxSessionBase(const SvcRxSessionBase&) = delete;
46+
SvcRxSessionBase(SvcRxSessionBase&&) noexcept = delete;
47+
SvcRxSessionBase& operator=(const SvcRxSessionBase&) = delete;
48+
SvcRxSessionBase& operator=(SvcRxSessionBase&&) noexcept = delete;
49+
50+
protected:
51+
~SvcRxSessionBase() = default;
52+
53+
TransportDelegate& delegate()
54+
{
55+
return delegate_;
56+
}
57+
58+
// MARK: Interface
59+
60+
CETL_NODISCARD Params getParams() const noexcept final
61+
{
62+
return params_;
63+
}
64+
65+
CETL_NODISCARD cetl::optional<ServiceRxTransfer> receive() final
66+
{
67+
if (last_rx_transfer_)
68+
{
69+
auto transfer = std::move(*last_rx_transfer_);
70+
last_rx_transfer_.reset();
71+
return transfer;
72+
}
73+
return cetl::nullopt;
74+
}
75+
76+
void setOnReceiveCallback(ISvcRxSession::OnReceiveCallback::Function&& function) final
77+
{
78+
on_receive_cb_fn_ = std::move(function);
79+
}
80+
81+
// MARK: IRxSessionDelegate
82+
83+
void acceptRxTransfer(LizardMemory&& lizard_memory,
84+
const TransferRxMetadata& rx_metadata,
85+
const NodeId source_node_id) final
86+
{
87+
const ServiceRxMetadata meta{rx_metadata, source_node_id};
88+
ServiceRxTransfer svc_rx_transfer{meta, ScatteredBuffer{std::move(lizard_memory)}};
89+
if (on_receive_cb_fn_)
90+
{
91+
on_receive_cb_fn_(ISvcRxSession::OnReceiveCallback::Arg{svc_rx_transfer});
92+
return;
93+
}
94+
(void) last_rx_transfer_.emplace(std::move(svc_rx_transfer));
95+
}
96+
97+
private:
98+
// MARK: Data members:
99+
100+
TransportDelegate& delegate_;
101+
const Params params_;
102+
cetl::optional<ServiceRxTransfer> last_rx_transfer_;
103+
ISvcRxSession::OnReceiveCallback::Function on_receive_cb_fn_;
104+
105+
}; // SvcRxSessionBase
106+
107+
} // namespace detail
108+
} // namespace transport
109+
} // namespace libcyphal
110+
111+
#endif // LIBCYPHAL_TRANSPORT_SVC_RX_SESSION_BASE_HPP_INCLUDED

include/libcyphal/transport/udp/delegate.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class IRxSessionDelegate
216216
///
217217
virtual void acceptRxTransfer(UdpardMemory&& udpard_memory,
218218
const TransferRxMetadata& rx_metadata,
219-
const UdpardNodeID source_node_id) = 0;
219+
const NodeId source_node_id) = 0;
220220

221221
protected:
222222
IRxSessionDelegate() = default;
@@ -521,7 +521,7 @@ class TransportDelegate
521521

522522
void acceptRxTransfer(UdpardMemory&& udpard_memory,
523523
const TransferRxMetadata& rx_metadata,
524-
const UdpardNodeID source_node_id) override
524+
const NodeId source_node_id) override
525525
{
526526
// This is where de-multiplexing happens: the transfer is forwarded to the appropriate session.
527527
// It's ok not to find the session delegate here - we drop unsolicited transfers.

include/libcyphal/transport/udp/msg_rx_session.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,10 @@ class MessageRxSession final : private IMsgRxSessionDelegate, public IMessageRxS
144144

145145
void acceptRxTransfer(UdpardMemory&& udpard_memory,
146146
const TransferRxMetadata& rx_metadata,
147-
const UdpardNodeID source_node_id) override
147+
const NodeId source_node_id) override
148148
{
149-
const cetl::optional<NodeId> publisher_node_id = //
150-
source_node_id > UDPARD_NODE_ID_MAX //
151-
? cetl::nullopt
152-
: cetl::make_optional<NodeId>(source_node_id);
149+
const cetl::optional<NodeId> publisher_node_id =
150+
source_node_id > UDPARD_NODE_ID_MAX ? cetl::nullopt : cetl::make_optional(source_node_id);
153151

154152
const MessageRxMetadata meta{rx_metadata, publisher_node_id};
155153
MessageRxTransfer msg_rx_transfer{meta, ScatteredBuffer{std::move(udpard_memory)}};

0 commit comments

Comments
 (0)