-
Notifications
You must be signed in to change notification settings - Fork 1
/
receive.hpp
69 lines (54 loc) · 1.74 KB
/
receive.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <chrono>
#include <thread>
#include <memory>
#include <utility>
#include "flags.hpp"
#include "detail/handler.hpp"
#include "detail/handler_registry.hpp"
#include "detail/message.hpp"
#include "detail/message_queue.hpp"
namespace sam
{
template <typename ...Callables_t>
void receive(Callables_t &&...callables);
template <typename Rep_t, typename Period_t, typename ...Callables_t>
void receive_for(const std::chrono::duration<Rep_t, Period_t> &timeout, Callables_t &&...callables);
}
namespace sam
{
template <typename ...Callables_t>
void receive(Callables_t &&...callables)
{
detail::handlers_t handlers {detail::register_handlers(std::forward<Callables_t>(callables)...)};
detail::msgqueue_t &message_queue {detail::message_queue_for_thread(std::this_thread::get_id())};
for (;;)
{
std::shared_ptr<detail::message> message_ptr {message_queue.wait_and_pop()};
ctlcode_t control_code {detail::dispatch_message(handlers, message_ptr)};
if (control_code == STOP)
{
break;
}
}
}
template <typename Rep_t, typename Period_t, typename ...Callables_t>
void receive_for(const std::chrono::duration<Rep_t, Period_t> &timeout, Callables_t &&...callables)
{
detail::handlers_t handlers {detail::register_handlers_with_timeout(std::forward<Callables_t>(callables)...)};
detail::msgqueue_t &message_queue {detail::message_queue_for_thread(std::this_thread::get_id())};
for (;;)
{
std::shared_ptr<detail::message> message_ptr {message_queue.wait_for_and_pop(timeout)};
if (message_ptr == nullptr)
{
message_ptr = detail::make_message(timeout_error());
}
ctlcode_t control_code {detail::dispatch_message(handlers, message_ptr)};
if (control_code == STOP)
{
break;
}
}
}
}