PipeImpl_WIN32.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // PipeImpl_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/PipeImpl_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: PipeImpl
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/PipeImpl_WIN32.h"
  16. #include "Poco/Exception.h"
  17. namespace Poco {
  18. PipeImpl::PipeImpl()
  19. {
  20. SECURITY_ATTRIBUTES attr;
  21. attr.nLength = sizeof(attr);
  22. attr.lpSecurityDescriptor = NULL;
  23. attr.bInheritHandle = FALSE;
  24. if (!CreatePipe(&_readHandle, &_writeHandle, &attr, 0))
  25. throw CreateFileException("anonymous pipe");
  26. }
  27. PipeImpl::~PipeImpl()
  28. {
  29. closeRead();
  30. closeWrite();
  31. }
  32. int PipeImpl::writeBytes(const void* buffer, int length)
  33. {
  34. poco_assert (_writeHandle != INVALID_HANDLE_VALUE);
  35. DWORD bytesWritten = 0;
  36. if (!WriteFile(_writeHandle, buffer, length, &bytesWritten, NULL))
  37. throw WriteFileException("anonymous pipe");
  38. return bytesWritten;
  39. }
  40. int PipeImpl::readBytes(void* buffer, int length)
  41. {
  42. poco_assert (_readHandle != INVALID_HANDLE_VALUE);
  43. DWORD bytesRead = 0;
  44. BOOL ok = ReadFile(_readHandle, buffer, length, &bytesRead, NULL);
  45. if (ok || GetLastError() == ERROR_BROKEN_PIPE)
  46. return bytesRead;
  47. else
  48. throw ReadFileException("anonymous pipe");
  49. }
  50. PipeImpl::Handle PipeImpl::readHandle() const
  51. {
  52. return _readHandle;
  53. }
  54. PipeImpl::Handle PipeImpl::writeHandle() const
  55. {
  56. return _writeHandle;
  57. }
  58. void PipeImpl::closeRead()
  59. {
  60. if (_readHandle != INVALID_HANDLE_VALUE)
  61. {
  62. CloseHandle(_readHandle);
  63. _readHandle = INVALID_HANDLE_VALUE;
  64. }
  65. }
  66. void PipeImpl::closeWrite()
  67. {
  68. if (_writeHandle != INVALID_HANDLE_VALUE)
  69. {
  70. CloseHandle(_writeHandle);
  71. _writeHandle = INVALID_HANDLE_VALUE;
  72. }
  73. }
  74. } // namespace Poco