The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  24. #include <sys/stat.h>
  25. #include <sys/dir.h>
  26. #include <fcntl.h>
  27. // As well as being for the mac, this file is included by the linux build.
  28. #if JUCE_MAC
  29. #include <Carbon/Carbon.h>
  30. #else
  31. #include <sys/wait.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #endif
  35. BEGIN_JUCE_NAMESPACE
  36. #include "../../../src/juce_core/io/files/juce_File.h"
  37. #include "../../../src/juce_core/io/files/juce_NamedPipe.h"
  38. #include "../../../src/juce_core/threads/juce_Thread.h"
  39. //==============================================================================
  40. struct NamedPipeInternal
  41. {
  42. String pipeInName, pipeOutName;
  43. int pipeIn, pipeOut;
  44. bool volatile createdPipe, blocked, stopReadOperation;
  45. static void signalHandler (int) {}
  46. };
  47. void NamedPipe::cancelPendingReads()
  48. {
  49. while (internal != 0 && ((NamedPipeInternal*) internal)->blocked)
  50. {
  51. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  52. intern->stopReadOperation = true;
  53. char buffer [1] = { 0 };
  54. ::write (intern->pipeIn, buffer, 1);
  55. int timeout = 2000;
  56. while (intern->blocked && --timeout >= 0)
  57. Thread::sleep (2);
  58. intern->stopReadOperation = false;
  59. }
  60. }
  61. void NamedPipe::close()
  62. {
  63. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  64. if (intern != 0)
  65. {
  66. internal = 0;
  67. if (intern->pipeIn != -1)
  68. ::close (intern->pipeIn);
  69. if (intern->pipeOut != -1)
  70. ::close (intern->pipeOut);
  71. if (intern->createdPipe)
  72. {
  73. unlink (intern->pipeInName);
  74. unlink (intern->pipeOutName);
  75. }
  76. delete intern;
  77. }
  78. }
  79. bool NamedPipe::openInternal (const String& pipeName, const bool createPipe)
  80. {
  81. close();
  82. NamedPipeInternal* const intern = new NamedPipeInternal();
  83. internal = intern;
  84. intern->createdPipe = createPipe;
  85. intern->blocked = false;
  86. intern->stopReadOperation = false;
  87. signal (SIGPIPE, NamedPipeInternal::signalHandler);
  88. siginterrupt (SIGPIPE, 1);
  89. const String pipePath (T("/tmp/") + File::createLegalFileName (pipeName));
  90. intern->pipeInName = pipePath + T("_in");
  91. intern->pipeOutName = pipePath + T("_out");
  92. intern->pipeIn = -1;
  93. intern->pipeOut = -1;
  94. if (createPipe)
  95. {
  96. if ((mkfifo (intern->pipeInName, 0666) && errno != EEXIST)
  97. || (mkfifo (intern->pipeOutName, 0666) && errno != EEXIST))
  98. {
  99. delete intern;
  100. internal = 0;
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. int NamedPipe::read (void* destBuffer, int maxBytesToRead, int /*timeOutMilliseconds*/)
  107. {
  108. int bytesRead = -1;
  109. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  110. if (intern != 0)
  111. {
  112. intern->blocked = true;
  113. if (intern->pipeIn == -1)
  114. {
  115. if (intern->createdPipe)
  116. intern->pipeIn = ::open (intern->pipeInName, O_RDWR);
  117. else
  118. intern->pipeIn = ::open (intern->pipeOutName, O_RDWR);
  119. if (intern->pipeIn == -1)
  120. {
  121. intern->blocked = false;
  122. return -1;
  123. }
  124. }
  125. bytesRead = 0;
  126. char* p = (char*) destBuffer;
  127. while (bytesRead < maxBytesToRead)
  128. {
  129. const int bytesThisTime = maxBytesToRead - bytesRead;
  130. const int numRead = ::read (intern->pipeIn, p, bytesThisTime);
  131. if (numRead <= 0 || intern->stopReadOperation)
  132. {
  133. bytesRead = -1;
  134. break;
  135. }
  136. bytesRead += numRead;
  137. p += bytesRead;
  138. }
  139. intern->blocked = false;
  140. }
  141. return bytesRead;
  142. }
  143. int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
  144. {
  145. int bytesWritten = -1;
  146. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  147. if (intern != 0)
  148. {
  149. if (intern->pipeOut == -1)
  150. {
  151. if (intern->createdPipe)
  152. intern->pipeOut = ::open (intern->pipeOutName, O_WRONLY);
  153. else
  154. intern->pipeOut = ::open (intern->pipeInName, O_WRONLY);
  155. if (intern->pipeOut == -1)
  156. {
  157. return -1;
  158. }
  159. }
  160. const char* p = (const char*) sourceBuffer;
  161. bytesWritten = 0;
  162. const uint32 timeOutTime = Time::getMillisecondCounter() + timeOutMilliseconds;
  163. while (bytesWritten < numBytesToWrite
  164. && (timeOutMilliseconds < 0 || Time::getMillisecondCounter() < timeOutTime))
  165. {
  166. const int bytesThisTime = numBytesToWrite - bytesWritten;
  167. const int numWritten = ::write (intern->pipeOut, p, bytesThisTime);
  168. if (numWritten <= 0)
  169. {
  170. bytesWritten = -1;
  171. break;
  172. }
  173. bytesWritten += numWritten;
  174. p += bytesWritten;
  175. }
  176. }
  177. return bytesWritten;
  178. }
  179. END_JUCE_NAMESPACE