NamedPipe.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../IO/NamedPipe.h"
  25. #include "../IO/Log.h"
  26. #ifdef _WIN32
  27. #include <windows.h>
  28. #else
  29. #include <fcntl.h>
  30. #include <sys/stat.h>
  31. #include <signal.h>
  32. #include <unistd.h>
  33. #endif
  34. #include "../DebugNew.h"
  35. namespace Atomic
  36. {
  37. static const unsigned PIPE_BUFFER_SIZE = 65536;
  38. NamedPipe::NamedPipe(Context* context) :
  39. Object(context),
  40. isServer_(false),
  41. #ifdef _WIN32
  42. handle_(INVALID_HANDLE_VALUE)
  43. #else
  44. readHandle_(-1),
  45. writeHandle_(-1)
  46. #endif
  47. {
  48. }
  49. NamedPipe::NamedPipe(Context* context, const String& pipeName, bool isServer) :
  50. Object(context),
  51. isServer_(false),
  52. #ifdef _WIN32
  53. handle_(INVALID_HANDLE_VALUE)
  54. #else
  55. readHandle_(-1),
  56. writeHandle_(-1)
  57. #endif
  58. {
  59. Open(pipeName, isServer);
  60. }
  61. NamedPipe::~NamedPipe()
  62. {
  63. Close();
  64. }
  65. unsigned NamedPipe::Seek(unsigned position)
  66. {
  67. return 0;
  68. }
  69. #ifdef _WIN32
  70. static const String pipePath("\\\\.\\pipe\\");
  71. bool NamedPipe::Open(const String& pipeName, bool isServer)
  72. {
  73. ATOMIC_PROFILE(OpenNamedPipe);
  74. Close();
  75. isServer_ = false;
  76. if (isServer)
  77. {
  78. handle_ = CreateNamedPipeW(WString(pipePath + pipeName).CString(),
  79. PIPE_ACCESS_DUPLEX,
  80. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT,
  81. 1,
  82. PIPE_BUFFER_SIZE,
  83. PIPE_BUFFER_SIZE,
  84. 0,
  85. 0
  86. );
  87. if (handle_ == INVALID_HANDLE_VALUE)
  88. {
  89. ATOMIC_LOGERROR("Failed to create named pipe " + pipeName);
  90. return false;
  91. }
  92. else
  93. {
  94. ATOMIC_LOGDEBUG("Created named pipe " + pipeName);
  95. pipeName_ = pipeName;
  96. isServer_ = true;
  97. return true;
  98. }
  99. }
  100. else
  101. {
  102. handle_ = CreateFileW(
  103. WString(pipePath + pipeName).CString(),
  104. GENERIC_READ | GENERIC_WRITE,
  105. 0,
  106. 0,
  107. OPEN_EXISTING,
  108. 0,
  109. 0
  110. );
  111. if (handle_ == INVALID_HANDLE_VALUE)
  112. {
  113. ATOMIC_LOGERROR("Failed to connect to named pipe " + pipeName);
  114. return false;
  115. }
  116. else
  117. {
  118. ATOMIC_LOGDEBUG("Connected to named pipe " + pipeName);
  119. pipeName_ = pipeName;
  120. return true;
  121. }
  122. }
  123. }
  124. unsigned NamedPipe::Read(void* dest, unsigned size)
  125. {
  126. if (handle_ != INVALID_HANDLE_VALUE)
  127. {
  128. DWORD read = 0;
  129. ReadFile(handle_, dest, size, &read, 0);
  130. return read;
  131. }
  132. return 0;
  133. }
  134. unsigned NamedPipe::Write(const void* data, unsigned size)
  135. {
  136. if (handle_ != INVALID_HANDLE_VALUE)
  137. {
  138. DWORD written = 0;
  139. WriteFile(handle_, data, size, &written, 0);
  140. return written;
  141. }
  142. return 0;
  143. }
  144. void NamedPipe::Close()
  145. {
  146. if (handle_ != INVALID_HANDLE_VALUE)
  147. {
  148. ATOMIC_PROFILE(CloseNamedPipe);
  149. if (isServer_)
  150. {
  151. DisconnectNamedPipe(handle_);
  152. isServer_ = false;
  153. }
  154. CloseHandle(handle_);
  155. handle_ = INVALID_HANDLE_VALUE;
  156. pipeName_.Clear();
  157. ATOMIC_LOGDEBUG("Closed named pipe " + pipeName_);
  158. }
  159. }
  160. bool NamedPipe::IsOpen() const
  161. {
  162. return handle_ != INVALID_HANDLE_VALUE;
  163. }
  164. bool NamedPipe::IsEof() const
  165. {
  166. if (handle_ != INVALID_HANDLE_VALUE)
  167. {
  168. DWORD bytesAvailable = 0;
  169. PeekNamedPipe(handle_, 0, 0, 0, &bytesAvailable, 0);
  170. return bytesAvailable == 0;
  171. }
  172. else
  173. return true;
  174. }
  175. #else
  176. static const String pipePath("/tmp/");
  177. #define SAFE_CLOSE(handle) if (handle != -1) { close(handle); handle = -1; }
  178. bool NamedPipe::Open(const String& pipeName, bool isServer)
  179. {
  180. #ifdef __EMSCRIPTEN__
  181. ATOMIC_LOGERROR("Opening a named pipe not supported on Web platform");
  182. return false;
  183. #else
  184. ATOMIC_PROFILE(OpenNamedPipe);
  185. Close();
  186. isServer_ = false;
  187. String serverReadName = pipePath + pipeName + "SR";
  188. String clientReadName = pipePath + pipeName + "CR";
  189. // Make sure SIGPIPE is ignored and will not lead to process termination
  190. signal(SIGPIPE, SIG_IGN);
  191. if (isServer)
  192. {
  193. mkfifo(serverReadName.CString(), 0660);
  194. mkfifo(clientReadName.CString(), 0660);
  195. readHandle_ = open(serverReadName.CString(), O_RDONLY | O_NDELAY);
  196. writeHandle_ = open(clientReadName.CString(), O_WRONLY | O_NDELAY);
  197. if (readHandle_ == -1 && writeHandle_ == -1)
  198. {
  199. ATOMIC_LOGERROR("Failed to create named pipe " + pipeName);
  200. SAFE_CLOSE(readHandle_);
  201. SAFE_CLOSE(writeHandle_);
  202. unlink(serverReadName.CString());
  203. unlink(clientReadName.CString());
  204. return false;
  205. }
  206. else
  207. {
  208. ATOMIC_LOGDEBUG("Created named pipe " + pipeName);
  209. pipeName_ = pipeName;
  210. isServer_ = true;
  211. return true;
  212. }
  213. }
  214. else
  215. {
  216. readHandle_ = open(clientReadName.CString(), O_RDONLY | O_NDELAY);
  217. writeHandle_ = open(serverReadName.CString(), O_WRONLY | O_NDELAY);
  218. if (readHandle_ == -1 && writeHandle_ == -1)
  219. {
  220. ATOMIC_LOGERROR("Failed to connect to named pipe " + pipeName);
  221. SAFE_CLOSE(readHandle_);
  222. SAFE_CLOSE(writeHandle_);
  223. return false;
  224. }
  225. else
  226. {
  227. ATOMIC_LOGDEBUG("Connected to named pipe " + pipeName);
  228. pipeName_ = pipeName;
  229. return true;
  230. }
  231. }
  232. #endif
  233. }
  234. unsigned NamedPipe::Read(void* dest, unsigned size)
  235. {
  236. // Attempt to open late if only the write handle is open yet
  237. if (readHandle_ == -1 && writeHandle_ != -1)
  238. {
  239. if (isServer_)
  240. readHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_RDONLY | O_NDELAY);
  241. else
  242. readHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_RDONLY | O_NDELAY);
  243. }
  244. if (readHandle_ != -1)
  245. {
  246. ssize_t readNow = read(readHandle_, dest, size);
  247. return readNow < 0 ? 0 : (unsigned)readNow;
  248. }
  249. else
  250. return 0;
  251. }
  252. unsigned NamedPipe::Write(const void* data, unsigned size)
  253. {
  254. // Attempt to open late if only the read handle is open yet
  255. if (writeHandle_ == -1 && readHandle_ != -1)
  256. {
  257. if (isServer_)
  258. writeHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_WRONLY | O_NDELAY);
  259. else
  260. writeHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_WRONLY | O_NDELAY);
  261. }
  262. // Loop until all bytes written in case of partial write
  263. if (writeHandle_ != -1)
  264. {
  265. ssize_t written = 0;
  266. while (written < (ssize_t)size)
  267. {
  268. ssize_t writtenNow = write(writeHandle_, ((const unsigned char*)data) + written, size - written);
  269. if (writtenNow < 0)
  270. return 0; // Error while writing
  271. written += writtenNow;
  272. }
  273. return (unsigned)written;
  274. }
  275. else
  276. return 0;
  277. }
  278. void NamedPipe::Close()
  279. {
  280. if (readHandle_ != -1 || writeHandle_ != -1)
  281. {
  282. ATOMIC_PROFILE(CloseNamedPipe);
  283. SAFE_CLOSE(readHandle_);
  284. SAFE_CLOSE(writeHandle_);
  285. if (isServer_)
  286. {
  287. String serverReadName = pipePath + pipeName_ + "SR";
  288. String clientReadName = pipePath + pipeName_ + "CR";
  289. unlink(serverReadName.CString());
  290. unlink(clientReadName.CString());
  291. isServer_ = false;
  292. }
  293. pipeName_.Clear();
  294. }
  295. }
  296. bool NamedPipe::IsOpen() const
  297. {
  298. return readHandle_ != -1 || writeHandle_ != -1;
  299. }
  300. bool NamedPipe::IsEof() const
  301. {
  302. #ifdef __EMSCRIPTEN__
  303. return true;
  304. #else
  305. // Attempt to open late if only the write handle is open yet
  306. if (readHandle_ == -1 && writeHandle_ != -1)
  307. {
  308. if (isServer_)
  309. readHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_RDONLY | O_NDELAY);
  310. else
  311. readHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_RDONLY | O_NDELAY);
  312. }
  313. if (readHandle_ != -1)
  314. {
  315. fd_set set;
  316. FD_ZERO(&set);
  317. FD_SET(readHandle_, &set);
  318. struct timeval timeout;
  319. timeout.tv_sec = 0;
  320. timeout.tv_usec = 1000; // 1ms timeout for select
  321. return select(readHandle_ + 1, &set, 0, 0, &timeout) <= 0;
  322. }
  323. else
  324. return true;
  325. #endif
  326. }
  327. #endif
  328. }