#pragma once /**************************************************************************** Copyright (c) 2006, Radon Labs GmbH Copyright (c) 2011-2013,WebJet Business Division,CYOU http://www.genesis-3d.com.cn Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "messaging/message.h" #include "messaging/handler.h" #include "messaging/handlerthreadbase.h" //------------------------------------------------------------------------------ namespace Messaging { class AsyncPort : public Core::RefCounted { __DeclareClass(AsyncPort); public: /// constructor AsyncPort(); /// destructor virtual ~AsyncPort(); /// set pointer to handler thread object (must be derived from HandlerThreadBase) void SetHandlerThread(const GPtr& handlerThread); /// get pointer to handler thread object const GPtr& GetHandlerThread() const; /// attach a handler to the port, may be called before or after Open() virtual void AttachHandler(const GPtr& h); /// dynamically remove a handler from the port virtual void RemoveHandler(const GPtr& h); /// open the async port virtual void Open(); /// close the async port virtual void Close(); /// return true if port is open bool IsOpen() const; /// send an asynchronous message to the port template void Send(const GPtr& msg); /// send a message and wait for completion template void SendWait(const GPtr& msg); /// wait for a message to be handled template void Wait(const GPtr& msg); /// peek a message whether it has been handled template bool Peek(const GPtr& msg); /// cancel a pending message template void Cancel(const GPtr& msg); private: /// send an asynchronous message to the port void SendInternal(const GPtr& msg); /// send a message and wait for completion void SendWaitInternal(const GPtr& msg); /// wait for a message to be handled void WaitInternal(const GPtr& msg); /// peek a message whether it has been handled bool PeekInternal(const GPtr& msg); /// cancel a pending message void CancelInternal(const GPtr& msg); private: /// clear all attached message handlers void ClearHandlers(); GPtr thread; bool isOpen; }; //------------------------------------------------------------------------------ /** */ inline void AsyncPort::SetHandlerThread(const GPtr& handlerThread) { n_assert(!this->IsOpen()); this->thread = handlerThread; } //------------------------------------------------------------------------------ /** */ inline const GPtr& AsyncPort::GetHandlerThread() const { return this->thread; } //------------------------------------------------------------------------------ /** */ inline bool AsyncPort::IsOpen() const { return this->isOpen; } //------------------------------------------------------------------------------ /** */ template inline void AsyncPort::Send(const GPtr& msg) { this->SendInternal((const GPtr&)msg); } //------------------------------------------------------------------------------ /** */ template inline void AsyncPort::SendWait(const GPtr& msg) { this->SendWaitInternal((const GPtr&)msg); } //------------------------------------------------------------------------------ /** */ template inline void AsyncPort::Wait(const GPtr& msg) { this->WaitInternal((const GPtr&)msg); } //------------------------------------------------------------------------------ /** */ template inline bool AsyncPort::Peek(const GPtr& msg) { return this->PeekInternal((const GPtr&)msg); } //------------------------------------------------------------------------------ /** */ template inline void AsyncPort::Cancel(const GPtr& msg) { this->CancelInternal((const GPtr&)msg); } } // namespace Messaging //------------------------------------------------------------------------------