![]() |
VR-Link API Documentation for HLA 1.3
|
00001 /******************************************************************************* 00002 ** Copyright (c) 2005 MaK Technologies, Inc. 00003 ** All rights reserved. 00004 *******************************************************************************/ 00005 /******************************************************************************* 00006 ** $RCSfile: vlAsyncSocket.h,v $ $Revision: 1.2.6.1 $ $State: Exp $ 00007 *******************************************************************************/ 00008 #ifndef vlAsyncSocket_H_ 00009 #define vlAsyncSocket_H_ 00010 00014 00015 #ifndef VXWORKS 00016 00017 #include "vlNetSocket.h" 00018 #include <vlutil/vlMutex.h> 00019 #include <vlutil/vlThreadSafe.h> 00020 #include <vlutil/vlThreadedObject.h> 00021 #include <vlutil/vlRunnable.h> 00022 #include <deque> 00023 00024 00025 00026 #ifdef DtUSE_UTILITIES_NAMESPACE 00027 namespace DtUSE_UTILITIES_NAMESPACE 00028 { 00029 #endif 00030 00036 class DT_DLL_NETCOMPAT DtAsyncSocketPacket 00037 { 00038 public: 00039 DtAsyncSocketPacket(); 00040 DtAsyncSocketPacket(int size, const char *data, DtIpv4Addr addr=0); 00041 DtAsyncSocketPacket(const DtAsyncSocketPacket &other); 00042 virtual ~DtAsyncSocketPacket(); 00043 00044 00045 DtAsyncSocketPacket &operator=(const DtAsyncSocketPacket &other); 00046 00047 size_t size() const { return mySize;} 00048 char * data() { return &myData[0];} 00049 DtIpv4Addr addr() const { return myAddr;} 00050 00051 protected: 00052 00053 int mySize; 00054 std::vector<char> myData; 00055 DtIpv4Addr myAddr; 00056 00057 }; 00058 00065 class DT_DLL_NETCOMPAT DtAsyncNetSocket : public DtNetSocket 00066 { 00067 public: 00068 00069 DtAsyncNetSocket(int port = DtDEFAULT_PORT, 00070 DtU8 flags = DtReadWrite, DtIpv4Addr devAddr = 0); 00071 00072 DtAsyncNetSocket(int port, 00073 DtIpv4Addr destAddr, DtU8 flags, 00074 DtIpv4Addr devAddr = 0); 00075 00076 DtAsyncNetSocket(char* dummy, int sockfd, DtU8 flags = DtReadWrite); 00077 00079 virtual ~DtAsyncNetSocket(); 00080 00081 private: 00082 00084 DtAsyncNetSocket(const DtAsyncNetSocket &); 00085 00087 DtAsyncNetSocket & operator=(const DtAsyncNetSocket &); 00088 00089 public: 00090 00096 virtual int sendTo(caddr_t packet, size_t size, DtIpv4Addr addr); 00097 00100 virtual int directSendTo(caddr_t packet, size_t size, DtIpv4Addr addr); 00101 00103 virtual bool usingAsyncSend() const; 00104 virtual void setUsingAsyncSend(bool b); 00105 00107 virtual bool usingAsyncReceive() const; 00108 virtual void setUsingAsyncReceive(bool b); 00109 00110 protected: 00111 00114 virtual int recvFromNet(caddr_t *buffptr); 00115 00117 void initThread(); 00118 00120 virtual void workerSendThread(); 00121 00123 virtual void workerReceiveThread(); 00124 00127 virtual void processSendQueue(); 00128 00130 virtual void processReceiveQueue(); 00131 00132 protected: 00135 #ifdef _WIN32 00136 static unsigned long runReceive(void* usr_data); 00137 static unsigned long runSend(void *user_data); 00138 #else 00139 static void* runReceive(void* usr_data); 00140 static void* runSend(void* usr_data); 00141 #endif 00142 static void setQueueTimeout(double new_time_out); 00143 static double queueTimeout(); 00144 00145 protected: 00146 00148 static double theQueueTimeout; 00149 00150 00151 protected: 00152 00153 typedef DtThreadSafe<std::deque<DtAsyncSocketPacket> > ThreadSafePacketQueue; 00154 00155 bool myUseAsyncSendFlag; 00156 bool myUseAsyncReceiveFlag; 00157 00159 ThreadSafePacketQueue mySendQueue; 00160 00162 ThreadSafePacketQueue myReceiveQueue; 00163 00164 volatile bool myTimeToQuitSender; 00165 volatile bool myTimeToQuitReceiver; 00166 char *myThreadPktBuff; 00167 00168 #ifdef _WIN32 00169 HANDLE mySendThreadHandle; 00170 HANDLE myReceiveThreadHandle; 00171 HANDLE mySendEvent; 00172 HANDLE myReceiveEvent; 00173 #else 00174 pthread_t mySendThreadHandle; 00175 pthread_t myReceiveThreadHandle; 00176 int mySendEvent[2]; 00177 #endif 00178 00179 }; 00180 00181 inline void 00182 DtAsyncNetSocket::setQueueTimeout(double time_out) 00183 { 00184 theQueueTimeout = time_out; 00185 } 00186 00187 inline double 00188 DtAsyncNetSocket::queueTimeout() 00189 { 00190 return theQueueTimeout; 00191 } 00192 inline bool 00193 DtAsyncNetSocket::usingAsyncSend() const 00194 { 00195 return myUseAsyncSendFlag; 00196 } 00197 00198 inline void 00199 DtAsyncNetSocket::setUsingAsyncSend(bool b) 00200 { 00201 myUseAsyncSendFlag = b; 00202 } 00203 inline bool 00204 DtAsyncNetSocket::usingAsyncReceive() const 00205 { 00206 return myUseAsyncReceiveFlag; 00207 } 00208 00209 inline void 00210 DtAsyncNetSocket::setUsingAsyncReceive(bool b) 00211 { 00212 myUseAsyncReceiveFlag = b; 00213 } 00214 00215 #ifdef DtUSE_UTILITIES_NAMESPACE 00216 } 00217 #endif 00218 00219 #endif //! !VXWORKS 00220 #endif //! DtAsyncNetSocket_H_ 00221