NamedPipe.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Profiler.h"
  5. #include "../IO/NamedPipe.h"
  6. #include "../IO/Log.h"
  7. #ifdef _WIN32
  8. #include "../Engine/WinWrapped.h"
  9. #else
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <csignal>
  13. #include <unistd.h>
  14. #endif
  15. #include "../DebugNew.h"
  16. namespace Urho3D
  17. {
  18. static constexpr i32 PIPE_BUFFER_SIZE = 65536;
  19. NamedPipe::NamedPipe(Context* context) :
  20. Object(context),
  21. isServer_(false),
  22. #ifdef _WIN32
  23. handle_(INVALID_HANDLE_VALUE)
  24. #else
  25. readHandle_(-1),
  26. writeHandle_(-1)
  27. #endif
  28. {
  29. }
  30. NamedPipe::NamedPipe(Context* context, const String& name, bool isServer) :
  31. Object(context),
  32. isServer_(false),
  33. #ifdef _WIN32
  34. handle_(INVALID_HANDLE_VALUE)
  35. #else
  36. readHandle_(-1),
  37. writeHandle_(-1)
  38. #endif
  39. {
  40. Open(name, isServer);
  41. }
  42. NamedPipe::~NamedPipe()
  43. {
  44. Close();
  45. }
  46. i64 NamedPipe::Seek(i64 position)
  47. {
  48. return 0;
  49. }
  50. #ifdef _WIN32
  51. static const char* pipePath = "\\\\.\\pipe\\";
  52. bool NamedPipe::Open(const String& name, bool isServer)
  53. {
  54. URHO3D_PROFILE(OpenNamedPipe);
  55. Close();
  56. isServer_ = false;
  57. if (isServer)
  58. {
  59. handle_ = CreateNamedPipeW(WString(pipePath + name).CString(),
  60. PIPE_ACCESS_DUPLEX,
  61. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT,
  62. 1,
  63. PIPE_BUFFER_SIZE,
  64. PIPE_BUFFER_SIZE,
  65. 0,
  66. nullptr
  67. );
  68. if (handle_ == INVALID_HANDLE_VALUE)
  69. {
  70. URHO3D_LOGERROR("Failed to create named pipe " + name);
  71. return false;
  72. }
  73. else
  74. {
  75. URHO3D_LOGDEBUG("Created named pipe " + name);
  76. name_ = name;
  77. isServer_ = true;
  78. return true;
  79. }
  80. }
  81. else
  82. {
  83. handle_ = CreateFileW(
  84. WString(pipePath + name).CString(),
  85. GENERIC_READ | GENERIC_WRITE,
  86. 0,
  87. nullptr,
  88. OPEN_EXISTING,
  89. 0,
  90. nullptr
  91. );
  92. if (handle_ == INVALID_HANDLE_VALUE)
  93. {
  94. URHO3D_LOGERROR("Failed to connect to named pipe " + name);
  95. return false;
  96. }
  97. else
  98. {
  99. URHO3D_LOGDEBUG("Connected to named pipe " + name);
  100. name_ = name;
  101. return true;
  102. }
  103. }
  104. }
  105. i32 NamedPipe::Read(void* dest, i32 size)
  106. {
  107. assert(size >= 0);
  108. if (handle_ != INVALID_HANDLE_VALUE)
  109. {
  110. DWORD read = 0;
  111. ReadFile(handle_, dest, size, &read, nullptr);
  112. return read;
  113. }
  114. return 0;
  115. }
  116. i32 NamedPipe::Write(const void* data, i32 size)
  117. {
  118. assert(size >= 0);
  119. if (handle_ != INVALID_HANDLE_VALUE)
  120. {
  121. DWORD written = 0;
  122. WriteFile(handle_, data, size, &written, nullptr);
  123. return written;
  124. }
  125. return 0;
  126. }
  127. void NamedPipe::Close()
  128. {
  129. if (handle_ != INVALID_HANDLE_VALUE)
  130. {
  131. URHO3D_PROFILE(CloseNamedPipe);
  132. if (isServer_)
  133. {
  134. DisconnectNamedPipe(handle_);
  135. isServer_ = false;
  136. }
  137. CloseHandle(handle_);
  138. handle_ = INVALID_HANDLE_VALUE;
  139. name_.Clear();
  140. URHO3D_LOGDEBUG("Closed named pipe " + name_);
  141. }
  142. }
  143. bool NamedPipe::IsOpen() const
  144. {
  145. return handle_ != INVALID_HANDLE_VALUE;
  146. }
  147. bool NamedPipe::IsEof() const
  148. {
  149. if (handle_ != INVALID_HANDLE_VALUE)
  150. {
  151. DWORD bytesAvailable = 0;
  152. PeekNamedPipe(handle_, nullptr, 0, nullptr, &bytesAvailable, nullptr);
  153. return bytesAvailable == 0;
  154. }
  155. else
  156. return true;
  157. }
  158. #else
  159. static const char* pipePath = "/tmp/";
  160. #define SAFE_CLOSE(handle) if ((handle) != -1) { close(handle); (handle) = -1; }
  161. bool NamedPipe::Open(const String& name, bool isServer)
  162. {
  163. #ifdef __EMSCRIPTEN__
  164. URHO3D_LOGERROR("Opening a named pipe not supported on Web platform");
  165. return false;
  166. #else
  167. URHO3D_PROFILE(OpenNamedPipe);
  168. Close();
  169. isServer_ = false;
  170. String serverReadName = pipePath + name + "SR";
  171. String clientReadName = pipePath + name + "CR";
  172. // Make sure SIGPIPE is ignored and will not lead to process termination
  173. signal(SIGPIPE, SIG_IGN);
  174. if (isServer)
  175. {
  176. mkfifo(serverReadName.CString(), 0660);
  177. mkfifo(clientReadName.CString(), 0660);
  178. readHandle_ = open(serverReadName.CString(), O_RDONLY | O_NDELAY);
  179. writeHandle_ = open(clientReadName.CString(), O_WRONLY | O_NDELAY);
  180. if (readHandle_ == -1 && writeHandle_ == -1)
  181. {
  182. URHO3D_LOGERROR("Failed to create named pipe " + name);
  183. SAFE_CLOSE(readHandle_);
  184. SAFE_CLOSE(writeHandle_);
  185. unlink(serverReadName.CString());
  186. unlink(clientReadName.CString());
  187. return false;
  188. }
  189. else
  190. {
  191. URHO3D_LOGDEBUG("Created named pipe " + name);
  192. name_ = name;
  193. isServer_ = true;
  194. return true;
  195. }
  196. }
  197. else
  198. {
  199. readHandle_ = open(clientReadName.CString(), O_RDONLY | O_NDELAY);
  200. writeHandle_ = open(serverReadName.CString(), O_WRONLY | O_NDELAY);
  201. if (readHandle_ == -1 && writeHandle_ == -1)
  202. {
  203. URHO3D_LOGERROR("Failed to connect to named pipe " + name);
  204. SAFE_CLOSE(readHandle_);
  205. SAFE_CLOSE(writeHandle_);
  206. return false;
  207. }
  208. else
  209. {
  210. URHO3D_LOGDEBUG("Connected to named pipe " + name);
  211. name_ = name;
  212. return true;
  213. }
  214. }
  215. #endif
  216. }
  217. i32 NamedPipe::Read(void* dest, i32 size)
  218. {
  219. assert(size >= 0);
  220. // Attempt to open late if only the write handle is open yet
  221. if (readHandle_ == -1 && writeHandle_ != -1)
  222. {
  223. if (isServer_)
  224. readHandle_ = open((pipePath + name_ + "SR").CString(), O_RDONLY | O_NDELAY);
  225. else
  226. readHandle_ = open((pipePath + name_ + "CR").CString(), O_RDONLY | O_NDELAY);
  227. }
  228. if (readHandle_ != -1)
  229. {
  230. ssize_t readNow = read(readHandle_, dest, size);
  231. return readNow < 0 ? 0 : (unsigned)readNow;
  232. }
  233. else
  234. return 0;
  235. }
  236. i32 NamedPipe::Write(const void* data, i32 size)
  237. {
  238. assert(size >= 0);
  239. // Attempt to open late if only the read handle is open yet
  240. if (writeHandle_ == -1 && readHandle_ != -1)
  241. {
  242. if (isServer_)
  243. writeHandle_ = open((pipePath + name_ + "CR").CString(), O_WRONLY | O_NDELAY);
  244. else
  245. writeHandle_ = open((pipePath + name_ + "SR").CString(), O_WRONLY | O_NDELAY);
  246. }
  247. // Loop until all bytes written in case of partial write
  248. if (writeHandle_ != -1)
  249. {
  250. ssize_t written = 0;
  251. while (written < (ssize_t)size)
  252. {
  253. ssize_t writtenNow = write(writeHandle_, ((const unsigned char*)data) + written, size - written);
  254. if (writtenNow < 0)
  255. return 0; // Error while writing
  256. written += writtenNow;
  257. }
  258. return written;
  259. }
  260. else
  261. return 0;
  262. }
  263. void NamedPipe::Close()
  264. {
  265. if (readHandle_ != -1 || writeHandle_ != -1)
  266. {
  267. URHO3D_PROFILE(CloseNamedPipe);
  268. SAFE_CLOSE(readHandle_);
  269. SAFE_CLOSE(writeHandle_);
  270. if (isServer_)
  271. {
  272. String serverReadName = pipePath + name_ + "SR";
  273. String clientReadName = pipePath + name_ + "CR";
  274. unlink(serverReadName.CString());
  275. unlink(clientReadName.CString());
  276. isServer_ = false;
  277. }
  278. name_.Clear();
  279. }
  280. }
  281. bool NamedPipe::IsOpen() const
  282. {
  283. return readHandle_ != -1 || writeHandle_ != -1;
  284. }
  285. bool NamedPipe::IsEof() const
  286. {
  287. #ifdef __EMSCRIPTEN__
  288. return true;
  289. #else
  290. // Attempt to open late if only the write handle is open yet
  291. if (readHandle_ == -1 && writeHandle_ != -1)
  292. {
  293. if (isServer_)
  294. readHandle_ = open((pipePath + name_ + "SR").CString(), O_RDONLY | O_NDELAY);
  295. else
  296. readHandle_ = open((pipePath + name_ + "CR").CString(), O_RDONLY | O_NDELAY);
  297. }
  298. if (readHandle_ != -1)
  299. {
  300. fd_set set;
  301. FD_ZERO(&set); // NOLINT(modernize-use-bool-literals)
  302. FD_SET(readHandle_, &set);
  303. struct timeval timeout{0, 1000}; // 1ms timeout for select
  304. return select(readHandle_ + 1, &set, nullptr, nullptr, &timeout) <= 0;
  305. }
  306. else
  307. return true;
  308. #endif
  309. }
  310. #endif
  311. void NamedPipe::SetName(const String &name)
  312. {
  313. URHO3D_LOGERROR("Cannot change name of the NamedPipe!");
  314. assert(0);
  315. }
  316. }