Team Fortress 2 Source Code as on 22/4/2020
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.

143 lines
3.5 KiB

  1. #ifndef CRYPTOPP_WINPIPES_H
  2. #define CRYPTOPP_WINPIPES_H
  3. #ifdef WINDOWS_PIPES_AVAILABLE
  4. #include "cryptlib.h"
  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() const {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. HANDLE GetHandle() const {return m_event;}
  57. unsigned int GetMaxWaitObjectCount() const {return 1;}
  58. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  59. private:
  60. WindowsHandle m_event;
  61. OVERLAPPED m_overlapped;
  62. bool m_resultPending;
  63. DWORD m_lastResult;
  64. bool m_eofReceived;
  65. };
  66. //! pipe-based implementation of NetworkSender
  67. class WindowsPipeSender : public WindowsPipe, public NetworkSender
  68. {
  69. public:
  70. WindowsPipeSender();
  71. bool MustWaitForResult() {return true;}
  72. void Send(const byte* buf, size_t bufLen);
  73. unsigned int GetSendResult();
  74. bool MustWaitForEof() { return false; }
  75. void SendEof() {}
  76. HANDLE GetHandle() const {return m_event;}
  77. unsigned int GetMaxWaitObjectCount() const {return 1;}
  78. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  79. private:
  80. WindowsHandle m_event;
  81. OVERLAPPED m_overlapped;
  82. bool m_resultPending;
  83. DWORD m_lastResult;
  84. };
  85. //! Windows Pipe Source
  86. class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
  87. {
  88. public:
  89. WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
  90. : WindowsHandle(h), NetworkSource(attachment)
  91. {
  92. if (pumpAll)
  93. PumpAll();
  94. }
  95. using NetworkSource::GetMaxWaitObjectCount;
  96. using NetworkSource::GetWaitObjects;
  97. private:
  98. HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
  99. NetworkReceiver & AccessReceiver() {return *this;}
  100. };
  101. //! Windows Pipe Sink
  102. class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
  103. {
  104. public:
  105. WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
  106. : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
  107. using NetworkSink::GetMaxWaitObjectCount;
  108. using NetworkSink::GetWaitObjects;
  109. private:
  110. HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
  111. NetworkSender & AccessSender() {return *this;}
  112. };
  113. NAMESPACE_END
  114. #endif // WINDOWS_PIPES_AVAILABLE
  115. #endif