file_access_windows_pipe.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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) {
  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] != 0 || fd[1] != 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  39. fd[0] = p_rfd;
  40. fd[1] = p_wfd;
  41. last_error = OK;
  42. return OK;
  43. }
  44. Error FileAccessWindowsPipe::open_internal(const String &p_path, int p_mode_flags) {
  45. _close();
  46. path_src = p_path;
  47. ERR_FAIL_COND_V_MSG(fd[0] != 0 || fd[1] != 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  48. path = String("\\\\.\\pipe\\LOCAL\\") + p_path.replace("pipe://", "").replace("/", "_");
  49. HANDLE h = CreateFileW((LPCWSTR)path.utf16().get_data(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  50. if (h == INVALID_HANDLE_VALUE) {
  51. h = CreateNamedPipeW((LPCWSTR)path.utf16().get_data(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 4096, 4096, 0, nullptr);
  52. if (h == INVALID_HANDLE_VALUE) {
  53. last_error = ERR_FILE_CANT_OPEN;
  54. return last_error;
  55. }
  56. ConnectNamedPipe(h, NULL);
  57. }
  58. fd[0] = h;
  59. fd[1] = h;
  60. last_error = OK;
  61. return OK;
  62. }
  63. void FileAccessWindowsPipe::_close() {
  64. if (fd[0] == 0) {
  65. return;
  66. }
  67. if (fd[1] != fd[0]) {
  68. CloseHandle(fd[1]);
  69. }
  70. CloseHandle(fd[0]);
  71. fd[0] = 0;
  72. fd[1] = 0;
  73. }
  74. bool FileAccessWindowsPipe::is_open() const {
  75. return (fd[0] != 0 || fd[1] != 0);
  76. }
  77. String FileAccessWindowsPipe::get_path() const {
  78. return path_src;
  79. }
  80. String FileAccessWindowsPipe::get_path_absolute() const {
  81. return path_src;
  82. }
  83. uint8_t FileAccessWindowsPipe::get_8() const {
  84. ERR_FAIL_COND_V_MSG(fd[0] == 0, 0, "Pipe must be opened before use.");
  85. uint8_t b;
  86. if (!ReadFile(fd[0], &b, 1, nullptr, nullptr)) {
  87. last_error = ERR_FILE_CANT_READ;
  88. b = '\0';
  89. } else {
  90. last_error = OK;
  91. }
  92. return b;
  93. }
  94. uint64_t FileAccessWindowsPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  95. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  96. ERR_FAIL_COND_V_MSG(fd[0] == 0, -1, "Pipe must be opened before use.");
  97. DWORD read = -1;
  98. if (!ReadFile(fd[0], p_dst, p_length, &read, nullptr) || read != p_length) {
  99. last_error = ERR_FILE_CANT_READ;
  100. } else {
  101. last_error = OK;
  102. }
  103. return read;
  104. }
  105. Error FileAccessWindowsPipe::get_error() const {
  106. return last_error;
  107. }
  108. void FileAccessWindowsPipe::store_8(uint8_t p_src) {
  109. ERR_FAIL_COND_MSG(fd[1] == 0, "Pipe must be opened before use.");
  110. if (!WriteFile(fd[1], &p_src, 1, nullptr, nullptr)) {
  111. last_error = ERR_FILE_CANT_WRITE;
  112. } else {
  113. last_error = OK;
  114. }
  115. }
  116. void FileAccessWindowsPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  117. ERR_FAIL_COND_MSG(fd[1] == 0, "Pipe must be opened before use.");
  118. ERR_FAIL_COND(!p_src && p_length > 0);
  119. DWORD read = -1;
  120. bool ok = WriteFile(fd[1], p_src, p_length, &read, nullptr);
  121. if (!ok || read != p_length) {
  122. last_error = ERR_FILE_CANT_WRITE;
  123. } else {
  124. last_error = OK;
  125. }
  126. }
  127. void FileAccessWindowsPipe::close() {
  128. _close();
  129. }
  130. FileAccessWindowsPipe::~FileAccessWindowsPipe() {
  131. _close();
  132. }
  133. #endif // WINDOWS_ENABLED