Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.4 KiB

  1. #ifndef CRYPTOPP_WINPIPES_H
  2. #define CRYPTOPP_WINPIPES_H
  3. #include "config.h"
  4. #ifdef WINDOWS_PIPES_AVAILABLE
  5. #include "network.h"
  6. #include "queue.h"
  7. #include <winsock2.h>
  8. NAMESPACE_BEGIN(CryptoPP)
  9. //! Windows Handle
  10. class WindowsHandle
  11. {
  12. public:
  13. WindowsHandle(HANDLE h = INVALID_HANDLE_VALUE, bool own=false);
  14. WindowsHandle(const WindowsHandle &h) : m_h(h.m_h), m_own(false) {}
  15. virtual ~WindowsHandle();
  16. bool GetOwnership() const {return m_own;}
  17. void SetOwnership(bool own) {m_own = own;}
  18. operator HANDLE() {return m_h;}
  19. HANDLE GetHandle() const {return m_h;}
  20. bool HandleValid() const;
  21. void AttachHandle(HANDLE h, bool own=false);
  22. HANDLE DetachHandle();
  23. void CloseHandle();
  24. protected:
  25. virtual void HandleChanged() {}
  26. HANDLE m_h;
  27. bool m_own;
  28. };
  29. //! Windows Pipe
  30. class WindowsPipe
  31. {
  32. public:
  33. class Err : public OS_Error
  34. {
  35. public:
  36. Err(HANDLE h, const std::string& operation, int error);
  37. HANDLE GetHandle() const {return m_h;}
  38. private:
  39. HANDLE m_h;
  40. };
  41. protected:
  42. virtual HANDLE GetHandle() const =0;
  43. virtual void HandleError(const char *operation) const;
  44. void CheckAndHandleError(const char *operation, BOOL result) const
  45. {assert(result==TRUE || result==FALSE); if (!result) HandleError(operation);}
  46. };
  47. //! pipe-based implementation of NetworkReceiver
  48. class WindowsPipeReceiver : public WindowsPipe, public NetworkReceiver
  49. {
  50. public:
  51. WindowsPipeReceiver();
  52. bool MustWaitForResult() {return true;}
  53. bool Receive(byte* buf, size_t bufLen);
  54. unsigned int GetReceiveResult();
  55. bool EofReceived() const {return m_eofReceived;}
  56. unsigned int GetMaxWaitObjectCount() const {return 1;}
  57. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  58. private:
  59. WindowsHandle m_event;
  60. OVERLAPPED m_overlapped;
  61. bool m_resultPending;
  62. DWORD m_lastResult;
  63. bool m_eofReceived;
  64. };
  65. //! pipe-based implementation of NetworkSender
  66. class WindowsPipeSender : public WindowsPipe, public NetworkSender
  67. {
  68. public:
  69. WindowsPipeSender();
  70. bool MustWaitForResult() {return true;}
  71. void Send(const byte* buf, size_t bufLen);
  72. unsigned int GetSendResult();
  73. bool MustWaitForEof() { return false; }
  74. void SendEof() {}
  75. unsigned int GetMaxWaitObjectCount() const {return 1;}
  76. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  77. private:
  78. WindowsHandle m_event;
  79. OVERLAPPED m_overlapped;
  80. bool m_resultPending;
  81. DWORD m_lastResult;
  82. };
  83. //! Windows Pipe Source
  84. class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
  85. {
  86. public:
  87. WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
  88. : WindowsHandle(h), NetworkSource(attachment)
  89. {
  90. if (pumpAll)
  91. PumpAll();
  92. }
  93. NetworkSource::GetMaxWaitObjectCount;
  94. NetworkSource::GetWaitObjects;
  95. private:
  96. HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
  97. NetworkReceiver & AccessReceiver() {return *this;}
  98. };
  99. //! Windows Pipe Sink
  100. class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
  101. {
  102. public:
  103. WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
  104. : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
  105. NetworkSink::GetMaxWaitObjectCount;
  106. NetworkSink::GetWaitObjects;
  107. private:
  108. HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
  109. NetworkSender & AccessSender() {return *this;}
  110. };
  111. NAMESPACE_END
  112. #endif
  113. #endif