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
00043
00044
00045
00046
00047
00048
00049 #ifndef __CXUTILS_UDP_SHARED_SERVER__H
00050 #define __CXUTILS_UDP_SHARED_SERVER__H
00051
00052 #include "cxutils/networking/udpserver.h"
00053 #include "cxutils/ipc/messageserver.h"
00054 #include "cxutils/ipc/messageclient.h"
00055 #include "cxutils/thread.h"
00056 #include "cxutils/circulararray.h"
00057
00058 namespace CxUtils
00059 {
00083 class CX_UTILS_DLL UdpSharedServer
00084 {
00085 public:
00092 class CX_UTILS_DLL Info
00093 {
00094 public:
00095 static const unsigned int Size = 18;
00096 Info();
00097 Info(const Info& info);
00098 ~Info();
00099 Info& operator=(const Info& info);
00100 int mNetworkInterface;
00101 IP4Address mMulticastGroup;
00102 unsigned short mPort;
00103 };
00111 class CX_UTILS_DLL Callback
00112 {
00113 public:
00120 class CX_UTILS_DLL Data
00121 {
00122 public:
00123 Data();
00124 Data(void (*callback)(const Packet& packet,
00125 const IP4Address& ipAddress,
00126 void* args),
00127 void* cbArgs = NULL);
00128 Data(const Data& data);
00129 ~Data();
00130 void Run(const Packet& packet, const IP4Address& ipAddress);
00131 Data& operator=(const Data& data);
00132 void (*mpCallback)(const Packet& packet, const IP4Address& ipAddress, void* args);
00133 void *mpCallbackArgs;
00134 };
00135 virtual void ProcessUDP(const Packet& packet,
00136 const IP4Address& ipAddress) {};
00137 virtual void ProcessUDP(const Packet& packet,
00138 const IP4Address& ipAddress,
00139 const unsigned short sourcePort) {}
00140 };
00141 UdpSharedServer();
00142 ~UdpSharedServer();
00143 int InitializeSocket(const unsigned short port,
00144 const IP4Address& multicastGroup = "224.0.0.1",
00145 const int netInterface = -1);
00146 void Shutdown();
00147 int GetNetworkInterface() const;
00148 bool RegisterCallback(Callback* callback);
00149 bool RegisterCallback(const Callback::Data& callback);
00150 void RemoveCallback(Callback* callback);
00151 void RemoveCallback(const Callback::Data& callback);
00152 bool IsValid() const;
00153 bool IsPrimary() const;
00154 bool IsConnected() const { return mConnectedFlag; }
00155 bool Recv(Packet& packet, IP4Address& ipAddress);
00156 void SetUpdateDelayMs(const unsigned int delayTimeMs = 0);
00157 private:
00158 static void UdpStatusThread(void* udpSharedServer);
00159 static void UdpSocketThread(void* udpSharedServer);
00160 static void ProcessUdpMessage(Packet& udpMessage, void* udpSharedServer);
00161 volatile bool mPrimaryFlag;
00162 volatile bool mConnectedFlag;
00163 volatile unsigned int mUpdateDelayMs;
00164 Thread mUdpStatusThread;
00165 Thread mUdpSocketThread;
00166 UdpServer* mpSocket;
00167 MessageServer* mpMessageServer;
00168 MessageClient* mpMessageClient;
00169 Info mSocketInfo;
00170 Mutex mCallbacksMutex;
00171 Mutex mDelayMutex;
00172 std::set<Callback*> mCallbacks;
00173 std::vector<Callback::Data> mFunctionCallbacks;
00174 volatile bool mQuitServerFlag;
00175 Mutex mQueueMutex;
00176 CircularArray<IP4Address> mMessageAddressQueue;
00177 CircularArray<Packet> mPacketQueue;
00178 };
00179 }
00180
00181
00182 #endif
00183