PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Sources/Kernel/zport.h

http://mmo-resourse.googlecode.com/
C++ Header | 480 lines | 318 code | 110 blank | 52 comment | 33 complexity | 5c21f6f035c4720952e6f2906e63196c MD5 | raw file
Possible License(s): Zlib, LGPL-2.1, LGPL-2.0
  1. #ifndef ZPORT_H
  2. #define ZPORT_H
  3. //Windows?????----------------------------------------------------------------------------------
  4. #ifdef WIN32
  5. #include <windows.h>
  6. #else
  7. #include <pthread.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <arpa/inet.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #endif
  15. #include <stdio.h>
  16. #include <string.h>
  17. #ifndef WIN32
  18. #define WINAPI
  19. #define BYTE unsigned char
  20. #define DWORD unsigned long
  21. #define LPVOID void *
  22. #ifdef _DEBUG
  23. //{
  24. void _trace(char *fmt, ...);
  25. #ifndef ASSERT
  26. #define ASSERT(x) { if ( !( x ) ) _asm{ int 0x03 } }
  27. #endif
  28. #ifndef VERIFY
  29. #define VERIFY(x) { if ( !( x ) ) _asm{ int 0x03 } }
  30. #endif
  31. //}
  32. #else
  33. //{
  34. #ifndef ASSERT
  35. #define ASSERT(x)
  36. #endif
  37. #ifndef VERIFY
  38. #define VERIFY(x) x
  39. #endif
  40. //}
  41. #endif
  42. #ifdef _DEBUG
  43. //{
  44. #ifndef TRACE
  45. #define TRACE _trace
  46. #endif
  47. //}
  48. #else
  49. //{
  50. inline void _trace(const char* fmt, ...) { }
  51. #ifndef TRACE
  52. #define TRACE 1 ? (void)0 : _trace
  53. #endif
  54. //}
  55. #endif
  56. #define INIT_PTR(x) do{ (x) = NULL; }while(0);
  57. #define SAFE_DELETE(x) try{ if( (x) != NULL ) { delete (x); (x) = NULL; } } catch(...) { TRACE("SAFE_DELETE error\n"); }
  58. #define SAFE_DELETE_ARRAY(x) try{ if( (x) != NULL ) { delete[] (x); (x) = NULL; } } catch(...) { TRACE("SAFE_DELETE_ARRAY error\n"); }
  59. #ifndef SAFE_FREE
  60. #undef SAFE_FREE
  61. #define SAFE_FREE(x) try{ if( (x) != NULL ) { free(x); (x)=NULL; } } catch(...) { TRACE("SAFE_FREE error\n"); }
  62. #endif
  63. #ifndef SAFE_RELEASE
  64. #undef SAFE_RELEASE
  65. #define SAFE_RELEASE(x) try{ if( (x) != NULL ) { (x)->Release(); (x) = NULL; } } catch(...) { TRACE("SAFE_RELEASE error\n"); }
  66. #endif
  67. #define SAFE_CLOSEHANDLE(x) try{ if (x) { CloseHandle(x); (x) = NULL; } } catch(...) { TRACE("SAFE_CLOSEHANDLE error\n"); }
  68. #define SAFE_FREELIB(x) try{ if (x) { FreeLibrary(x); (x) = NULL; } } catch(...) { TRACE("SAFE_FREELIB error\n"); }
  69. /*
  70. * Quick and safely convert number format
  71. */
  72. #ifndef MAKEFOURCC
  73. //{
  74. #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
  75. ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
  76. ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
  77. //}
  78. #endif
  79. #define COUNT_OF_ARRAY(array) ( sizeof( array ) / sizeof( array[0] ) )
  80. /*
  81. * 'c' must be a char and not be a expression in the _private macro
  82. */
  83. #define _private_IS_NUM(c) ((c) >= '0' && (c) <= '9')
  84. #define _private_IS_SPACE(c) ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == '\t' || (c) == 'x')
  85. #define IS_NUM(c) _private_IS_NUM(c)
  86. #define IS_SPACE(c) _private_IS_SPACE(c)
  87. inline void strlwr(char* sz)
  88. {
  89. char c;
  90. for (int i = 0; 0 != (c = sz[i]); i++)
  91. {
  92. if (c >= 'A' && c <= 'Z')
  93. sz[i] = c + ('a' - 'A');
  94. }
  95. }
  96. #define SOCKET int
  97. #define closesocket close
  98. #define INVALID_SOCKET -1
  99. #endif
  100. //???????
  101. class ZMutex {
  102. #ifdef WIN32
  103. CRITICAL_SECTION mutex;
  104. #else
  105. public:
  106. pthread_mutex_t mutex;
  107. //static int all_count;
  108. int count;
  109. #endif
  110. public:
  111. ZMutex() {
  112. #ifdef WIN32
  113. InitializeCriticalSection(&mutex);
  114. #else
  115. pthread_mutexattr_t attr;
  116. pthread_mutexattr_init(&attr);
  117. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  118. int rc = pthread_mutex_init(&mutex, &attr);
  119. //printf("ZMutex Created..\n");
  120. count = 0;
  121. #endif
  122. }
  123. ~ZMutex() {
  124. #ifdef WIN32
  125. DeleteCriticalSection(&mutex);
  126. #else
  127. int rc = pthread_mutex_destroy(&mutex);
  128. //printf("ZMutex released..\n");
  129. #endif
  130. }
  131. bool trylock()
  132. {
  133. #ifdef WIN32
  134. return false;
  135. #else
  136. int rc = pthread_mutex_trylock(&mutex);
  137. /* if (rc != 0)
  138. {
  139. printf("trylock mutex error %d\n", rc);
  140. }*/
  141. return (0 == rc);
  142. #endif
  143. }
  144. void lock() {
  145. #ifdef WIN32
  146. EnterCriticalSection(&mutex);
  147. #else
  148. int rc = pthread_mutex_lock(&mutex);
  149. if (rc != 0)
  150. {
  151. printf("lock mutex error %d\n", rc);
  152. }
  153. //all_count++;
  154. count++;
  155. #endif
  156. }
  157. void unlock() {
  158. #ifdef WIN32
  159. LeaveCriticalSection(&mutex);
  160. #else
  161. //all_count--;
  162. count--;
  163. int rc = pthread_mutex_unlock(&mutex);
  164. if (rc != 0)
  165. {
  166. printf("unlock mutex error %d\n", rc);
  167. }
  168. #endif
  169. }
  170. };
  171. //???????(?????)
  172. class ZTimer {
  173. public:
  174. static inline unsigned long now() { //????????
  175. #ifdef WIN32
  176. return GetTickCount();
  177. #else
  178. return 0;
  179. #endif
  180. }
  181. };
  182. //??????,?????????
  183. class ZThread {
  184. #ifdef WIN32
  185. unsigned long id;
  186. HANDLE handle;
  187. #else
  188. pthread_t p_thread;
  189. #endif
  190. public:
  191. bool bStop;
  192. ZThread() {
  193. #ifdef WIN32
  194. id = -1;
  195. #endif
  196. bStop = false;
  197. }
  198. void start();
  199. void stop();
  200. virtual int action() = 0;
  201. };
  202. #include <map>
  203. #include <list>
  204. namespace OnlineGameLib {
  205. /*
  206. * CNodeList
  207. */
  208. class CNodeList
  209. {
  210. public:
  211. class Node
  212. {
  213. public:
  214. Node *Next() const { return m_pNext; };
  215. void Next( Node *pNext );
  216. void AddToList( CNodeList *pList );
  217. void RemoveFromList();
  218. protected:
  219. Node();
  220. ~Node();
  221. private:
  222. friend class CNodeList;
  223. void Unlink();
  224. Node *m_pNext;
  225. Node *m_pPrev;
  226. CNodeList *m_pList;
  227. };
  228. CNodeList();
  229. void PushNode( Node *pNode );
  230. Node *PopNode();
  231. Node *Head() const { return m_pHead; };
  232. size_t Count() const { return m_numNodes; };
  233. bool Empty() const { return ( 0 == m_numNodes ); };
  234. private:
  235. friend void Node::RemoveFromList();
  236. void RemoveNode( Node *pNode );
  237. Node *m_pHead;
  238. size_t m_numNodes;
  239. };
  240. inline void CNodeList::Node::Next( Node *pNext )
  241. {
  242. m_pNext = pNext;
  243. if ( pNext )
  244. {
  245. pNext->m_pPrev = this;
  246. }
  247. }
  248. /*
  249. * TNodeList
  250. */
  251. template <class T> class TNodeList : public CNodeList
  252. {
  253. public:
  254. T *PopNode();
  255. T *Head() const;
  256. static T *Next( const T *pNode );
  257. };
  258. template <class T>
  259. T *TNodeList<T>::PopNode()
  260. {
  261. return static_cast< T* >( CNodeList::PopNode() );
  262. }
  263. template <class T>
  264. T *TNodeList<T>::Head() const
  265. {
  266. return static_cast< T* >( CNodeList::Head() );
  267. }
  268. template <class T>
  269. T *TNodeList<T>::Next( const T *pNode )
  270. {
  271. return static_cast< T* >( pNode->Next() );
  272. }
  273. } // End of namespace OnlineGameLib
  274. namespace OnlineGameLib {
  275. namespace Win32 {
  276. /*
  277. * CBuffer class
  278. */
  279. class CBuffer : public CNodeList::Node
  280. {
  281. public:
  282. class Allocator;
  283. friend class Allocator;
  284. size_t GetUsed() const { return m_used; };
  285. size_t GetSize() const { return m_size; };
  286. const BYTE *GetBuffer() const { return m_buffer_ptr; };
  287. CBuffer *GetHeadPack( size_t packLength );
  288. CBuffer *GetNextPack();
  289. bool HaveNextPack() { return ( m_offsetNextPack < m_used ) ? true : false; };
  290. void AddData( const char * const pData, size_t dataLength );
  291. void AddData( const BYTE * const pData, size_t dataLength );
  292. void AddData( BYTE data );
  293. void Use( size_t dataUsed ) { m_used += dataUsed; };
  294. CBuffer *SplitBuffer( size_t bytesToRemove );
  295. CBuffer *AllocateNewBuffer() const;
  296. void ConsumeAndRemove( size_t bytesToRemove );
  297. void Empty() { m_used = 0; };
  298. void AddRef() { m_ref++; };
  299. void Release();
  300. private:
  301. Allocator &m_allocator;
  302. long m_ref;
  303. const size_t m_size;
  304. size_t m_used;
  305. size_t m_packLength;
  306. size_t m_offsetNextPack;
  307. /*
  308. * start of the actual buffer, must remain the last
  309. * data member in the class.
  310. */
  311. BYTE *m_buffer_ptr; // four bytes aligned
  312. //BYTE m_buffer_base_addr[0];
  313. private:
  314. static void *operator new( size_t objSize, size_t bufferSize );
  315. static void operator delete( void *pObj, size_t bufferSize );
  316. CBuffer( Allocator &allocator, size_t size );
  317. ~CBuffer();
  318. /*
  319. * No copies do not implement
  320. */
  321. CBuffer( const CBuffer &rhs );
  322. CBuffer &operator=( const CBuffer &rhs );
  323. };
  324. inline void CBuffer::ConsumeAndRemove( size_t bytesToRemove )
  325. {
  326. m_used -= bytesToRemove;
  327. memmove( m_buffer_ptr, m_buffer_ptr + bytesToRemove, m_used );
  328. }
  329. /*
  330. * CBuffer::Allocator class
  331. */
  332. class CBuffer::Allocator
  333. {
  334. public:
  335. friend class CBuffer;
  336. explicit Allocator( size_t bufferSize, size_t maxFreeBuffers );
  337. virtual ~Allocator();
  338. CBuffer *Allocate();
  339. size_t GetBufferSize() const { return m_bufferSize; };
  340. bool ReSet( size_t bufferSize, size_t maxFreeBuffers )
  341. {
  342. memcpy((void*)&m_bufferSize, &bufferSize, sizeof( size_t ) );
  343. memcpy((void*)&m_maxFreeBuffers, &maxFreeBuffers, sizeof( size_t ) );
  344. return true;
  345. }
  346. protected:
  347. void Flush();
  348. private:
  349. void Release( CBuffer *pBuffer );
  350. virtual void OnBufferCreated() {}
  351. virtual void OnBufferAllocated() {}
  352. virtual void OnBufferReleased() {}
  353. void DestroyBuffer( CBuffer *pBuffer )
  354. {
  355. SAFE_DELETE( pBuffer );
  356. }
  357. const size_t m_bufferSize;
  358. typedef TNodeList< CBuffer > BufferList;
  359. BufferList m_freeList;
  360. BufferList m_activeList;
  361. const size_t m_maxFreeBuffers;
  362. /*
  363. * No copies do not implement
  364. */
  365. Allocator( const Allocator &rhs );
  366. Allocator &operator=( const Allocator &rhs );
  367. };
  368. } // End of namespace OnlineGameLib
  369. } // End of namespace Win32
  370. #endif