IOSocket.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. class CIOBuffer;
  3. struct IO_STRUCT
  4. {
  5. enum
  6. {
  7. BASE_SIZE = 64,
  8. ALLOC_ARG = 256
  9. };
  10. IO_STRUCT* pNext;
  11. CIOBuffer* pBuffer;
  12. IO_STRUCT() :pNext(nullptr), pBuffer(nullptr)
  13. {
  14. }
  15. };
  16. class CIOSocket
  17. {
  18. public:
  19. enum {
  20. ALLOC_ARG = 5,
  21. BASE_SIZE = 2
  22. };
  23. CIOSocket(HANDLE hCom, SOCKET s);
  24. ~CIOSocket();
  25. static unsigned int __stdcall IOThread(void* param);
  26. virtual void OnCreate() = 0;
  27. virtual void OnClose() = 0;
  28. virtual void OnRead() = 0;
  29. void Read(DWORD dwLeft);
  30. void Close();
  31. void Write(CIOBuffer* pBuffer);
  32. void WriteCallback(DWORD dwTransferred);
  33. void ReadCallback(DWORD dwTransferred);
  34. void AddRef();
  35. void Release();
  36. void OnIOCallback(BOOL bSucc, DWORD dwTransferred, LPOVERLAPPED lpOverlapped);
  37. protected:
  38. bool Init(HANDLE hCom);
  39. HANDLE m_hComplete;
  40. SOCKET m_hSocket;
  41. CIOBuffer* m_pBuffer;
  42. IO_STRUCT* m_pFirstHead;
  43. IO_STRUCT* m_pLastHead;
  44. OVERLAPPED m_OverlappedRead;
  45. OVERLAPPED m_OverlappedWrite;
  46. std::mutex m_lock;
  47. DWORD m_dwBufferCount;
  48. WSABUF m_WriteBuff[64];
  49. DWORD m_dwWriteCount;
  50. DWORD m_IOCount;
  51. bool m_bSending;
  52. long m_lRef;
  53. };