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 #ifndef _CXUTILS_IPC_MESSAGE_SERVER__H
00042 #define _CXUTILS_IPC_MESSAGE_SERVER__H
00043
00044 #include "cxutils/ipc/mappedmessagebox.h"
00045 #include "cxutils/thread.h"
00046 #include <set>
00047 #include <map>
00048
00049 namespace CxUtils
00050 {
00062 class CX_UTILS_DLL MessageServer
00063 {
00064 public:
00065 static const unsigned int DefaultMaxClients = 25;
00066 typedef MappedMemory::ID ID;
00067 typedef std::set<ID> List;
00068
00069
00070
00071
00072
00073
00074
00075
00076 class CX_UTILS_DLL Registry
00077 {
00078 public:
00079 friend class MessageServer;
00080 static const unsigned int MaxClients = 25;
00081 static const unsigned int HeaderSize = 20;
00082 Registry();
00083 ~Registry();
00084 int OpenRegistry(const ID registryID);
00085 int OpenRegistry(const std::string& registryID);
00086 int CreateRegistry(const ID registryID,
00087 const unsigned int maxClients = MaxClients);
00088 int CreateRegistry(const std::string& registryID,
00089 const unsigned int maxClients = MaxClients);
00090 int CloseRegistry();
00091 int Register(const ID id);
00092 int Unregister(const ID id);
00093 int GetRegistry(List& registry);
00094 bool IsRegistered(const ID id) const;
00095 bool IsOpen() const { return mRegistry.IsOpen(); }
00096 bool IsActive(const unsigned int thresholdMs = 500) const;
00097 private:
00098 static const unsigned int TimePos = 8;
00099 static const unsigned int CountPos = 16;
00100 bool Touch(const Time::Stamp timestampMs);
00101 MappedMemory mRegistry;
00102 bool mServerFlag;
00103 };
00104 MessageServer();
00105 ~MessageServer();
00106 int Initialize(const ID serverID,
00107 const unsigned int maxClients = DefaultMaxClients);
00108 int Initialize(const std::string& serverID,
00109 const unsigned int maxClients = DefaultMaxClients);
00110 void Shutdown();
00111 bool CreateConnection(const ID id);
00112 bool SendToClient(const ID id, const std::string& message);
00113 bool SendToClient(const ID id, const Packet& message);
00114 bool SendToAllClients(const std::string& message);
00115 bool SendToAllClients(const Packet& message);
00116 bool GetClientList(List& clients) const;
00117 unsigned int GetNumClients() const;
00118 bool IsInitialized() const;
00119 static bool IsServerPresent(const std::string& serverID);
00120 private:
00121 typedef std::map<ID, MappedMessageBox*> Clients;
00122 static void ClientDiscoveryThread(void* messageServer);
00123 Mutex mClientsMutex;
00124 Thread mClientDiscoveryThread;
00125 Clients mClients;
00126 Registry mClientRegistry;
00127 };
00128 };
00129
00130 #endif
00131