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.

220 lines
6.1KB

  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. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #if JUCE_INCLUDED_FILE
  26. //==============================================================================
  27. struct NamedPipeInternal
  28. {
  29. String pipeInName, pipeOutName;
  30. int pipeIn, pipeOut;
  31. bool volatile createdPipe, blocked, stopReadOperation;
  32. static void signalHandler (int) {}
  33. };
  34. void NamedPipe::cancelPendingReads()
  35. {
  36. while (internal != 0 && ((NamedPipeInternal*) internal)->blocked)
  37. {
  38. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  39. intern->stopReadOperation = true;
  40. char buffer [1] = { 0 };
  41. int bytesWritten = ::write (intern->pipeIn, buffer, 1);
  42. (void) bytesWritten;
  43. int timeout = 2000;
  44. while (intern->blocked && --timeout >= 0)
  45. Thread::sleep (2);
  46. intern->stopReadOperation = false;
  47. }
  48. }
  49. void NamedPipe::close()
  50. {
  51. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  52. if (intern != 0)
  53. {
  54. internal = 0;
  55. if (intern->pipeIn != -1)
  56. ::close (intern->pipeIn);
  57. if (intern->pipeOut != -1)
  58. ::close (intern->pipeOut);
  59. if (intern->createdPipe)
  60. {
  61. unlink (intern->pipeInName);
  62. unlink (intern->pipeOutName);
  63. }
  64. delete intern;
  65. }
  66. }
  67. bool NamedPipe::openInternal (const String& pipeName, const bool createPipe)
  68. {
  69. close();
  70. NamedPipeInternal* const intern = new NamedPipeInternal();
  71. internal = intern;
  72. intern->createdPipe = createPipe;
  73. intern->blocked = false;
  74. intern->stopReadOperation = false;
  75. signal (SIGPIPE, NamedPipeInternal::signalHandler);
  76. siginterrupt (SIGPIPE, 1);
  77. const String pipePath (T("/tmp/") + File::createLegalFileName (pipeName));
  78. intern->pipeInName = pipePath + T("_in");
  79. intern->pipeOutName = pipePath + T("_out");
  80. intern->pipeIn = -1;
  81. intern->pipeOut = -1;
  82. if (createPipe)
  83. {
  84. if ((mkfifo (intern->pipeInName, 0666) && errno != EEXIST)
  85. || (mkfifo (intern->pipeOutName, 0666) && errno != EEXIST))
  86. {
  87. delete intern;
  88. internal = 0;
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. int NamedPipe::read (void* destBuffer, int maxBytesToRead, int /*timeOutMilliseconds*/)
  95. {
  96. int bytesRead = -1;
  97. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  98. if (intern != 0)
  99. {
  100. intern->blocked = true;
  101. if (intern->pipeIn == -1)
  102. {
  103. if (intern->createdPipe)
  104. intern->pipeIn = ::open (intern->pipeInName, O_RDWR);
  105. else
  106. intern->pipeIn = ::open (intern->pipeOutName, O_RDWR);
  107. if (intern->pipeIn == -1)
  108. {
  109. intern->blocked = false;
  110. return -1;
  111. }
  112. }
  113. bytesRead = 0;
  114. char* p = (char*) destBuffer;
  115. while (bytesRead < maxBytesToRead)
  116. {
  117. const int bytesThisTime = maxBytesToRead - bytesRead;
  118. const int numRead = ::read (intern->pipeIn, p, bytesThisTime);
  119. if (numRead <= 0 || intern->stopReadOperation)
  120. {
  121. bytesRead = -1;
  122. break;
  123. }
  124. bytesRead += numRead;
  125. p += bytesRead;
  126. }
  127. intern->blocked = false;
  128. }
  129. return bytesRead;
  130. }
  131. int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
  132. {
  133. int bytesWritten = -1;
  134. NamedPipeInternal* const intern = (NamedPipeInternal*) internal;
  135. if (intern != 0)
  136. {
  137. if (intern->pipeOut == -1)
  138. {
  139. if (intern->createdPipe)
  140. intern->pipeOut = ::open (intern->pipeOutName, O_WRONLY);
  141. else
  142. intern->pipeOut = ::open (intern->pipeInName, O_WRONLY);
  143. if (intern->pipeOut == -1)
  144. {
  145. return -1;
  146. }
  147. }
  148. const char* p = (const char*) sourceBuffer;
  149. bytesWritten = 0;
  150. const uint32 timeOutTime = Time::getMillisecondCounter() + timeOutMilliseconds;
  151. while (bytesWritten < numBytesToWrite
  152. && (timeOutMilliseconds < 0 || Time::getMillisecondCounter() < timeOutTime))
  153. {
  154. const int bytesThisTime = numBytesToWrite - bytesWritten;
  155. const int numWritten = ::write (intern->pipeOut, p, bytesThisTime);
  156. if (numWritten <= 0)
  157. {
  158. bytesWritten = -1;
  159. break;
  160. }
  161. bytesWritten += numWritten;
  162. p += bytesWritten;
  163. }
  164. }
  165. return bytesWritten;
  166. }
  167. #endif