PerSocketContext.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. memset(&_Overlapped, 0, sizeof(_Overlapped));
  29. }
  30. explicit PerSocketContext(SOCKET sock, size_t size): _socket(sock){
  31. memset(&_Overlapped, 0, sizeof(_Overlapped));
  32. _data.resize( size );
  33. }
  34. PerSocketContext(const PerSocketContext& sbuf){
  35. Copy(sbuf);
  36. }
  37. PerSocketContext& operator=(const PerSocketContext& sbuf){
  38. return Copy(sbuf);
  39. }
  40. ~PerSocketContext(){
  41. Free();
  42. }
  43. bool IsValid() const { return (_socket != INVALID_SOCKET); }
  44. void ReAlloc( size_t count) { _data.resize(count); }
  45. void Free()
  46. {
  47. _data.clear();
  48. }
  49. size_t BufferSize() const { return _data.size(); }
  50. PerSocketContext& Copy(const PerSocketContext& sbuf)
  51. {
  52. if ( this != &sbuf )
  53. {
  54. _socket = sbuf._socket;
  55. _sockAddr = sbuf._sockAddr;
  56. if ( !sbuf._data.empty() )
  57. {
  58. _data.resize( sbuf._data.size() );
  59. memcpy(&_data[0], &(sbuf._data[0]), _data.size());
  60. }
  61. }
  62. return *this;
  63. }
  64. // Quick access operator
  65. operator SOCKET() { return _socket; }
  66. operator SOCKET() const { return _socket; }
  67. operator SockAddrIn&() { return _sockAddr; }
  68. operator const SockAddrIn&() const { return _sockAddr; }
  69. operator LPSOCKADDR() { return _sockAddr; }
  70. operator LPWSAOVERLAPPED() { return &_Overlapped; }
  71. operator LPBYTE() { return &_data[0]; }
  72. bool IsEqual(const PerSocketContext& sbuf) const { return (_socket == sbuf._socket); }
  73. bool operator==(const PerSocketContext& sbuf) const { return IsEqual( sbuf ); }
  74. bool operator==(SOCKET sock) const { return (_socket == sock); }
  75. bool operator!=(const PerSocketContext& sbuf) const { return !IsEqual( sbuf ); }
  76. private:
  77. SOCKET _socket;
  78. SockAddrIn _sockAddr;
  79. WSAOVERLAPPED _Overlapped;
  80. std::vector<unsigned char> _data;
  81. PerSocketContext();
  82. };
  83. typedef std::list<PerSocketContext> SocketContextList;
  84. #endif // __PER_SOCKET_CONTEXT_20160303__