Audio plugin host https://kx.studio/carla
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.

1517 lines
41KB

  1. /*
  2. * Carla Utility Tests
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPipeUtils.hpp"
  18. #include "CarlaString.hpp"
  19. #include "CarlaMIDI.h"
  20. // needed for atom-util
  21. #undef NULL
  22. #define NULL nullptr
  23. #include "juce_core.h"
  24. #include "lv2/atom-util.h"
  25. #include <clocale>
  26. #ifdef CARLA_OS_WIN
  27. # include <ctime>
  28. #else
  29. # include <cerrno>
  30. # include <fcntl.h>
  31. # include <signal.h>
  32. # include <sys/wait.h>
  33. #endif
  34. #ifdef CARLA_OS_WIN
  35. # define INVALID_PIPE_VALUE INVALID_HANDLE_VALUE
  36. #else
  37. # define INVALID_PIPE_VALUE -1
  38. #endif
  39. #ifdef CARLA_OS_WIN
  40. // -----------------------------------------------------------------------
  41. // win32 stuff
  42. struct OverlappedEvent {
  43. OVERLAPPED over;
  44. OverlappedEvent()
  45. : over()
  46. {
  47. carla_zeroStruct(over);
  48. over.hEvent = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
  49. }
  50. ~OverlappedEvent()
  51. {
  52. ::CloseHandle(over.hEvent);
  53. }
  54. CARLA_DECLARE_NON_COPY_STRUCT(OverlappedEvent)
  55. };
  56. static inline
  57. ssize_t ReadFileNonBlock(const HANDLE pipeh, const HANDLE cancelh, void* const buf, const std::size_t numBytes)
  58. {
  59. DWORD dsize;
  60. OverlappedEvent over;
  61. if (::ReadFile(pipeh, buf, numBytes, &dsize, &over.over) != FALSE)
  62. return static_cast<ssize_t>(dsize);
  63. if (::GetLastError() == ERROR_IO_PENDING)
  64. {
  65. HANDLE handles[] = { over.over.hEvent, cancelh };
  66. if (::WaitForMultipleObjects(2, handles, FALSE, 0) != WAIT_OBJECT_0)
  67. {
  68. ::CancelIo(pipeh);
  69. return -1;
  70. }
  71. if (::GetOverlappedResult(pipeh, &over.over, &dsize, FALSE) != FALSE)
  72. return static_cast<ssize_t>(dsize);
  73. }
  74. return -1;
  75. }
  76. static inline
  77. ssize_t WriteFileNonBlock(const HANDLE pipeh, const HANDLE cancelh, const void* const buf, const std::size_t numBytes)
  78. {
  79. DWORD dsize;
  80. if (::WriteFile(pipeh, buf, numBytes, &dsize, nullptr) != FALSE)
  81. return static_cast<ssize_t>(dsize);
  82. return -1;
  83. //DWORD dsize;
  84. OverlappedEvent over;
  85. if (::WriteFile(pipeh, buf, numBytes, &dsize, &over.over) != FALSE)
  86. return static_cast<ssize_t>(dsize);
  87. if (::GetLastError() == ERROR_IO_PENDING)
  88. {
  89. HANDLE handles[] = { over.over.hEvent, cancelh };
  90. if (::WaitForMultipleObjects(2, handles, FALSE, 0) != WAIT_OBJECT_0)
  91. {
  92. ::CancelIo(pipeh);
  93. return -1;
  94. }
  95. if (::GetOverlappedResult(pipeh, &over.over, &dsize, FALSE) != FALSE)
  96. return static_cast<ssize_t>(dsize);
  97. }
  98. return -1;
  99. }
  100. #endif // CARLA_OS_WIN
  101. // -----------------------------------------------------------------------
  102. // startProcess
  103. #ifdef CARLA_OS_WIN
  104. static inline
  105. bool startProcess(const char* const argv[], PROCESS_INFORMATION& processInfo)
  106. {
  107. using juce::String;
  108. String command;
  109. for (int i=0; argv[i] != nullptr; ++i)
  110. {
  111. String arg(argv[i]);
  112. // If there are spaces, surround it with quotes. If there are quotes,
  113. // replace them with \" so that CommandLineToArgv will correctly parse them.
  114. if (arg.containsAnyOf("\" "))
  115. arg = arg.replace("\"", "\\\"").quoted();
  116. command << arg << ' ';
  117. }
  118. command = command.trim();
  119. STARTUPINFOW startupInfo;
  120. carla_zeroStruct(startupInfo);
  121. # if 0
  122. startupInfo.hStdInput = INVALID_HANDLE_VALUE;
  123. startupInfo.hStdOutput = INVALID_HANDLE_VALUE;
  124. startupInfo.hStdError = INVALID_HANDLE_VALUE;
  125. startupInfo.dwFlags = STARTF_USESTDHANDLES;
  126. # endif
  127. startupInfo.cb = sizeof(STARTUPINFOW);
  128. return CreateProcessW(nullptr, const_cast<LPWSTR>(command.toWideCharPointer()),
  129. nullptr, nullptr, TRUE, 0x0, nullptr, nullptr, &startupInfo, &processInfo) != FALSE;
  130. }
  131. #else
  132. static inline
  133. bool startProcess(const char* const argv[], pid_t& pidinst) noexcept
  134. {
  135. const pid_t ret = pidinst = vfork();
  136. switch (ret)
  137. {
  138. case 0: { // child process
  139. execvp(argv[0], const_cast<char* const*>(argv));
  140. CarlaString error(std::strerror(errno));
  141. carla_stderr2("exec failed: %s", error.buffer());
  142. _exit(0); // this is not noexcept safe but doesn't matter anyway
  143. } break;
  144. case -1: { // error
  145. CarlaString error(std::strerror(errno));
  146. carla_stderr2("fork() failed: %s", error.buffer());
  147. _exit(0); // this is not noexcept safe but doesn't matter anyway
  148. } break;
  149. }
  150. return (ret > 0);
  151. }
  152. #endif
  153. // -----------------------------------------------------------------------
  154. // waitForClientFirstMessage
  155. template<typename P>
  156. static inline
  157. bool waitForClientFirstMessage(const P& pipe, const uint32_t timeOutMilliseconds) noexcept
  158. {
  159. #ifdef CARLA_OS_WIN
  160. CARLA_SAFE_ASSERT_RETURN(pipe.handle != INVALID_HANDLE_VALUE, false);
  161. CARLA_SAFE_ASSERT_RETURN(pipe.cancel != INVALID_HANDLE_VALUE, false);
  162. #else
  163. CARLA_SAFE_ASSERT_RETURN(pipe > 0, false);
  164. #endif
  165. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  166. char c;
  167. ssize_t ret;
  168. const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds);
  169. for (;;)
  170. {
  171. try {
  172. #ifdef CARLA_OS_WIN
  173. ret = ::ReadFileNonBlock(pipe.handle, pipe.cancel, &c, 1);
  174. #else
  175. ret = ::read(pipe, &c, 1);
  176. #endif
  177. } CARLA_SAFE_EXCEPTION_BREAK("read pipefd");
  178. switch (ret)
  179. {
  180. case -1: // failed to read
  181. #ifndef CARLA_OS_WIN
  182. if (errno == EAGAIN)
  183. #endif
  184. {
  185. if (juce::Time::getMillisecondCounter() < timeoutEnd)
  186. {
  187. carla_msleep(5);
  188. continue;
  189. }
  190. carla_stderr("waitForClientFirstMessage() - timed out");
  191. }
  192. #ifndef CARLA_OS_WIN
  193. else
  194. {
  195. carla_stderr("waitForClientFirstMessage() - read failed");
  196. CarlaString error(std::strerror(errno));
  197. carla_stderr("waitForClientFirstMessage() - read failed: %s", error.buffer());
  198. }
  199. #endif
  200. break;
  201. case 1: // read ok
  202. if (c == '\n')
  203. {
  204. // success
  205. return true;
  206. }
  207. else
  208. {
  209. carla_stderr("waitForClientFirstMessage() - read has wrong first char '%c'", c);
  210. }
  211. break;
  212. default: // ???
  213. carla_stderr("waitForClientFirstMessage() - read returned %i", int(ret));
  214. break;
  215. }
  216. break;
  217. }
  218. return false;
  219. }
  220. // -----------------------------------------------------------------------
  221. // waitForChildToStop / waitForProcessToStop
  222. #ifdef CARLA_OS_WIN
  223. static inline
  224. bool waitForProcessToStop(const PROCESS_INFORMATION& processInfo, const uint32_t timeOutMilliseconds) noexcept
  225. {
  226. CARLA_SAFE_ASSERT_RETURN(processInfo.hProcess != INVALID_HANDLE_VALUE, false);
  227. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  228. // TODO - this code is completly wrong...
  229. const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds);
  230. for (;;)
  231. {
  232. if (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_OBJECT_0)
  233. return true;
  234. if (juce::Time::getMillisecondCounter() >= timeoutEnd)
  235. break;
  236. carla_msleep(5);
  237. }
  238. return false;
  239. }
  240. static inline
  241. void waitForProcessToStopOrKillIt(const PROCESS_INFORMATION& processInfo, const uint32_t timeOutMilliseconds) noexcept
  242. {
  243. CARLA_SAFE_ASSERT_RETURN(processInfo.hProcess != INVALID_HANDLE_VALUE,);
  244. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0,);
  245. if (! waitForProcessToStop(processInfo, timeOutMilliseconds))
  246. {
  247. carla_stderr("waitForProcessToStopOrKillIt() - process didn't stop, force termination");
  248. if (TerminateProcess(processInfo.hProcess, 0) != FALSE)
  249. {
  250. // wait for process to stop
  251. waitForProcessToStop(processInfo, timeOutMilliseconds);
  252. }
  253. }
  254. }
  255. #else
  256. static inline
  257. bool waitForChildToStop(const pid_t pid, const uint32_t timeOutMilliseconds) noexcept
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(pid > 0, false);
  260. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  261. pid_t ret;
  262. const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds);
  263. for (;;)
  264. {
  265. try {
  266. ret = ::waitpid(pid, nullptr, WNOHANG);
  267. } CARLA_SAFE_EXCEPTION_BREAK("waitpid");
  268. switch (ret)
  269. {
  270. case -1:
  271. if (errno == ECHILD)
  272. {
  273. // success, child doesn't exist
  274. return true;
  275. }
  276. else
  277. {
  278. CarlaString error(std::strerror(errno));
  279. carla_stderr("waitForChildToStop() - waitpid failed: %s", error.buffer());
  280. return false;
  281. }
  282. break;
  283. case 0:
  284. if (juce::Time::getMillisecondCounter() < timeoutEnd)
  285. {
  286. carla_msleep(5);
  287. continue;
  288. }
  289. carla_stderr("waitForChildToStop() - timed out");
  290. break;
  291. default:
  292. if (ret == pid)
  293. {
  294. // success
  295. return true;
  296. }
  297. else
  298. {
  299. carla_stderr("waitForChildToStop() - got wrong pid %i (requested was %i)", int(ret), int(pid));
  300. return false;
  301. }
  302. }
  303. break;
  304. }
  305. return false;
  306. }
  307. static inline
  308. void waitForChildToStopOrKillIt(pid_t& pid, const uint32_t timeOutMilliseconds) noexcept
  309. {
  310. CARLA_SAFE_ASSERT_RETURN(pid > 0,);
  311. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0,);
  312. if (! waitForChildToStop(pid, timeOutMilliseconds))
  313. {
  314. carla_stderr("waitForChildToStopOrKillIt() - process didn't stop, force killing");
  315. if (::kill(pid, SIGKILL) != -1)
  316. {
  317. // wait for killing to take place
  318. waitForChildToStop(pid, timeOutMilliseconds);
  319. }
  320. else
  321. {
  322. CarlaString error(std::strerror(errno));
  323. carla_stderr("waitForChildToStopOrKillIt() - kill failed: %s", error.buffer());
  324. }
  325. }
  326. }
  327. #endif
  328. // -----------------------------------------------------------------------
  329. struct CarlaPipeCommon::PrivateData {
  330. // pipes
  331. #ifdef CARLA_OS_WIN
  332. PROCESS_INFORMATION processInfo;
  333. HANDLE cancelEvent;
  334. HANDLE pipeRecv;
  335. HANDLE pipeSend;
  336. #else
  337. pid_t pid;
  338. int pipeRecv;
  339. int pipeSend;
  340. #endif
  341. // read functions must only be called in context of idlePipe()
  342. bool isReading;
  343. // common write lock
  344. CarlaMutex writeLock;
  345. // temporary buffers for _readline()
  346. mutable char tmpBuf[0xff+1];
  347. mutable CarlaString tmpStr;
  348. PrivateData() noexcept
  349. #ifdef CARLA_OS_WIN
  350. : processInfo(),
  351. cancelEvent(INVALID_HANDLE_VALUE),
  352. #else
  353. : pid(-1),
  354. #endif
  355. pipeRecv(INVALID_PIPE_VALUE),
  356. pipeSend(INVALID_PIPE_VALUE),
  357. isReading(false),
  358. writeLock(),
  359. tmpBuf(),
  360. tmpStr()
  361. {
  362. #ifdef CARLA_OS_WIN
  363. carla_zeroStruct(processInfo);
  364. try {
  365. cancelEvent = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
  366. } CARLA_SAFE_EXCEPTION("CreateEvent");
  367. #endif
  368. carla_zeroChar(tmpBuf, 0xff+1);
  369. }
  370. ~PrivateData() noexcept
  371. {
  372. #ifdef CARLA_OS_WIN
  373. if (cancelEvent != INVALID_HANDLE_VALUE)
  374. {
  375. ::CloseHandle(cancelEvent);
  376. cancelEvent = INVALID_HANDLE_VALUE;
  377. }
  378. #endif
  379. }
  380. CARLA_DECLARE_NON_COPY_STRUCT(PrivateData)
  381. };
  382. // -----------------------------------------------------------------------
  383. CarlaPipeCommon::CarlaPipeCommon() noexcept
  384. : pData(new PrivateData()),
  385. leakDetector_CarlaPipeCommon()
  386. {
  387. carla_debug("CarlaPipeCommon::CarlaPipeCommon()");
  388. }
  389. CarlaPipeCommon::~CarlaPipeCommon() noexcept
  390. {
  391. carla_debug("CarlaPipeCommon::~CarlaPipeCommon()");
  392. delete pData;
  393. }
  394. // -------------------------------------------------------------------
  395. bool CarlaPipeCommon::isPipeRunning() const noexcept
  396. {
  397. return (pData->pipeRecv != INVALID_PIPE_VALUE && pData->pipeSend != INVALID_PIPE_VALUE);
  398. }
  399. void CarlaPipeCommon::idlePipe() noexcept
  400. {
  401. const char* locale = nullptr;
  402. for (;;)
  403. {
  404. const char* const msg(_readline());
  405. if (msg == nullptr)
  406. break;
  407. if (locale == nullptr)
  408. {
  409. locale = carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr));
  410. ::setlocale(LC_NUMERIC, "C");
  411. }
  412. pData->isReading = true;
  413. try {
  414. msgReceived(msg);
  415. } CARLA_SAFE_EXCEPTION("msgReceived");
  416. pData->isReading = false;
  417. delete[] msg;
  418. }
  419. if (locale != nullptr)
  420. {
  421. ::setlocale(LC_NUMERIC, locale);
  422. delete[] locale;
  423. }
  424. }
  425. // -------------------------------------------------------------------
  426. void CarlaPipeCommon::lockPipe() const noexcept
  427. {
  428. pData->writeLock.lock();
  429. }
  430. bool CarlaPipeCommon::tryLockPipe() const noexcept
  431. {
  432. return pData->writeLock.tryLock();
  433. }
  434. void CarlaPipeCommon::unlockPipe() const noexcept
  435. {
  436. pData->writeLock.unlock();
  437. }
  438. CarlaMutex& CarlaPipeCommon::getPipeLock() const noexcept
  439. {
  440. return pData->writeLock;
  441. }
  442. // -------------------------------------------------------------------
  443. bool CarlaPipeCommon::readNextLineAsBool(bool& value) const noexcept
  444. {
  445. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  446. if (const char* const msg = _readlineblock())
  447. {
  448. value = (std::strcmp(msg, "true") == 0);
  449. delete[] msg;
  450. return true;
  451. }
  452. return false;
  453. }
  454. bool CarlaPipeCommon::readNextLineAsByte(uint8_t& value) const noexcept
  455. {
  456. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  457. if (const char* const msg = _readlineblock())
  458. {
  459. int tmp = std::atoi(msg);
  460. delete[] msg;
  461. if (tmp >= 0 && tmp <= 0xFF)
  462. {
  463. value = static_cast<uint8_t>(tmp);
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. bool CarlaPipeCommon::readNextLineAsInt(int32_t& value) const noexcept
  470. {
  471. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  472. if (const char* const msg = _readlineblock())
  473. {
  474. value = std::atoi(msg);
  475. delete[] msg;
  476. return true;
  477. }
  478. return false;
  479. }
  480. bool CarlaPipeCommon::readNextLineAsUInt(uint32_t& value) const noexcept
  481. {
  482. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  483. if (const char* const msg = _readlineblock())
  484. {
  485. int32_t tmp = std::atoi(msg);
  486. delete[] msg;
  487. if (tmp >= 0)
  488. {
  489. value = static_cast<uint32_t>(tmp);
  490. return true;
  491. }
  492. }
  493. return false;
  494. }
  495. bool CarlaPipeCommon::readNextLineAsLong(int64_t& value) const noexcept
  496. {
  497. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  498. if (const char* const msg = _readlineblock())
  499. {
  500. value = std::atol(msg);
  501. delete[] msg;
  502. return true;
  503. }
  504. return false;
  505. }
  506. bool CarlaPipeCommon::readNextLineAsULong(uint64_t& value) const noexcept
  507. {
  508. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  509. if (const char* const msg = _readlineblock())
  510. {
  511. int64_t tmp = std::atol(msg);
  512. delete[] msg;
  513. if (tmp >= 0)
  514. {
  515. value = static_cast<uint64_t>(tmp);
  516. return true;
  517. }
  518. }
  519. return false;
  520. }
  521. bool CarlaPipeCommon::readNextLineAsFloat(float& value) const noexcept
  522. {
  523. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  524. if (const char* const msg = _readlineblock())
  525. {
  526. value = static_cast<float>(std::atof(msg));
  527. delete[] msg;
  528. return true;
  529. }
  530. return false;
  531. }
  532. bool CarlaPipeCommon::readNextLineAsDouble(double& value) const noexcept
  533. {
  534. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  535. if (const char* const msg = _readlineblock())
  536. {
  537. value = std::atof(msg);
  538. delete[] msg;
  539. return true;
  540. }
  541. return false;
  542. }
  543. bool CarlaPipeCommon::readNextLineAsString(const char*& value) const noexcept
  544. {
  545. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  546. if (const char* const msg = _readlineblock())
  547. {
  548. value = msg;
  549. return true;
  550. }
  551. return false;
  552. }
  553. // -------------------------------------------------------------------
  554. // must be locked before calling
  555. bool CarlaPipeCommon::writeMessage(const char* const msg) const noexcept
  556. {
  557. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  558. const std::size_t size(std::strlen(msg));
  559. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  560. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  561. return _writeMsgBuffer(msg, size);
  562. }
  563. bool CarlaPipeCommon::writeMessage(const char* const msg, std::size_t size) const noexcept
  564. {
  565. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  566. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  567. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  568. return _writeMsgBuffer(msg, size);
  569. }
  570. bool CarlaPipeCommon::writeAndFixMessage(const char* const msg) const noexcept
  571. {
  572. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, false);
  573. const std::size_t size(std::strlen(msg));
  574. char fixedMsg[size+2];
  575. if (size > 0)
  576. {
  577. std::strcpy(fixedMsg, msg);
  578. for (std::size_t i=0; i<size; ++i)
  579. {
  580. if (fixedMsg[i] == '\n')
  581. fixedMsg[i] = '\r';
  582. }
  583. if (fixedMsg[size-1] == '\r')
  584. {
  585. fixedMsg[size-1] = '\n';
  586. fixedMsg[size] = '\0';
  587. fixedMsg[size+1] = '\0';
  588. }
  589. else
  590. {
  591. fixedMsg[size] = '\n';
  592. fixedMsg[size+1] = '\0';
  593. }
  594. }
  595. else
  596. {
  597. fixedMsg[0] = '\n';
  598. fixedMsg[1] = '\0';
  599. }
  600. return _writeMsgBuffer(fixedMsg, size+1);
  601. }
  602. bool CarlaPipeCommon::flushMessages() const noexcept
  603. {
  604. // TESTING remove later (replace with trylock scope)
  605. if (pData->writeLock.tryLock())
  606. {
  607. carla_safe_assert("! pData->writeLock.tryLock()", __FILE__, __LINE__);
  608. pData->writeLock.unlock();
  609. return false;
  610. }
  611. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend != INVALID_PIPE_VALUE, false);
  612. try {
  613. #ifdef CARLA_OS_WIN
  614. return (::FlushFileBuffers(pData->pipeSend) != FALSE);
  615. #else
  616. return (::fsync(pData->pipeSend) == 0);
  617. #endif
  618. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  619. }
  620. // -------------------------------------------------------------------
  621. void CarlaPipeCommon::writeShowMessage() const noexcept
  622. {
  623. const CarlaMutexLocker cml(pData->writeLock);
  624. _writeMsgBuffer("show\n", 5);
  625. flushMessages();
  626. }
  627. void CarlaPipeCommon::writeFocusMessage() const noexcept
  628. {
  629. const CarlaMutexLocker cml(pData->writeLock);
  630. _writeMsgBuffer("focus\n", 6);
  631. flushMessages();
  632. }
  633. void CarlaPipeCommon::writeHideMessage() const noexcept
  634. {
  635. const CarlaMutexLocker cml(pData->writeLock);
  636. _writeMsgBuffer("show\n", 5);
  637. flushMessages();
  638. }
  639. void CarlaPipeCommon::writeErrorMessage(const char* const error) const noexcept
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(error != nullptr && error[0] != '\0',);
  642. const CarlaMutexLocker cml(pData->writeLock);
  643. _writeMsgBuffer("error\n", 6);
  644. writeAndFixMessage(error);
  645. flushMessages();
  646. }
  647. // -------------------------------------------------------------------
  648. void CarlaPipeCommon::writeControlMessage(const uint32_t index, const float value) const noexcept
  649. {
  650. char tmpBuf[0xff+1];
  651. tmpBuf[0xff] = '\0';
  652. const CarlaMutexLocker cml(pData->writeLock);
  653. const ScopedLocale csl;
  654. _writeMsgBuffer("control\n", 8);
  655. {
  656. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  657. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  658. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  659. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  660. }
  661. flushMessages();
  662. }
  663. void CarlaPipeCommon::writeConfigureMessage(const char* const key, const char* const value) const noexcept
  664. {
  665. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  666. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  667. const CarlaMutexLocker cml(pData->writeLock);
  668. _writeMsgBuffer("configure\n", 10);
  669. {
  670. writeAndFixMessage(key);
  671. writeAndFixMessage(value);
  672. }
  673. flushMessages();
  674. }
  675. void CarlaPipeCommon::writeProgramMessage(const uint32_t index) const noexcept
  676. {
  677. char tmpBuf[0xff+1];
  678. tmpBuf[0xff] = '\0';
  679. const CarlaMutexLocker cml(pData->writeLock);
  680. _writeMsgBuffer("program\n", 8);
  681. {
  682. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  683. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  684. }
  685. flushMessages();
  686. }
  687. void CarlaPipeCommon::writeMidiProgramMessage(const uint32_t bank, const uint32_t program) const noexcept
  688. {
  689. char tmpBuf[0xff+1];
  690. tmpBuf[0xff] = '\0';
  691. const CarlaMutexLocker cml(pData->writeLock);
  692. _writeMsgBuffer("midiprogram\n", 8);
  693. {
  694. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  695. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  696. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  697. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  698. }
  699. flushMessages();
  700. }
  701. void CarlaPipeCommon::writeMidiNoteMessage(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) const noexcept
  702. {
  703. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  704. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  705. CARLA_SAFE_ASSERT_RETURN(velocity < MAX_MIDI_VALUE,);
  706. char tmpBuf[0xff+1];
  707. tmpBuf[0xff] = '\0';
  708. const CarlaMutexLocker cml(pData->writeLock);
  709. _writeMsgBuffer("note\n", 5);
  710. {
  711. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(onOff));
  712. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  713. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  714. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  715. std::snprintf(tmpBuf, 0xff, "%i\n", note);
  716. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  717. std::snprintf(tmpBuf, 0xff, "%i\n", velocity);
  718. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  719. }
  720. flushMessages();
  721. }
  722. void CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index, const LV2_Atom* const atom) const noexcept
  723. {
  724. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  725. char tmpBuf[0xff+1];
  726. tmpBuf[0xff] = '\0';
  727. CarlaString base64atom(CarlaString::asBase64(atom, lv2_atom_total_size(atom)));
  728. const CarlaMutexLocker cml(pData->writeLock);
  729. _writeMsgBuffer("atom\n", 5);
  730. {
  731. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  732. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  733. std::snprintf(tmpBuf, 0xff, "%i\n", atom->size);
  734. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  735. writeAndFixMessage(base64atom.buffer());
  736. }
  737. flushMessages();
  738. }
  739. void CarlaPipeCommon::writeLv2UridMessage(const uint32_t urid, const char* const uri) const noexcept
  740. {
  741. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  742. char tmpBuf[0xff+1];
  743. tmpBuf[0xff] = '\0';
  744. const CarlaMutexLocker cml(pData->writeLock);
  745. _writeMsgBuffer("urid\n", 5);
  746. {
  747. std::snprintf(tmpBuf, 0xff, "%i\n", urid);
  748. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  749. writeAndFixMessage(uri);
  750. }
  751. flushMessages();
  752. }
  753. // -------------------------------------------------------------------
  754. // internal
  755. const char* CarlaPipeCommon::_readline() const noexcept
  756. {
  757. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv != INVALID_PIPE_VALUE, nullptr);
  758. char c;
  759. char* ptr = pData->tmpBuf;
  760. ssize_t ret;
  761. pData->tmpStr.clear();
  762. for (int i=0; i < 0xff; ++i)
  763. {
  764. try {
  765. #ifdef CARLA_OS_WIN
  766. ret = ::ReadFileNonBlock(pData->pipeRecv, pData->cancelEvent, &c, 1);
  767. #else
  768. ret = ::read(pData->pipeRecv, &c, 1);
  769. #endif
  770. } CARLA_SAFE_EXCEPTION_BREAK("CarlaPipeCommon::readline() - read");
  771. if (ret == 1 && c != '\n')
  772. {
  773. if (c == '\r')
  774. c = '\n';
  775. *ptr++ = c;
  776. if (i+1 == 0xff)
  777. {
  778. i = 0;
  779. ptr = pData->tmpBuf;
  780. pData->tmpStr += pData->tmpBuf;
  781. }
  782. continue;
  783. }
  784. if (pData->tmpStr.isNotEmpty() || ptr != pData->tmpBuf || ret == 1)
  785. {
  786. if (ptr != pData->tmpBuf)
  787. {
  788. *ptr = '\0';
  789. pData->tmpStr += pData->tmpBuf;
  790. }
  791. try {
  792. return pData->tmpStr.dup();
  793. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - dup", nullptr);
  794. }
  795. break;
  796. }
  797. return nullptr;
  798. }
  799. const char* CarlaPipeCommon::_readlineblock(const uint32_t timeOutMilliseconds) const noexcept
  800. {
  801. const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds);
  802. for (;;)
  803. {
  804. if (const char* const msg = _readline())
  805. return msg;
  806. if (juce::Time::getMillisecondCounter() >= timeoutEnd)
  807. break;
  808. carla_msleep(5);
  809. }
  810. carla_stderr("readlineblock timed out");
  811. return nullptr;
  812. }
  813. bool CarlaPipeCommon::_writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept
  814. {
  815. // TESTING remove later (replace with trylock scope)
  816. if (pData->writeLock.tryLock())
  817. {
  818. carla_safe_assert("! pData->writeLock.tryLock()", __FILE__, __LINE__);
  819. pData->writeLock.unlock();
  820. return false;
  821. }
  822. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend != INVALID_PIPE_VALUE, false);
  823. ssize_t ret;
  824. try {
  825. #ifdef CARLA_OS_WIN
  826. ret = ::WriteFileNonBlock(pData->pipeSend, pData->cancelEvent, msg, size);
  827. #else
  828. ret = ::write(pData->pipeSend, msg, size);
  829. #endif
  830. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  831. return (ret == static_cast<ssize_t>(size));
  832. }
  833. // -----------------------------------------------------------------------
  834. CarlaPipeServer::CarlaPipeServer() noexcept
  835. : CarlaPipeCommon(),
  836. leakDetector_CarlaPipeServer()
  837. {
  838. carla_debug("CarlaPipeServer::CarlaPipeServer()");
  839. }
  840. CarlaPipeServer::~CarlaPipeServer() noexcept
  841. {
  842. carla_debug("CarlaPipeServer::~CarlaPipeServer()");
  843. stopPipeServer(5*1000);
  844. }
  845. // -----------------------------------------------------------------------
  846. bool CarlaPipeServer::startPipeServer(const char* const filename, const char* const arg1, const char* const arg2) noexcept
  847. {
  848. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  849. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  850. #ifdef CARLA_OS_WIN
  851. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hThread == nullptr, false);
  852. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hProcess == nullptr, false);
  853. #else
  854. CARLA_SAFE_ASSERT_RETURN(pData->pid == -1, false);
  855. #endif
  856. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  857. CARLA_SAFE_ASSERT_RETURN(arg1 != nullptr, false);
  858. CARLA_SAFE_ASSERT_RETURN(arg2 != nullptr, false);
  859. carla_debug("CarlaPipeServer::startPipeServer(\"%s\", \"%s\", \"%s\")", filename, arg1, arg2);
  860. const CarlaMutexLocker cml(pData->writeLock);
  861. //----------------------------------------------------------------
  862. // create pipes
  863. #ifdef CARLA_OS_WIN
  864. HANDLE pipe1[2]; // read by server, written by client
  865. HANDLE pipe2[2]; // read by client, written by server
  866. SECURITY_ATTRIBUTES sa;
  867. carla_zeroStruct(sa);
  868. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  869. sa.bInheritHandle = TRUE;
  870. std::srand(static_cast<uint>(std::time(nullptr)));
  871. char strBuf[0xff+1];
  872. strBuf[0xff] = '\0';
  873. static ulong sCounter = 0;
  874. ++sCounter;
  875. const int randint = std::rand();
  876. std::snprintf(strBuf, 0xff, "\\\\.\\pipe\\carla-pipe1-%i-%li", randint, sCounter);
  877. pipe1[0] = ::CreateNamedPipeA(strBuf, PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_WAIT, 1, 4096, 4096, 120*1000, &sa);
  878. pipe1[1] = ::CreateFileA(strBuf, GENERIC_WRITE, 0x0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, nullptr);
  879. std::snprintf(strBuf, 0xff, "\\\\.\\pipe\\carla-pipe2-%i-%li", randint, sCounter);
  880. pipe2[0] = ::CreateNamedPipeA(strBuf, PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_WAIT, 1, 4096, 4096, 120*1000, &sa);
  881. pipe2[1] = ::CreateFileA(strBuf, GENERIC_WRITE, 0x0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, nullptr);
  882. if (pipe1[0] == INVALID_HANDLE_VALUE || pipe1[1] == INVALID_HANDLE_VALUE || pipe2[0] == INVALID_HANDLE_VALUE || pipe2[1] == INVALID_HANDLE_VALUE)
  883. {
  884. if (pipe1[0] != INVALID_HANDLE_VALUE) {
  885. try { ::CloseHandle(pipe1[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[0])");
  886. }
  887. if (pipe1[1] != INVALID_HANDLE_VALUE) {
  888. try { ::CloseHandle(pipe1[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[1])");
  889. }
  890. if (pipe2[0] != INVALID_HANDLE_VALUE) {
  891. try { ::CloseHandle(pipe2[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[0])");
  892. }
  893. if (pipe2[1] != INVALID_HANDLE_VALUE) {
  894. try { ::CloseHandle(pipe2[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[1])");
  895. }
  896. fail("pipe creation failed");
  897. return false;
  898. }
  899. HANDLE pipeRecvServer = pipe1[0];
  900. HANDLE pipeRecvClient = pipe2[0];
  901. HANDLE pipeSendClient = pipe1[1];
  902. HANDLE pipeSendServer = pipe2[1];
  903. #else
  904. int pipe1[2]; // read by server, written by client
  905. int pipe2[2]; // read by client, written by server
  906. if (::pipe(pipe1) != 0)
  907. {
  908. fail("pipe1 creation failed");
  909. return false;
  910. }
  911. if (::pipe(pipe2) != 0)
  912. {
  913. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  914. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  915. fail("pipe2 creation failed");
  916. return false;
  917. }
  918. int pipeRecvServer = pipe1[0];
  919. int pipeRecvClient = pipe2[0];
  920. int pipeSendClient = pipe1[1];
  921. int pipeSendServer = pipe2[1];
  922. #endif
  923. //----------------------------------------------------------------
  924. // set arguments
  925. const char* argv[8];
  926. //----------------------------------------------------------------
  927. // argv[0] => filename
  928. argv[0] = filename;
  929. //----------------------------------------------------------------
  930. // argv[1-2] => args
  931. argv[1] = arg1;
  932. argv[2] = arg2;
  933. //----------------------------------------------------------------
  934. // argv[3-6] => pipes
  935. char pipeRecvServerStr[100+1];
  936. char pipeRecvClientStr[100+1];
  937. char pipeSendServerStr[100+1];
  938. char pipeSendClientStr[100+1];
  939. std::snprintf(pipeRecvServerStr, 100, P_INTPTR, (intptr_t)pipeRecvServer); // pipe1[0]
  940. std::snprintf(pipeRecvClientStr, 100, P_INTPTR, (intptr_t)pipeRecvClient); // pipe2[0]
  941. std::snprintf(pipeSendServerStr, 100, P_INTPTR, (intptr_t)pipeSendServer); // pipe2[1]
  942. std::snprintf(pipeSendClientStr, 100, P_INTPTR, (intptr_t)pipeSendClient); // pipe1[1]
  943. pipeRecvServerStr[100] = '\0';
  944. pipeRecvClientStr[100] = '\0';
  945. pipeSendServerStr[100] = '\0';
  946. pipeSendClientStr[100] = '\0';
  947. argv[3] = pipeRecvServerStr; // pipe1[0] close READ
  948. argv[4] = pipeRecvClientStr; // pipe2[0] READ
  949. argv[5] = pipeSendServerStr; // pipe2[1] close SEND
  950. argv[6] = pipeSendClientStr; // pipe1[1] SEND
  951. //----------------------------------------------------------------
  952. // argv[7] => null
  953. argv[7] = nullptr;
  954. //----------------------------------------------------------------
  955. // start process
  956. #ifdef CARLA_OS_WIN
  957. if (! startProcess(argv, pData->processInfo))
  958. {
  959. carla_zeroStruct(pData->processInfo);
  960. try { ::CloseHandle(pipe1[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[0])");
  961. try { ::CloseHandle(pipe1[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[1])");
  962. try { ::CloseHandle(pipe2[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[0])");
  963. try { ::CloseHandle(pipe2[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[1])");
  964. fail("startProcess() failed");
  965. return false;
  966. }
  967. // just to make sure
  968. CARLA_SAFE_ASSERT(pData->processInfo.hThread != nullptr);
  969. CARLA_SAFE_ASSERT(pData->processInfo.hProcess != nullptr);
  970. #else
  971. if (! startProcess(argv, pData->pid))
  972. {
  973. pData->pid = -1;
  974. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  975. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  976. try { ::close(pipe2[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
  977. try { ::close(pipe2[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
  978. fail("startProcess() failed");
  979. return false;
  980. }
  981. #endif
  982. //----------------------------------------------------------------
  983. // close duplicated handles used by the client
  984. #ifdef CARLA_OS_WIN
  985. try { ::CloseHandle(pipeRecvServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvServer)");
  986. try { ::CloseHandle(pipeSendServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendServer)");
  987. #else
  988. try { ::close (pipeRecvServer); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
  989. try { ::close (pipeSendServer); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
  990. #endif
  991. pipeRecvServer = pipeSendServer = INVALID_PIPE_VALUE;
  992. #ifndef CARLA_OS_WIN
  993. //----------------------------------------------------------------
  994. // set non-block reading
  995. int ret = 0;
  996. try {
  997. ret = ::fcntl(pipeRecvClient, F_SETFL, ::fcntl(pipeRecvClient, F_GETFL) | O_NONBLOCK);
  998. } catch (...) {
  999. ret = -1;
  1000. fail("failed to set pipe as non-block");
  1001. }
  1002. #endif
  1003. //----------------------------------------------------------------
  1004. // wait for client to say something
  1005. #ifdef CARLA_OS_WIN
  1006. struct { HANDLE handle; HANDLE cancel; } pipe;
  1007. pipe.handle = pipeRecvClient;
  1008. pipe.cancel = pData->cancelEvent;
  1009. if ( waitForClientFirstMessage(pipe, 10*1000 /* 10 secs */))
  1010. #else
  1011. if (ret != -1 && waitForClientFirstMessage(pipeRecvClient, 10*1000 /* 10 secs */))
  1012. #endif
  1013. {
  1014. pData->pipeRecv = pipeRecvClient;
  1015. pData->pipeSend = pipeSendClient;
  1016. carla_stdout("ALL OK!");
  1017. return true;
  1018. }
  1019. //----------------------------------------------------------------
  1020. // failed to set non-block or get first child message, cannot continue
  1021. #ifdef CARLA_OS_WIN
  1022. if (TerminateProcess(pData->processInfo.hProcess, 0) != FALSE)
  1023. {
  1024. // wait for process to stop
  1025. waitForProcessToStop(pData->processInfo, 2*1000);
  1026. }
  1027. // clear pData->processInfo
  1028. try { CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1029. try { CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1030. carla_zeroStruct(pData->processInfo);
  1031. #else
  1032. if (::kill(pData->pid, SIGKILL) != -1)
  1033. {
  1034. // wait for killing to take place
  1035. waitForChildToStop(pData->pid, 2*1000);
  1036. }
  1037. pData->pid = -1;
  1038. #endif
  1039. //----------------------------------------------------------------
  1040. // close pipes
  1041. #ifdef CARLA_OS_WIN
  1042. try { ::CloseHandle(pipeRecvServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvServer)");
  1043. try { ::CloseHandle(pipeSendServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendServer)");
  1044. #else
  1045. try { ::close (pipeRecvServer); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
  1046. try { ::close (pipeSendServer); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
  1047. #endif
  1048. return false;
  1049. }
  1050. void CarlaPipeServer::stopPipeServer(const uint32_t timeOutMilliseconds) noexcept
  1051. {
  1052. carla_debug("CarlaPipeServer::stopPipeServer(%i)", timeOutMilliseconds);
  1053. #ifdef CARLA_OS_WIN
  1054. if (pData->processInfo.hProcess != INVALID_HANDLE_VALUE)
  1055. {
  1056. const CarlaMutexLocker cml(pData->writeLock);
  1057. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1058. _writeMsgBuffer("quit\n", 5);
  1059. waitForProcessToStopOrKillIt(pData->processInfo, timeOutMilliseconds);
  1060. try { CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1061. try { CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1062. carla_zeroStruct(pData->processInfo);
  1063. }
  1064. #else
  1065. if (pData->pid != -1)
  1066. {
  1067. const CarlaMutexLocker cml(pData->writeLock);
  1068. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1069. _writeMsgBuffer("quit\n", 5);
  1070. waitForChildToStopOrKillIt(pData->pid, timeOutMilliseconds);
  1071. pData->pid = -1;
  1072. }
  1073. #endif
  1074. closePipeServer();
  1075. }
  1076. void CarlaPipeServer::closePipeServer() noexcept
  1077. {
  1078. carla_debug("CarlaPipeServer::closePipeServer()");
  1079. const CarlaMutexLocker cml(pData->writeLock);
  1080. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1081. {
  1082. #ifdef CARLA_OS_WIN
  1083. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1084. #else
  1085. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1086. #endif
  1087. pData->pipeRecv = INVALID_PIPE_VALUE;
  1088. }
  1089. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1090. {
  1091. #ifdef CARLA_OS_WIN
  1092. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1093. #else
  1094. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1095. #endif
  1096. pData->pipeSend = INVALID_PIPE_VALUE;
  1097. }
  1098. }
  1099. // -----------------------------------------------------------------------
  1100. CarlaPipeClient::CarlaPipeClient() noexcept
  1101. : CarlaPipeCommon(),
  1102. leakDetector_CarlaPipeClient()
  1103. {
  1104. carla_debug("CarlaPipeClient::CarlaPipeClient()");
  1105. }
  1106. CarlaPipeClient::~CarlaPipeClient() noexcept
  1107. {
  1108. carla_debug("CarlaPipeClient::~CarlaPipeClient()");
  1109. closePipeClient();
  1110. }
  1111. bool CarlaPipeClient::initPipeClient(const char* argv[]) noexcept
  1112. {
  1113. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  1114. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  1115. carla_debug("CarlaPipeClient::initPipeClient(%p)", argv);
  1116. const CarlaMutexLocker cml(pData->writeLock);
  1117. //----------------------------------------------------------------
  1118. // read arguments
  1119. #ifdef CARLA_OS_WIN
  1120. HANDLE pipeRecvServer = (HANDLE)std::atoll(argv[3]); // READ
  1121. HANDLE pipeRecvClient = (HANDLE)std::atoll(argv[4]);
  1122. HANDLE pipeSendServer = (HANDLE)std::atoll(argv[5]); // SEND
  1123. HANDLE pipeSendClient = (HANDLE)std::atoll(argv[6]);
  1124. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer != INVALID_HANDLE_VALUE, false);
  1125. CARLA_SAFE_ASSERT_RETURN(pipeRecvClient != INVALID_HANDLE_VALUE, false);
  1126. CARLA_SAFE_ASSERT_RETURN(pipeSendServer != INVALID_HANDLE_VALUE, false);
  1127. CARLA_SAFE_ASSERT_RETURN(pipeSendClient != INVALID_HANDLE_VALUE, false);
  1128. #else
  1129. int pipeRecvServer = std::atoi(argv[3]); // READ
  1130. int pipeRecvClient = std::atoi(argv[4]);
  1131. int pipeSendServer = std::atoi(argv[5]); // SEND
  1132. int pipeSendClient = std::atoi(argv[6]);
  1133. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer > 0, false);
  1134. CARLA_SAFE_ASSERT_RETURN(pipeRecvClient > 0, false);
  1135. CARLA_SAFE_ASSERT_RETURN(pipeSendServer > 0, false);
  1136. CARLA_SAFE_ASSERT_RETURN(pipeSendClient > 0, false);
  1137. #endif
  1138. //----------------------------------------------------------------
  1139. // close duplicated handles used by the client
  1140. #ifdef CARLA_OS_WIN
  1141. try { ::CloseHandle(pipeRecvClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvClient)");
  1142. try { ::CloseHandle(pipeSendClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendClient)");
  1143. #else
  1144. try { ::close (pipeRecvClient); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
  1145. try { ::close (pipeSendClient); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
  1146. #endif
  1147. pipeRecvClient = pipeSendClient = INVALID_PIPE_VALUE;
  1148. #ifndef CARLA_OS_WIN
  1149. //----------------------------------------------------------------
  1150. // set non-block reading
  1151. int ret = 0;
  1152. try {
  1153. ret = ::fcntl(pipeRecvServer, F_SETFL, ::fcntl(pipeRecvServer, F_GETFL) | O_NONBLOCK);
  1154. } catch (...) {
  1155. ret = -1;
  1156. }
  1157. CARLA_SAFE_ASSERT_RETURN(ret != -1, false);
  1158. #endif
  1159. //----------------------------------------------------------------
  1160. // done
  1161. pData->pipeRecv = pipeRecvServer;
  1162. pData->pipeSend = pipeSendServer;
  1163. writeMessage("\n", 1);
  1164. flushMessages();
  1165. return true;
  1166. }
  1167. void CarlaPipeClient::closePipeClient() noexcept
  1168. {
  1169. carla_debug("CarlaPipeClient::closePipeClient()");
  1170. const CarlaMutexLocker cml(pData->writeLock);
  1171. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1172. {
  1173. #ifdef CARLA_OS_WIN
  1174. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1175. #else
  1176. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1177. #endif
  1178. pData->pipeRecv = INVALID_PIPE_VALUE;
  1179. }
  1180. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1181. {
  1182. #ifdef CARLA_OS_WIN
  1183. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1184. #else
  1185. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1186. #endif
  1187. pData->pipeSend = INVALID_PIPE_VALUE;
  1188. }
  1189. }
  1190. // -----------------------------------------------------------------------
  1191. ScopedLocale::ScopedLocale() noexcept
  1192. : fLocale(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr)))
  1193. {
  1194. ::setlocale(LC_NUMERIC, "C");
  1195. }
  1196. ScopedLocale::~ScopedLocale() noexcept
  1197. {
  1198. if (fLocale != nullptr)
  1199. {
  1200. ::setlocale(LC_NUMERIC, fLocale);
  1201. delete[] fLocale;
  1202. }
  1203. }
  1204. // -----------------------------------------------------------------------
  1205. #undef INVALID_PIPE_VALUE