CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
actor.h
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2025 Luan Young
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
88#pragma once
89
90#include <chrono>
91#include <functional>
92#include <memory>
93#include <mutex>
94#include <zmq.hpp>
95
96#include "cppzmqzoltanext/czze_export.h"
97
98namespace zmqzext {
99
121using actor_fn_t = std::function<bool(zmq::socket_t&)>;
122
131class CZZE_EXPORT actor_t {
132public:
143 explicit actor_t(zmq::context_t& context);
144
153 actor_t(actor_t const&) = delete;
154
163 actor_t& operator=(actor_t const&) = delete;
164
181 actor_t(actor_t&& other) noexcept;
182
200 actor_t& operator=(actor_t&& other) noexcept;
201
207 ~actor_t() noexcept;
208
223 void start(actor_fn_t func);
224
238 bool stop(std::chrono::milliseconds timeout = std::chrono::milliseconds{-1});
239
245 zmq::socket_t& socket() noexcept { return _parent_socket; }
246
252 bool is_started() const noexcept { return _started; }
253
259 bool is_stopped() const noexcept { return _stopped; }
260
265 void set_destructor_timeout(std::chrono::milliseconds timeout) noexcept { _timeout_on_destructor = timeout; }
266
271 std::chrono::milliseconds get_destructor_timeout() const noexcept { return _timeout_on_destructor; }
272
273private:
277 static constexpr std::chrono::milliseconds DEFAULT_DESTRUCTOR_TIMEOUT{100};
278
283 struct SharedExceptionState {
284 std::mutex exception_mutex;
285 std::exception_ptr saved_exception;
286 };
287
300 static void execute(actor_fn_t func, std::unique_ptr<zmq::socket_t> socket,
301 std::shared_ptr<SharedExceptionState> exception_state) noexcept;
302
312 std::string bind_to_unique_address();
313
314 zmq::socket_t _parent_socket;
315 std::unique_ptr<zmq::socket_t> _child_socket;
316 std::shared_ptr<SharedExceptionState> _exception_state;
317 bool _started;
318 bool _stopped;
319 std::chrono::milliseconds _timeout_on_destructor{DEFAULT_DESTRUCTOR_TIMEOUT};
320};
321
322} // namespace zmqzext
std::function< bool(zmq::socket_t &)> actor_fn_t
Alias for a function type used to define actor behaviors.
Definition: actor.h:121
Class that implements the Actor pattern using ZMQ PAIR sockets.
Definition: actor.h:131
bool is_stopped() const noexcept
Checks if the actor thread was stopped.
Definition: actor.h:259
bool is_started() const noexcept
Checks if the actor thread was started.
Definition: actor.h:252
actor_t(actor_t const &)=delete
Copy constructor is deleted.
zmq::socket_t & socket() noexcept
Gets the parent socket for external communication.
Definition: actor.h:245
actor_t & operator=(actor_t const &)=delete
Copy assignment operator is deleted.
std::chrono::milliseconds get_destructor_timeout() const noexcept
Gets the current timeout value used in the destructor.
Definition: actor.h:271
void set_destructor_timeout(std::chrono::milliseconds timeout) noexcept
Sets the timeout value used in the destructor.
Definition: actor.h:265