MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ request()

bool MayaFlux::Core::SerialDevice::request ( std::span< const uint8_t > command,
ReplyMatcher matcher,
std::chrono::milliseconds timeout,
ReplyCallback done )

Send a command and wait for its reply.

Parameters
commandSent as it is.
matcherTrue for the message that answers the command.
timeoutHow long to wait.
doneCalled exactly once for a request that returns true, on the link's thread, with the reply when there is one. Never called for a request that returns false.
Returns
false when a request is already pending, the port is not open or the send failed. A request whose reply or timeout beat a failed send is already complete, its callback has run, and this returns true.

Definition at line 120 of file SerialDevice.cpp.

122{
123 if (!matcher || !done || m_link->state() != EndpointState::OPEN) {
124 return false;
125 }
126
127 auto request = std::make_shared<Pending>();
128 request->matcher = std::move(matcher);
129 request->done = std::move(done);
130
131 const bool installed = m_pending.update([&](std::shared_ptr<Pending>& slot) {
132 if (slot) {
133 return false;
134 }
135 slot = request;
136 return true;
137 });
138 if (!installed) {
139 return false;
140 }
141
142 request->timer.store(m_link->post_after(timeout, [this, request] {
143 if (const auto expired = take_pending(request)) {
144 end_request(expired, SerialReply::TIMED_OUT, {});
145 }
146 }));
147
148 if (!is_pending(request)) {
149 if (const auto token = request->timer.exchange(0); token != 0) {
150 m_link->cancel_post(token);
151 }
152 }
153
154 if (!m_link->write(command)) {
155 if (const auto failed = take_pending(request)) {
156 if (const auto token = failed->timer.exchange(0); token != 0) {
157 m_link->cancel_post(token);
158 }
159 return false;
160 }
161 return true;
162 }
163
164 return true;
165}
Memory::SnapshotCell< std::shared_ptr< Pending > > m_pending
std::shared_ptr< Pending > take_pending(const std::shared_ptr< Pending > &only={})
std::unique_ptr< ISerialLink > m_link
bool is_pending(const std::shared_ptr< Pending > &request) const
bool request(std::span< const uint8_t > command, ReplyMatcher matcher, std::chrono::milliseconds timeout, ReplyCallback done)
Send a command and wait for its reply.

References m_link, m_pending, MayaFlux::Core::OPEN, and request().

Referenced by complete_request(), end_request(), is_pending(), operator=(), and request().

Here is the call graph for this function:
Here is the caller graph for this function: