Skip to content

Commit 8dca2c5

Browse files
mspangisiu-apple
authored andcommitted
Fix compilation of transport/SessionHandle.h (project-chip#21765)
Currently SessionHandle, SessionHolder, and Session must be defined in order. Any other order will not compile. Merge these headers in order to satisfy the following objectives: 1) All headers compile in isolation 2) Header order inclusion does not matter This is the simplest way to fix the problem, and seems to be the only way to fix it without changing the class definitions.
1 parent 6bcd768 commit 8dca2c5

File tree

4 files changed

+127
-143
lines changed

4 files changed

+127
-143
lines changed

src/transport/Session.h

+125-1
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,140 @@
1616

1717
#pragma once
1818

19+
#include <access/SubjectDescriptor.h>
1920
#include <credentials/FabricTable.h>
2021
#include <lib/core/CHIPConfig.h>
22+
#include <lib/core/Optional.h>
2123
#include <lib/core/PeerId.h>
2224
#include <lib/core/ScopedNodeId.h>
25+
#include <lib/support/IntrusiveList.h>
26+
#include <lib/support/ReferenceCountedHandle.h>
2327
#include <messaging/ReliableMessageProtocolConfig.h>
2428
#include <platform/LockTracker.h>
25-
#include <transport/SessionHolder.h>
29+
#include <transport/SessionDelegate.h>
2630
#include <transport/raw/PeerAddress.h>
2731

2832
namespace chip {
33+
namespace Transport {
34+
class Session;
35+
} // namespace Transport
36+
37+
/** @brief
38+
* Non-copyable session reference. All SessionHandles are created within SessionManager. It is not allowed to store SessionHandle
39+
* anywhere except for function arguments and return values.
40+
*
41+
* SessionHandle is reference counted such that it is never dangling, but there can be a gray period when the session is marked
42+
* as pending removal but not actually removed yet. During this period, the handle is functional, but the underlying session
43+
* won't be able to be grabbed by any SessionHolder. SessionHandle->IsActiveSession can be used to check if the session is
44+
* active.
45+
*/
46+
class SessionHandle
47+
{
48+
public:
49+
SessionHandle(Transport::Session & session) : mSession(session) {}
50+
~SessionHandle() {}
51+
52+
SessionHandle(const SessionHandle &) = delete;
53+
SessionHandle operator=(const SessionHandle &) = delete;
54+
SessionHandle(SessionHandle &&) = default;
55+
SessionHandle & operator=(SessionHandle &&) = delete;
56+
57+
bool operator==(const SessionHandle & that) const { return &mSession.Get() == &that.mSession.Get(); }
58+
59+
Transport::Session * operator->() const { return mSession.operator->(); }
60+
61+
private:
62+
friend class SessionHolder;
63+
ReferenceCountedHandle<Transport::Session> mSession;
64+
};
65+
66+
/** @brief
67+
* Managed session reference. The object is used to store a session, the stored session will be automatically
68+
* released when the underlying session is released. One must verify it is available before use. The object can be
69+
* created using SessionHandle.Grab()
70+
*/
71+
class SessionHolder : public IntrusiveListNodeBase<>
72+
{
73+
public:
74+
SessionHolder() {}
75+
SessionHolder(const SessionHandle & handle) { Grab(handle); }
76+
virtual ~SessionHolder();
77+
78+
SessionHolder(const SessionHolder &);
79+
SessionHolder(SessionHolder && that);
80+
SessionHolder & operator=(const SessionHolder &);
81+
SessionHolder & operator=(SessionHolder && that);
82+
83+
virtual void SessionReleased() { Release(); }
84+
virtual void ShiftToSession(const SessionHandle & session)
85+
{
86+
Release();
87+
Grab(session);
88+
}
89+
90+
bool Contains(const SessionHandle & session) const
91+
{
92+
return mSession.HasValue() && &mSession.Value().Get() == &session.mSession.Get();
93+
}
94+
95+
bool GrabPairingSession(const SessionHandle & session); // Should be only used inside CASE/PASE pairing.
96+
bool Grab(const SessionHandle & session);
97+
void Release();
98+
99+
explicit operator bool() const { return mSession.HasValue(); }
100+
Optional<SessionHandle> Get() const
101+
{
102+
//
103+
// We cannot return mSession directly even if Optional<SessionHandle> is internally composed of the same bits,
104+
// since they are not actually equivalent type-wise, and SessionHandle does not permit copy-construction.
105+
//
106+
// So, construct a new Optional<SessionHandle> from the underlying Transport::Session reference.
107+
//
108+
return mSession.HasValue() ? chip::MakeOptional<SessionHandle>(mSession.Value().Get())
109+
: chip::Optional<SessionHandle>::Missing();
110+
}
111+
112+
Transport::Session * operator->() const { return &mSession.Value().Get(); }
113+
114+
// There is not delegate, nothing to do here
115+
virtual void DispatchSessionEvent(SessionDelegate::Event event) {}
116+
117+
protected:
118+
// Helper for use by the Grab methods.
119+
void GrabUnchecked(const SessionHandle & session);
120+
121+
Optional<ReferenceCountedHandle<Transport::Session>> mSession;
122+
};
123+
124+
/// @brief Extends SessionHolder to allow propagate SessionDelegate::* events to a given destination
125+
class SessionHolderWithDelegate : public SessionHolder
126+
{
127+
public:
128+
SessionHolderWithDelegate(SessionDelegate & delegate) : mDelegate(delegate) {}
129+
SessionHolderWithDelegate(const SessionHandle & handle, SessionDelegate & delegate) : SessionHolder(handle), mDelegate(delegate)
130+
{}
131+
operator bool() const { return SessionHolder::operator bool(); }
132+
133+
void SessionReleased() override
134+
{
135+
Release();
136+
137+
// Note, the session is already cleared during mDelegate.OnSessionReleased
138+
mDelegate.OnSessionReleased();
139+
}
140+
141+
void ShiftToSession(const SessionHandle & session) override
142+
{
143+
if (mDelegate.GetNewSessionHandlingPolicy() == SessionDelegate::NewSessionHandlingPolicy::kShiftToNewSession)
144+
SessionHolder::ShiftToSession(session);
145+
}
146+
147+
void DispatchSessionEvent(SessionDelegate::Event event) override { (mDelegate.*event)(); }
148+
149+
private:
150+
SessionDelegate & mDelegate;
151+
};
152+
29153
namespace Transport {
30154

31155
class SecureSession;

src/transport/SessionHandle.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include <transport/SecureSession.h>
1918
#include <transport/SessionHandle.h>
20-
#include <transport/SessionManager.h>
21-
22-
namespace chip {
23-
24-
using namespace Transport;
25-
26-
} // namespace chip

src/transport/SessionHandle.h

+1-39
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,4 @@
1717

1818
#pragma once
1919

20-
#include <access/SubjectDescriptor.h>
21-
#include <lib/support/ReferenceCountedHandle.h>
22-
23-
namespace chip {
24-
25-
namespace Transport {
26-
class Session;
27-
} // namespace Transport
28-
29-
/** @brief
30-
* Non-copyable session reference. All SessionHandles are created within SessionManager. It is not allowed to store SessionHandle
31-
* anywhere except for function arguments and return values.
32-
*
33-
* SessionHandle is reference counted such that it is never dangling, but there can be a gray period when the session is marked
34-
* as pending removal but not actually removed yet. During this period, the handle is functional, but the underlying session
35-
* won't be able to be grabbed by any SessionHolder. SessionHandle->IsActiveSession can be used to check if the session is
36-
* active.
37-
*/
38-
class SessionHandle
39-
{
40-
public:
41-
SessionHandle(Transport::Session & session) : mSession(session) {}
42-
~SessionHandle() {}
43-
44-
SessionHandle(const SessionHandle &) = delete;
45-
SessionHandle operator=(const SessionHandle &) = delete;
46-
SessionHandle(SessionHandle &&) = default;
47-
SessionHandle & operator=(SessionHandle &&) = delete;
48-
49-
bool operator==(const SessionHandle & that) const { return &mSession.Get() == &that.mSession.Get(); }
50-
51-
Transport::Session * operator->() const { return mSession.operator->(); }
52-
53-
private:
54-
friend class SessionHolder;
55-
ReferenceCountedHandle<Transport::Session> mSession;
56-
};
57-
58-
} // namespace chip
20+
#include <transport/Session.h>

src/transport/SessionHolder.h

+1-95
Original file line numberDiff line numberDiff line change
@@ -16,98 +16,4 @@
1616

1717
#pragma once
1818

19-
#include <lib/core/Optional.h>
20-
#include <lib/support/IntrusiveList.h>
21-
#include <transport/SessionDelegate.h>
22-
#include <transport/SessionHandle.h>
23-
24-
namespace chip {
25-
26-
/** @brief
27-
* Managed session reference. The object is used to store a session, the stored session will be automatically
28-
* released when the underlying session is released. One must verify it is available before use. The object can be
29-
* created using SessionHandle.Grab()
30-
*/
31-
class SessionHolder : public IntrusiveListNodeBase<>
32-
{
33-
public:
34-
SessionHolder() {}
35-
SessionHolder(const SessionHandle & handle) { Grab(handle); }
36-
virtual ~SessionHolder();
37-
38-
SessionHolder(const SessionHolder &);
39-
SessionHolder(SessionHolder && that);
40-
SessionHolder & operator=(const SessionHolder &);
41-
SessionHolder & operator=(SessionHolder && that);
42-
43-
virtual void SessionReleased() { Release(); }
44-
virtual void ShiftToSession(const SessionHandle & session)
45-
{
46-
Release();
47-
Grab(session);
48-
}
49-
50-
bool Contains(const SessionHandle & session) const
51-
{
52-
return mSession.HasValue() && &mSession.Value().Get() == &session.mSession.Get();
53-
}
54-
55-
bool GrabPairingSession(const SessionHandle & session); // Should be only used inside CASE/PASE pairing.
56-
bool Grab(const SessionHandle & session);
57-
void Release();
58-
59-
explicit operator bool() const { return mSession.HasValue(); }
60-
Optional<SessionHandle> Get() const
61-
{
62-
//
63-
// We cannot return mSession directly even if Optional<SessionHandle> is internally composed of the same bits,
64-
// since they are not actually equivalent type-wise, and SessionHandle does not permit copy-construction.
65-
//
66-
// So, construct a new Optional<SessionHandle> from the underlying Transport::Session reference.
67-
//
68-
return mSession.HasValue() ? chip::MakeOptional<SessionHandle>(mSession.Value().Get())
69-
: chip::Optional<SessionHandle>::Missing();
70-
}
71-
72-
Transport::Session * operator->() const { return &mSession.Value().Get(); }
73-
74-
// There is not delegate, nothing to do here
75-
virtual void DispatchSessionEvent(SessionDelegate::Event event) {}
76-
77-
protected:
78-
// Helper for use by the Grab methods.
79-
void GrabUnchecked(const SessionHandle & session);
80-
81-
Optional<ReferenceCountedHandle<Transport::Session>> mSession;
82-
};
83-
84-
/// @brief Extends SessionHolder to allow propagate SessionDelegate::* events to a given destination
85-
class SessionHolderWithDelegate : public SessionHolder
86-
{
87-
public:
88-
SessionHolderWithDelegate(SessionDelegate & delegate) : mDelegate(delegate) {}
89-
SessionHolderWithDelegate(const SessionHandle & handle, SessionDelegate & delegate) : SessionHolder(handle), mDelegate(delegate)
90-
{}
91-
operator bool() const { return SessionHolder::operator bool(); }
92-
93-
void SessionReleased() override
94-
{
95-
Release();
96-
97-
// Note, the session is already cleared during mDelegate.OnSessionReleased
98-
mDelegate.OnSessionReleased();
99-
}
100-
101-
void ShiftToSession(const SessionHandle & session) override
102-
{
103-
if (mDelegate.GetNewSessionHandlingPolicy() == SessionDelegate::NewSessionHandlingPolicy::kShiftToNewSession)
104-
SessionHolder::ShiftToSession(session);
105-
}
106-
107-
void DispatchSessionEvent(SessionDelegate::Event event) override { (mDelegate.*event)(); }
108-
109-
private:
110-
SessionDelegate & mDelegate;
111-
};
112-
113-
} // namespace chip
19+
#include <transport/Session.h>

0 commit comments

Comments
 (0)