IRSOL
C++ code implementing socket server for interacting with Baumer camera.
state.hpp
Go to the documentation of this file.
1
21#pragma once
22
23#include "irsol/logging.hpp"
24#include "irsol/types.hpp"
25
26#include <atomic>
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <thread>
31
32namespace irsol {
33namespace server {
34namespace internal {
35
44{
46 uint64_t inputSequenceLength{16};
47
50
52 double frameRate{4.0};
53};
54
68{
71
76 bool running() const;
77
100 template<typename Callable>
101 void start(Callable&& task, const std::string& description)
102 {
103 std::scoped_lock<std::mutex> lock(m_threadMutex);
104 if(m_running.load()) {
105 throw std::logic_error("Thread already running");
106 }
107
108 IRSOL_LOG_INFO("Locking the running of the thread");
109 m_stopRequested = std::make_shared<std::atomic<bool>>(false);
110 m_running.store(true);
111
112 std::thread([this,
113 stopRequested = m_stopRequested,
114 task = std::forward<Callable>(task),
115 description]() mutable {
116 try {
117 IRSOL_LOG_INFO("Inside thread");
118 task(stopRequested);
119 IRSOL_LOG_INFO("Inside thread, after task execution");
120 } catch(...) {
121 IRSOL_LOG_ERROR("Background thread for task '{}' failed running.", description);
122 }
123 m_running.store(false);
124 })
125 .detach();
126
127 IRSOL_LOG_INFO("Thread started");
128 }
129
136 void stop();
137
138private:
140 std::atomic<bool> m_running{false};
141
143 std::mutex m_threadMutex;
144
146 std::shared_ptr<std::atomic<bool>> m_stopRequested;
147};
148
166
167} // namespace internal
168} // namespace server
169} // namespace irsol
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
Logging utilities and configuration for the irsol library.
Controls how a client listens for frames, including thread management and GIS parameters.
Definition state.hpp:68
GisParams gisParams
Parameters for image streaming (GIS command).
Definition state.hpp:70
void stop()
Requests the active thread (if any) to stop by setting the stop flag.
Definition state.cpp:14
void start(Callable &&task, const std::string &description)
Starts the passed lambda function in a background thread.
Definition state.hpp:101
std::shared_ptr< std::atomic< bool > > m_stopRequested
Shared stop token used to cancel the thread's task loop.
Definition state.hpp:146
std::atomic< bool > m_running
Internal flag indicating whether the thread is running.
Definition state.hpp:140
std::mutex m_threadMutex
Synchronization primitive to guard start/stop operations.
Definition state.hpp:143
bool running() const
Returns whether a frame listening thread is currently active.
Definition state.cpp:8
Per-client configuration parameters for the GIS (GetImageSequence) command.
Definition state.hpp:44
uint64_t inputSequenceLength
Number of frames the client expects in a sequence.
Definition state.hpp:46
double frameRate
Desired frame rate for this client's stream.
Definition state.hpp:52
uint64_t inputSequenceNumber
Current frame index in the sequence (automatically incremented).
Definition state.hpp:49
Encapsulates all per-client data used during a session's lifetime.
Definition state.hpp:162
FrameListeningState frameListeningState
Client-specific state for image frame listening, including parameters and worker thread.
Definition state.hpp:164
auto description
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...