Nebula
Loading...
Searching...
No Matches
asyncport.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
14#include "messaging/message.h"
15#include "messaging/handler.h"
17
18//------------------------------------------------------------------------------
19namespace Messaging
20{
22{
24public:
26 AsyncPort();
28 virtual ~AsyncPort();
29
31 void SetHandlerThread(const Ptr<HandlerThreadBase>& handlerThread);
34
36 virtual void AttachHandler(const Ptr<Handler>& h);
38 virtual void RemoveHandler(const Ptr<Handler>& h);
39
41 virtual void Open();
43 virtual void Close();
45 bool IsOpen() const;
46
48 template<class MESSAGETYPE> void Send(const Ptr<MESSAGETYPE>& msg);
50 template<class MESSAGETYPE> void SendWait(const Ptr<MESSAGETYPE>& msg);
52 template<class MESSAGETYPE> void Wait(const Ptr<MESSAGETYPE>& msg);
54 template<class MESSAGETYPE> bool Peek(const Ptr<MESSAGETYPE>& msg);
56 template<class MESSAGETYPE> void Cancel(const Ptr<MESSAGETYPE>& msg);
57
58private:
60 void SendInternal(const Ptr<Message>& msg);
62 void SendWaitInternal(const Ptr<Message>& msg);
64 void WaitInternal(const Ptr<Message>& msg);
66 bool PeekInternal(const Ptr<Message>& msg);
68 void CancelInternal(const Ptr<Message>& msg);
69
70private:
73
75 bool isOpen;
76};
77
78//------------------------------------------------------------------------------
81inline void
83{
84 n_assert(!this->IsOpen());
85 this->thread = handlerThread;
86}
87
88//------------------------------------------------------------------------------
91inline const Ptr<HandlerThreadBase>&
93{
94 return this->thread;
95}
96
97//------------------------------------------------------------------------------
100inline bool
102{
103 return this->isOpen;
104}
105
106//------------------------------------------------------------------------------
109template<class MESSAGETYPE> inline void
111{
112 this->SendInternal((const Ptr<Messaging::Message>&)msg);
113}
114
115//------------------------------------------------------------------------------
118template<class MESSAGETYPE> inline void
123
124//------------------------------------------------------------------------------
127template<class MESSAGETYPE> inline void
129{
130 this->WaitInternal((const Ptr<Messaging::Message>&)msg);
131}
132
133//------------------------------------------------------------------------------
136template<class MESSAGETYPE> inline bool
138{
139 return this->PeekInternal((const Ptr<Messaging::Message>&)msg);
140}
141
142//------------------------------------------------------------------------------
145template<class MESSAGETYPE> inline void
147{
148 this->CancelInternal((const Ptr<Messaging::Message>&)msg);
149}
150
151} // namespace Messaging
152//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
The AsyncPort class runs its handlers in a separate thread, so that message processing happens in a s...
Definition asyncport.h:22
void Send(const Ptr< MESSAGETYPE > &msg)
send an asynchronous message to the port
Definition asyncport.h:110
void SendWaitInternal(const Ptr< Message > &msg)
send a message and wait for completion
Definition asyncport.cc:112
bool Peek(const Ptr< MESSAGETYPE > &msg)
peek a message whether it has been handled
Definition asyncport.h:137
bool isOpen
Definition asyncport.h:75
void ClearHandlers()
clear all attached message handlers
bool IsOpen() const
return true if port is open
Definition asyncport.h:101
virtual void Open()
open the async port
Definition asyncport.cc:43
__DeclareClass(AsyncPort)
void CancelInternal(const Ptr< Message > &msg)
cancel a pending message
Definition asyncport.cc:138
void Cancel(const Ptr< MESSAGETYPE > &msg)
cancel a pending message
Definition asyncport.h:146
Ptr< HandlerThreadBase > thread
Definition asyncport.h:74
virtual void RemoveHandler(const Ptr< Handler > &h)
dynamically remove a handler from the port
Definition asyncport.cc:85
void Wait(const Ptr< MESSAGETYPE > &msg)
wait for a message to be handled
Definition asyncport.h:128
void SetHandlerThread(const Ptr< HandlerThreadBase > &handlerThread)
set pointer to handler thread object (must be derived from HandlerThreadBase)
Definition asyncport.h:82
void SendWait(const Ptr< MESSAGETYPE > &msg)
send a message and wait for completion
Definition asyncport.h:119
virtual ~AsyncPort()
destructor
Definition asyncport.cc:28
const Ptr< HandlerThreadBase > & GetHandlerThread() const
get pointer to handler thread object
Definition asyncport.h:92
virtual void AttachHandler(const Ptr< Handler > &h)
attach a handler to the port, may be called before or after Open()
Definition asyncport.cc:75
AsyncPort()
constructor
Definition asyncport.cc:19
virtual void Close()
close the async port
Definition asyncport.cc:60
void WaitInternal(const Ptr< Message > &msg)
wait for a message to be handled
Definition asyncport.cc:152
bool PeekInternal(const Ptr< Message > &msg)
peek a message whether it has been handled
Definition asyncport.cc:127
void SendInternal(const Ptr< Message > &msg)
send an asynchronous message to the port
Definition asyncport.cc:98
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
#define n_assert(exp)
Definition debug.h:50
Definition asyncport.cc:10