file_access_windows_pipe.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**************************************************************************/
  2. /* file_access_windows_pipe.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef WINDOWS_ENABLED
  31. #include "file_access_windows_pipe.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. Error FileAccessWindowsPipe::open_existing(HANDLE p_rfd, HANDLE p_wfd, bool p_blocking) {
  35. // Open pipe using handles created by CreatePipe(rfd, wfd, NULL, 4096) call in the OS.execute_with_pipe.
  36. _close();
  37. path_src = String();
  38. ERR_FAIL_COND_V_MSG(fd[0] != nullptr || fd[1] != nullptr, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  39. fd[0] = p_rfd;
  40. fd[1] = p_wfd;
  41. if (!p_blocking) {
  42. DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
  43. SetNamedPipeHandleState(fd[0], &mode, nullptr, nullptr);
  44. SetNamedPipeHandleState(fd[1], &mode, nullptr, nullptr);
  45. }
  46. last_error = OK;
  47. return OK;
  48. }
  49. Error FileAccessWindowsPipe::open_internal(const String &p_path, int p_mode_flags) {
  50. _close();
  51. path_src = p_path;
  52. ERR_FAIL_COND_V_MSG(fd[0] != nullptr || fd[1] != nullptr, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  53. path = String("\\\\.\\pipe\\LOCAL\\") + p_path.replace("pipe://", "").replace("/", "_");
  54. HANDLE h = CreateFileW((LPCWSTR)path.utf16().get_data(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  55. if (h == INVALID_HANDLE_VALUE) {
  56. h = CreateNamedPipeW((LPCWSTR)path.utf16().get_data(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 4096, 4096, 0, nullptr);
  57. if (h == INVALID_HANDLE_VALUE) {
  58. last_error = ERR_FILE_CANT_OPEN;
  59. return last_error;
  60. }
  61. ConnectNamedPipe(h, nullptr);
  62. }
  63. fd[0] = h;
  64. fd[1] = h;
  65. last_error = OK;
  66. return OK;
  67. }
  68. void FileAccessWindowsPipe::_close() {
  69. if (fd[0] == nullptr) {
  70. return;
  71. }
  72. if (fd[1] != fd[0]) {
  73. CloseHandle(fd[1]);
  74. }
  75. CloseHandle(fd[0]);
  76. fd[0] = nullptr;
  77. fd[1] = nullptr;
  78. }
  79. bool FileAccessWindowsPipe::is_open() const {
  80. return (fd[0] != nullptr || fd[1] != nullptr);
  81. }
  82. String FileAccessWindowsPipe::get_path() const {
  83. return path_src;
  84. }
  85. String FileAccessWindowsPipe::get_path_absolute() const {
  86. return path_src;
  87. }
  88. uint64_t FileAccessWindowsPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  89. ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
  90. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  91. DWORD read = 0;
  92. if (!ReadFile(fd[0], p_dst, p_length, &read, nullptr) || read != p_length) {
  93. last_error = ERR_FILE_CANT_READ;
  94. } else {
  95. last_error = OK;
  96. }
  97. return read;
  98. }
  99. Error FileAccessWindowsPipe::get_error() const {
  100. return last_error;
  101. }
  102. void FileAccessWindowsPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  103. ERR_FAIL_COND_MSG(fd[1] == nullptr, "Pipe must be opened before use.");
  104. ERR_FAIL_COND(!p_src && p_length > 0);
  105. DWORD read = -1;
  106. bool ok = WriteFile(fd[1], p_src, p_length, &read, nullptr);
  107. if (!ok || read != p_length) {
  108. last_error = ERR_FILE_CANT_WRITE;
  109. } else {
  110. last_error = OK;
  111. }
  112. }
  113. void FileAccessWindowsPipe::close() {
  114. _close();
  115. }
  116. FileAccessWindowsPipe::~FileAccessWindowsPipe() {
  117. _close();
  118. }
  119. #endif // WINDOWS_ENABLED