PerSocketContext.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /************************************************************************/
  2. /* Copyright (C), 2016-2020, [IT], 保留所有权利;
  3. /* 模 块 名:PerSocketContext;
  4. /* 描 述:重叠IO缓冲类,每个客户端连接对象类;
  5. /*
  6. /* 版 本:[V];
  7. /* 作 者:[IT];
  8. /* 日 期:[2/23/2016];
  9. /*
  10. /*
  11. /* 注 意:;
  12. /*
  13. /* 修改记录:[IT];
  14. /* 修改日期:;
  15. /* 修改版本:;
  16. /* 修改内容:;
  17. /************************************************************************/
  18. #ifndef __PER_SOCKET_CONTEXT_20160303__
  19. #define __PER_SOCKET_CONTEXT_20160303__
  20. #pragma once
  21. #include <vector>
  22. #include <list>
  23. #include "SockAddrIn.h"
  24. class PerSocketContext
  25. {
  26. public:
  27. PerSocketContext(SOCKET sock):_socket(sock){
  28. npendingSize = 0;
  29. ncurSize = 0;
  30. pendingbuf = NULL;
  31. memset(&_Overlapped, 0, sizeof(_Overlapped));
  32. }
  33. explicit PerSocketContext(SOCKET sock, size_t size): _socket(sock){
  34. npendingSize = 0;
  35. ncurSize= 0;
  36. pendingbuf = NULL;
  37. memset(&_Overlapped, 0, sizeof(_Overlapped));
  38. _data.resize( size );
  39. }
  40. PerSocketContext(const PerSocketContext& sbuf){
  41. Copy(sbuf);
  42. }
  43. PerSocketContext& operator=(const PerSocketContext& sbuf){
  44. return Copy(sbuf);
  45. }
  46. ~PerSocketContext(){
  47. Free();
  48. }
  49. bool IsValid() const { return (_socket != INVALID_SOCKET); }
  50. void ReAlloc( size_t count) { _data.resize(count); }
  51. void Free()
  52. {
  53. _data.clear();
  54. npendingSize = 0;
  55. ncurSize = 0;
  56. if ( pendingbuf )
  57. {
  58. delete []pendingbuf;
  59. pendingbuf = NULL;
  60. }
  61. }
  62. size_t BufferSize() const { return _data.size(); }
  63. PerSocketContext& Copy(const PerSocketContext& sbuf)
  64. {
  65. if ( this != &sbuf )
  66. {
  67. _socket = sbuf._socket;
  68. _sockAddr = sbuf._sockAddr;
  69. if ( !sbuf._data.empty() )
  70. {
  71. _data.resize( sbuf._data.size() );
  72. memcpy(&_data[0], &(sbuf._data[0]), _data.size());
  73. }
  74. /*if ( ncurSize && pendingbuf )
  75. {
  76. delete []pendingbuf;
  77. pendingbuf = NULL;
  78. }*/
  79. ncurSize = sbuf.ncurSize;
  80. npendingSize = sbuf.npendingSize;
  81. pendingbuf = new unsigned char[npendingSize];
  82. memset(pendingbuf, 0, npendingSize);
  83. memcpy(pendingbuf, sbuf.pendingbuf, ncurSize);
  84. }
  85. return *this;
  86. }
  87. void SetPendingCurPack(const int& nCurSize)
  88. {
  89. if ( pendingbuf )
  90. {
  91. delete []pendingbuf;
  92. pendingbuf = NULL;
  93. }
  94. ncurSize = nCurSize;
  95. pendingbuf = new unsigned char[nCurSize];
  96. memset(pendingbuf, 0, nCurSize);
  97. }
  98. void SetPendingPack(const int& nSize)
  99. {
  100. if ( pendingbuf )
  101. {
  102. delete []pendingbuf;
  103. pendingbuf = NULL;
  104. }
  105. npendingSize = nSize;
  106. pendingbuf = new unsigned char[nSize];
  107. memset(pendingbuf, 0, nSize);
  108. }
  109. void ReSetPengingPack(const int& nSize)
  110. {
  111. if ( pendingbuf )
  112. {
  113. unsigned char *ptemp = new unsigned char[ncurSize];
  114. memcpy(ptemp, pendingbuf, ncurSize);
  115. delete []pendingbuf;
  116. pendingbuf = NULL;
  117. if ( npendingSize )
  118. {
  119. pendingbuf = new unsigned char[npendingSize];
  120. memset(pendingbuf, 0, npendingSize);
  121. memcpy(pendingbuf, ptemp, ncurSize);
  122. }
  123. else
  124. {
  125. pendingbuf = new unsigned char[nSize];
  126. memset(pendingbuf, 0, nSize);
  127. memcpy(pendingbuf, ptemp, ncurSize);
  128. }
  129. }
  130. }
  131. // Quick access operator
  132. operator SOCKET() { return _socket; }
  133. operator SOCKET() const { return _socket; }
  134. operator SockAddrIn&() { return _sockAddr; }
  135. operator const SockAddrIn&() const { return _sockAddr; }
  136. operator LPSOCKADDR() { return _sockAddr; }
  137. operator LPWSAOVERLAPPED() { return &_Overlapped; }
  138. operator LPBYTE() { return &_data[0]; }
  139. bool IsEqual(const PerSocketContext& sbuf) const { return (_socket == sbuf._socket); }
  140. bool operator==(const PerSocketContext& sbuf) const { return IsEqual( sbuf ); }
  141. bool operator==(SOCKET sock) const { return (_socket == sock); }
  142. bool operator!=(const PerSocketContext& sbuf) const { return !IsEqual( sbuf ); }
  143. private:
  144. WSAOVERLAPPED _Overlapped;
  145. std::vector<unsigned char> _data;
  146. SOCKET _socket;
  147. SockAddrIn _sockAddr;
  148. PerSocketContext();
  149. public:
  150. unsigned int npendingSize; // 总包大小(多个分包);
  151. unsigned int ncurSize; // 当前接收到的分包接收的大小;
  152. unsigned char *pendingbuf; // 超过一次接收包,需要分包处理的包缓冲区;
  153. };
  154. typedef std::list<PerSocketContext> SocketContextList;
  155. #endif // __PER_SOCKET_CONTEXT_20160303__