Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _CXUTILS_MAPPED_MESSAGE_CLIENT__H
00043 #define _CXUTILS_MAPPED_MESSAGE_CLIENT__H
00044
00045 #include "cxutils/ipc/mappedmessagebox.h"
00046 #include "cxutils/ipc/messageserver.h"
00047 #include <set>
00048 #include <vector>
00049
00050 namespace CxUtils
00051 {
00060 class CX_UTILS_DLL MessageClient
00061 {
00062 public:
00063 typedef MappedMemory::ID ID;
00064 static const ID AnyID = 0;
00065
00073 class CX_UTILS_DLL Callback
00074 {
00075 public:
00082 class CX_UTILS_DLL Function
00083 {
00084 public:
00085 Function();
00086 Function(void (*callback)(Packet& packet,
00087 void* args),
00088 void* cbArgs = NULL);
00089 Function(const Function& data);
00090 ~Function();
00091 void Run(Packet& packet);
00092 Function& operator=(const Function& data);
00093 void (*mpCallback)(Packet& packet, void* args);
00094 void *mpCallbackArgs;
00095 };
00096 virtual void ProcessMessage(const Packet& packet) = 0;
00097 };
00098
00099 MessageClient();
00100 ~MessageClient();
00101 int Initialize(const std::string& serverID,
00102 const ID clientID = AnyID,
00103 const unsigned int messageBoxSize = MappedMessageBox::DefaultSize,
00104 const bool mustConnect = true);
00105 int Initialize(const ID serverID,
00106 const ID clientID,
00107 const unsigned int messageBoxSize = MappedMessageBox::DefaultSize,
00108 const bool mustConnect = true);
00109 int Initialize(const ID clientID,
00110 const unsigned int messageBoxSize = MappedMessageBox::DefaultSize);
00111 void Shutdown();
00112 bool IsInitialized() const;
00113 bool ReadMessage(Packet& message) const;
00114 bool ReadMessage(std::string& message) const;
00115 bool RegisterCallback(Callback* callback);
00116 bool RegisterCallback(const Callback::Function& callback);
00117 void RemoveCallback(Callback* callback);
00118 void RemoveCallback(const Callback::Function& callback);
00119 bool IsConnected(const unsigned int thresholdMs = 500) const { return mRegistry.IsActive(thresholdMs); }
00120 void SetUpdateDelayMs(const unsigned int delayTimeMs = 0);
00121 ID GetID() const { return mID; }
00122 private:
00123 static void ReceiveThread(void* messageClient);
00124 ID mID;
00125 volatile bool mConnectToServerFlag;
00126 unsigned int mDesiredBoxSize;
00127 volatile unsigned int mUpdateDelayMs;
00128 MappedMessageBox mMessageBox;
00129 MessageServer::Registry mRegistry;
00130 Thread mReceiveThread;
00131 Mutex mReceivedDataMutex;
00132 Mutex mDelayMutex;
00133 Mutex mCallbacksMutex;
00134 Packet::Queue mReceivedMessages;
00135 std::string mServerID;
00136 std::set<Callback*> mCallbacks;
00137 std::vector<Callback::Function> mFunctionCallbacks;
00138 Packet mTempPacket;
00139 };
00140 }
00141
00142 #endif
00143