1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/app/MessageDispacher.hpp" 5 #include "hmbdc/MetaUtils.hpp" 6 #include "hmbdc/Compile.hpp" 12 #include <type_traits> 14 namespace hmbdc {
namespace app {
16 namespace client_detail {
17 template <
size_t MAX_MEMORY_ATTACHMENT>
21 uint8_t underlyingMessage[MAX_MEMORY_ATTACHMENT];
22 template <MessageC Message>
24 static_assert(
sizeof(Message) <= MAX_MEMORY_ATTACHMENT,
"");
25 if (hmbdc_unlikely(att->attachment)) {
26 HMBDC_THROW(std::logic_error,
"previous InBandMemoryAttachment not concluded");
30 if constexpr(Message::justBytes) {
31 memcpy(underlyingMessage, &ibma.underlyingMessage,
sizeof(underlyingMessage));
33 memcpy(underlyingMessage, &ibma.underlyingMessage,
sizeof(Message));
36 if (att->holdShmHandle<Message>()) {
37 if constexpr (has_hmbdcShmRefCount<Message>::value) {
39 auto shmAddr = hmbdcShmHandleToAddr(att->
shmHandle);
41 att->attachment = shmAddr;
42 att->
clientData[0] = (uint64_t)&hmbdcShmDeallocator;
43 static_assert(
sizeof(ibma.underlyingMessage.hmbdcShmRefCount) ==
sizeof(
size_t));
44 att->
clientData[1] = (uint64_t)&ibma.underlyingMessage.hmbdcShmRefCount;
45 if constexpr (has_hmbdcShmRefCount<Message>::value) {
47 auto hmbdcShmRefCount = (
size_t*)h->clientData[1];
48 if (0 == __atomic_sub_fetch(hmbdcShmRefCount, 1, __ATOMIC_RELAXED)) {
49 auto& hmbdcShmDeallocator
50 = *(std::function<void (uint8_t*)>*)h->clientData[0];
51 hmbdcShmDeallocator((uint8_t*)h->attachment);
59 att->attachment = malloc(att->len);
60 att->afterConsumedCleanupFunc = hasMemoryAttachment::free;
67 if (accSize < att->len) {
68 auto attBytes = ibms.seg;
69 auto copySize = std::min(n, att->len - accSize);
70 memcpy((
char*)att->attachment + accSize, attBytes, copySize);
72 return accSize == att->len;
76 hasMemoryAttachment *
const att;
79 std::function<void* (boost::interprocess::managed_shared_memory::handle_t)>
81 std::function<void (uint8_t*)> hmbdcShmDeallocator;
121 template <
typename CcClient, MessageC ... Messages>
123 using Interests = std::tuple<Messages ...>;
126 INTERESTS_SIZE = std::tuple_size<Interests>::value,
128 typename hmbdc::filter_in_tuple_by_base<hasMemoryAttachment, Interests>::type
137 char const*
hmbdcName()
const {
return "hmbdc-anonymous"; }
149 return std::make_tuple<char const*, int>(
nullptr, 0);
151 return std::make_tuple<char const*, int>(
nullptr, 20);
195 std::cerr <<
"Client " <<
static_cast<CcClient*
>(
this)->
hmbdcName()
196 <<
" exited with reason: " << e.what() << std::endl;
248 void stopped(std::exception
const&e) noexcept {
254 bool dropped() noexcept {
262 template <
typename Iterator>
263 size_t handleRangeImpl(Iterator it,
264 Iterator end, uint16_t threadId) {
265 CcClient& c =
static_cast<CcClient&
>(*this);
267 for (;hmbdc_likely(!batchDone_ && it != end); ++it) {
268 if (MessageDispacher<CcClient, Interests>()(
269 c, *
static_cast<MessageHead*
>(*it))) res++;
274 typename std::conditional<MAX_MEMORY_ATTACHMENT != 0
275 , client_detail::InBandMemoryAttachmentProcessor<MAX_MEMORY_ATTACHMENT>
276 , std::nullptr_t>::type ibmaProc;
278 uint16_t hmbdcNumber = 0xffff;
281 bool batchDone_ =
false;
287 template <
typename CcClient,
typename MessageTuple>
289 template <
typename CcClient, MessageC ... Messages>
virtual void invokedCb(size_t dispatched)
this callback is called all the time (frequently) - the exact timing is after a batch of messages are...
Definition: Client.hpp:221
char const * hmbdcName() const
return the name of thread that runs this client, override if necessary
Definition: Client.hpp:137
virtual ~Client()
trivial
Definition: Client.hpp:228
boost::interprocess::managed_shared_memory ::handle_t shmHandle
Definition: Message.hpp:169
Definition: TypedString.hpp:84
std::tuple< char const *, int > schedSpec() const
an overrideable method. returns the schedule policy and priority, override if necessary priority is o...
Definition: Client.hpp:147
bool cache(InBandHasMemoryAttachment< Message > const &ibma, uint16_t typeTagIn)
Definition: Client.hpp:23
virtual bool droppedCb()
callback called after the Client is safely taken out of the Context
Definition: Client.hpp:205
void batchDone()
the derived user's Client has the option to stop the current batch of event dispatching.
Definition: Client.hpp:240
void stopped(std::exception const &e) noexcept
the following are for internal use, don't change or override
Definition: Client.hpp:248
size_t maxBatchMessageCount() const
an overridable method. client receives events in batches and the max batch size is controllable when ...
Definition: Client.hpp:163
Definition: Client.hpp:288
Definition: Message.hpp:459
virtual void stoppedCb(std::exception const &e)
callback called when this Client is taken out of message dispatching
Definition: Client.hpp:193
a trait class, if a Client can only run on a single specific thread in Pool, derive the Client from i...
Definition: Client.hpp:90
Definition: MetaUtils.hpp:100
Definition: Client.hpp:18
A Client represents a thread of execution/a task. The execution is managed by a Context. a Client object could participate in message dispatching as the receiver of specifed message types.
Definition: Client.hpp:122
virtual void messageDispatchingStartedCb(size_t const *pClientDispatchingStarted)
called before any messages got dispatched - only once
Definition: Client.hpp:179
a std tuple holding messages types it can dispatch
Definition: BlockingContext.hpp:206
if a specific hmbdc network transport (for example tcpcast, rmcast, and rnetmap) supports message wit...
Definition: Message.hpp:125
uint64_t clientData[2]
byte size of the above
Definition: Message.hpp:183
Definition: Message.hpp:430