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.

1528 lines
42KB

  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(const bool onlyOnce) 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 && ! onlyOnce)
  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. if (onlyOnce)
  419. break;
  420. }
  421. if (locale != nullptr)
  422. {
  423. ::setlocale(LC_NUMERIC, locale);
  424. delete[] locale;
  425. }
  426. }
  427. // -------------------------------------------------------------------
  428. void CarlaPipeCommon::lockPipe() const noexcept
  429. {
  430. pData->writeLock.lock();
  431. }
  432. bool CarlaPipeCommon::tryLockPipe() const noexcept
  433. {
  434. return pData->writeLock.tryLock();
  435. }
  436. void CarlaPipeCommon::unlockPipe() const noexcept
  437. {
  438. pData->writeLock.unlock();
  439. }
  440. CarlaMutex& CarlaPipeCommon::getPipeLock() const noexcept
  441. {
  442. return pData->writeLock;
  443. }
  444. // -------------------------------------------------------------------
  445. bool CarlaPipeCommon::readNextLineAsBool(bool& value) const noexcept
  446. {
  447. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  448. if (const char* const msg = _readlineblock())
  449. {
  450. value = (std::strcmp(msg, "true") == 0);
  451. delete[] msg;
  452. return true;
  453. }
  454. return false;
  455. }
  456. bool CarlaPipeCommon::readNextLineAsByte(uint8_t& value) const noexcept
  457. {
  458. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  459. if (const char* const msg = _readlineblock())
  460. {
  461. int tmp = std::atoi(msg);
  462. delete[] msg;
  463. if (tmp >= 0 && tmp <= 0xFF)
  464. {
  465. value = static_cast<uint8_t>(tmp);
  466. return true;
  467. }
  468. }
  469. return false;
  470. }
  471. bool CarlaPipeCommon::readNextLineAsInt(int32_t& value) const noexcept
  472. {
  473. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  474. if (const char* const msg = _readlineblock())
  475. {
  476. value = std::atoi(msg);
  477. delete[] msg;
  478. return true;
  479. }
  480. return false;
  481. }
  482. bool CarlaPipeCommon::readNextLineAsUInt(uint32_t& value) const noexcept
  483. {
  484. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  485. if (const char* const msg = _readlineblock())
  486. {
  487. int32_t tmp = std::atoi(msg);
  488. delete[] msg;
  489. if (tmp >= 0)
  490. {
  491. value = static_cast<uint32_t>(tmp);
  492. return true;
  493. }
  494. }
  495. return false;
  496. }
  497. bool CarlaPipeCommon::readNextLineAsLong(int64_t& value) const noexcept
  498. {
  499. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  500. if (const char* const msg = _readlineblock())
  501. {
  502. value = std::atol(msg);
  503. delete[] msg;
  504. return true;
  505. }
  506. return false;
  507. }
  508. bool CarlaPipeCommon::readNextLineAsULong(uint64_t& value) const noexcept
  509. {
  510. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  511. if (const char* const msg = _readlineblock())
  512. {
  513. int64_t tmp = std::atol(msg);
  514. delete[] msg;
  515. if (tmp >= 0)
  516. {
  517. value = static_cast<uint64_t>(tmp);
  518. return true;
  519. }
  520. }
  521. return false;
  522. }
  523. bool CarlaPipeCommon::readNextLineAsFloat(float& value) const noexcept
  524. {
  525. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  526. if (const char* const msg = _readlineblock())
  527. {
  528. value = static_cast<float>(std::atof(msg));
  529. delete[] msg;
  530. return true;
  531. }
  532. return false;
  533. }
  534. bool CarlaPipeCommon::readNextLineAsDouble(double& value) const noexcept
  535. {
  536. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  537. if (const char* const msg = _readlineblock())
  538. {
  539. value = std::atof(msg);
  540. delete[] msg;
  541. return true;
  542. }
  543. return false;
  544. }
  545. bool CarlaPipeCommon::readNextLineAsString(const char*& value) const noexcept
  546. {
  547. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  548. if (const char* const msg = _readlineblock())
  549. {
  550. value = msg;
  551. return true;
  552. }
  553. return false;
  554. }
  555. // -------------------------------------------------------------------
  556. // must be locked before calling
  557. bool CarlaPipeCommon::writeMessage(const char* const msg) const noexcept
  558. {
  559. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  560. const std::size_t size(std::strlen(msg));
  561. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  562. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  563. return _writeMsgBuffer(msg, size);
  564. }
  565. bool CarlaPipeCommon::writeMessage(const char* const msg, std::size_t size) const noexcept
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  568. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  569. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  570. return _writeMsgBuffer(msg, size);
  571. }
  572. bool CarlaPipeCommon::writeAndFixMessage(const char* const msg) const noexcept
  573. {
  574. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, false);
  575. const std::size_t size(std::strlen(msg));
  576. char fixedMsg[size+2];
  577. if (size > 0)
  578. {
  579. std::strcpy(fixedMsg, msg);
  580. for (std::size_t i=0; i<size; ++i)
  581. {
  582. if (fixedMsg[i] == '\n')
  583. fixedMsg[i] = '\r';
  584. }
  585. if (fixedMsg[size-1] == '\r')
  586. {
  587. fixedMsg[size-1] = '\n';
  588. fixedMsg[size] = '\0';
  589. fixedMsg[size+1] = '\0';
  590. }
  591. else
  592. {
  593. fixedMsg[size] = '\n';
  594. fixedMsg[size+1] = '\0';
  595. }
  596. }
  597. else
  598. {
  599. fixedMsg[0] = '\n';
  600. fixedMsg[1] = '\0';
  601. }
  602. return _writeMsgBuffer(fixedMsg, size+1);
  603. }
  604. bool CarlaPipeCommon::flushMessages() const noexcept
  605. {
  606. // TESTING remove later (replace with trylock scope)
  607. if (pData->writeLock.tryLock())
  608. {
  609. carla_safe_assert("! pData->writeLock.tryLock()", __FILE__, __LINE__);
  610. pData->writeLock.unlock();
  611. return false;
  612. }
  613. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend != INVALID_PIPE_VALUE, false);
  614. try {
  615. #ifdef CARLA_OS_WIN
  616. return (::FlushFileBuffers(pData->pipeSend) != FALSE);
  617. #else
  618. return (::fsync(pData->pipeSend) == 0);
  619. #endif
  620. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  621. }
  622. // -------------------------------------------------------------------
  623. void CarlaPipeCommon::writeErrorMessage(const char* const error) const noexcept
  624. {
  625. CARLA_SAFE_ASSERT_RETURN(error != nullptr && error[0] != '\0',);
  626. const CarlaMutexLocker cml(pData->writeLock);
  627. _writeMsgBuffer("error\n", 6);
  628. writeAndFixMessage(error);
  629. flushMessages();
  630. }
  631. void CarlaPipeCommon::writeControlMessage(const uint32_t index, const float value) const noexcept
  632. {
  633. char tmpBuf[0xff+1];
  634. tmpBuf[0xff] = '\0';
  635. const CarlaMutexLocker cml(pData->writeLock);
  636. const ScopedLocale csl;
  637. _writeMsgBuffer("control\n", 8);
  638. {
  639. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  640. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  641. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  642. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  643. }
  644. flushMessages();
  645. }
  646. void CarlaPipeCommon::writeConfigureMessage(const char* const key, const char* const value) const noexcept
  647. {
  648. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  649. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  650. const CarlaMutexLocker cml(pData->writeLock);
  651. _writeMsgBuffer("configure\n", 10);
  652. {
  653. writeAndFixMessage(key);
  654. writeAndFixMessage(value);
  655. }
  656. flushMessages();
  657. }
  658. void CarlaPipeCommon::writeProgramMessage(const uint32_t index) const noexcept
  659. {
  660. char tmpBuf[0xff+1];
  661. tmpBuf[0xff] = '\0';
  662. const CarlaMutexLocker cml(pData->writeLock);
  663. _writeMsgBuffer("program\n", 8);
  664. {
  665. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  666. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  667. }
  668. flushMessages();
  669. }
  670. void CarlaPipeCommon::writeMidiProgramMessage(const uint32_t bank, const uint32_t program) const noexcept
  671. {
  672. char tmpBuf[0xff+1];
  673. tmpBuf[0xff] = '\0';
  674. const CarlaMutexLocker cml(pData->writeLock);
  675. _writeMsgBuffer("midiprogram\n", 8);
  676. {
  677. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  678. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  679. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  680. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  681. }
  682. flushMessages();
  683. }
  684. void CarlaPipeCommon::writeMidiNoteMessage(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) const noexcept
  685. {
  686. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  687. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  688. CARLA_SAFE_ASSERT_RETURN(velocity < MAX_MIDI_VALUE,);
  689. char tmpBuf[0xff+1];
  690. tmpBuf[0xff] = '\0';
  691. const CarlaMutexLocker cml(pData->writeLock);
  692. _writeMsgBuffer("note\n", 5);
  693. {
  694. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(onOff));
  695. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  696. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  697. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  698. std::snprintf(tmpBuf, 0xff, "%i\n", note);
  699. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  700. std::snprintf(tmpBuf, 0xff, "%i\n", velocity);
  701. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  702. }
  703. flushMessages();
  704. }
  705. void CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index, const LV2_Atom* const atom) const noexcept
  706. {
  707. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  708. char tmpBuf[0xff+1];
  709. tmpBuf[0xff] = '\0';
  710. CarlaString base64atom(CarlaString::asBase64(atom, lv2_atom_total_size(atom)));
  711. const CarlaMutexLocker cml(pData->writeLock);
  712. _writeMsgBuffer("atom\n", 5);
  713. {
  714. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  715. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  716. std::snprintf(tmpBuf, 0xff, "%i\n", atom->size);
  717. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  718. writeAndFixMessage(base64atom.buffer());
  719. }
  720. flushMessages();
  721. }
  722. void CarlaPipeCommon::writeLv2UridMessage(const uint32_t urid, const char* const uri) const noexcept
  723. {
  724. CARLA_SAFE_ASSERT_RETURN(urid != 0,);
  725. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  726. char tmpBuf[0xff+1];
  727. tmpBuf[0xff] = '\0';
  728. const CarlaMutexLocker cml(pData->writeLock);
  729. _writeMsgBuffer("urid\n", 5);
  730. {
  731. std::snprintf(tmpBuf, 0xff, "%i\n", urid);
  732. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  733. writeAndFixMessage(uri);
  734. }
  735. flushMessages();
  736. }
  737. // -------------------------------------------------------------------
  738. // internal
  739. const char* CarlaPipeCommon::_readline() const noexcept
  740. {
  741. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv != INVALID_PIPE_VALUE, nullptr);
  742. char c;
  743. char* ptr = pData->tmpBuf;
  744. ssize_t ret;
  745. pData->tmpStr.clear();
  746. for (int i=0; i < 0xff; ++i)
  747. {
  748. try {
  749. #ifdef CARLA_OS_WIN
  750. ret = ::ReadFileNonBlock(pData->pipeRecv, pData->cancelEvent, &c, 1);
  751. #else
  752. ret = ::read(pData->pipeRecv, &c, 1);
  753. #endif
  754. } CARLA_SAFE_EXCEPTION_BREAK("CarlaPipeCommon::readline() - read");
  755. if (ret == 1 && c != '\n')
  756. {
  757. if (c == '\r')
  758. c = '\n';
  759. *ptr++ = c;
  760. if (i+1 == 0xff)
  761. {
  762. i = 0;
  763. ptr = pData->tmpBuf;
  764. pData->tmpStr += pData->tmpBuf;
  765. }
  766. continue;
  767. }
  768. if (pData->tmpStr.isNotEmpty() || ptr != pData->tmpBuf || ret == 1)
  769. {
  770. if (ptr != pData->tmpBuf)
  771. {
  772. *ptr = '\0';
  773. pData->tmpStr += pData->tmpBuf;
  774. }
  775. try {
  776. return pData->tmpStr.dup();
  777. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - dup", nullptr);
  778. }
  779. break;
  780. }
  781. return nullptr;
  782. }
  783. const char* CarlaPipeCommon::_readlineblock(const uint32_t timeOutMilliseconds) const noexcept
  784. {
  785. const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds);
  786. for (;;)
  787. {
  788. if (const char* const msg = _readline())
  789. return msg;
  790. if (juce::Time::getMillisecondCounter() >= timeoutEnd)
  791. break;
  792. carla_msleep(5);
  793. }
  794. carla_stderr("readlineblock timed out");
  795. return nullptr;
  796. }
  797. bool CarlaPipeCommon::_writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept
  798. {
  799. // TESTING remove later (replace with trylock scope)
  800. if (pData->writeLock.tryLock())
  801. {
  802. carla_safe_assert("! pData->writeLock.tryLock()", __FILE__, __LINE__);
  803. pData->writeLock.unlock();
  804. return false;
  805. }
  806. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend != INVALID_PIPE_VALUE, false);
  807. ssize_t ret;
  808. try {
  809. #ifdef CARLA_OS_WIN
  810. ret = ::WriteFileNonBlock(pData->pipeSend, pData->cancelEvent, msg, size);
  811. #else
  812. ret = ::write(pData->pipeSend, msg, size);
  813. #endif
  814. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  815. return (ret == static_cast<ssize_t>(size));
  816. }
  817. // -----------------------------------------------------------------------
  818. CarlaPipeServer::CarlaPipeServer() noexcept
  819. : CarlaPipeCommon(),
  820. leakDetector_CarlaPipeServer()
  821. {
  822. carla_debug("CarlaPipeServer::CarlaPipeServer()");
  823. }
  824. CarlaPipeServer::~CarlaPipeServer() /*noexcept*/
  825. {
  826. carla_debug("CarlaPipeServer::~CarlaPipeServer()");
  827. stopPipeServer(5*1000);
  828. }
  829. uintptr_t CarlaPipeServer::getPID() const noexcept
  830. {
  831. #ifndef CARLA_OS_WIN
  832. return static_cast<uintptr_t>(pData->pid);
  833. #else
  834. return 0;
  835. #endif
  836. }
  837. // -----------------------------------------------------------------------
  838. bool CarlaPipeServer::startPipeServer(const char* const filename, const char* const arg1, const char* const arg2) noexcept
  839. {
  840. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  841. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  842. #ifdef CARLA_OS_WIN
  843. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hThread == nullptr, false);
  844. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hProcess == nullptr, false);
  845. #else
  846. CARLA_SAFE_ASSERT_RETURN(pData->pid == -1, false);
  847. #endif
  848. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  849. CARLA_SAFE_ASSERT_RETURN(arg1 != nullptr, false);
  850. CARLA_SAFE_ASSERT_RETURN(arg2 != nullptr, false);
  851. carla_debug("CarlaPipeServer::startPipeServer(\"%s\", \"%s\", \"%s\")", filename, arg1, arg2);
  852. const CarlaMutexLocker cml(pData->writeLock);
  853. //----------------------------------------------------------------
  854. // create pipes
  855. #ifdef CARLA_OS_WIN
  856. HANDLE pipe1[2]; // read by server, written by client
  857. HANDLE pipe2[2]; // read by client, written by server
  858. SECURITY_ATTRIBUTES sa;
  859. carla_zeroStruct(sa);
  860. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  861. sa.bInheritHandle = TRUE;
  862. std::srand(static_cast<uint>(std::time(nullptr)));
  863. char strBuf[0xff+1];
  864. strBuf[0xff] = '\0';
  865. static ulong sCounter = 0;
  866. ++sCounter;
  867. const int randint = std::rand();
  868. std::snprintf(strBuf, 0xff, "\\\\.\\pipe\\carla-pipe1-%i-%li", randint, sCounter);
  869. pipe1[0] = ::CreateNamedPipeA(strBuf, PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_WAIT, 1, 4096, 4096, 120*1000, &sa);
  870. pipe1[1] = ::CreateFileA(strBuf, GENERIC_WRITE, 0x0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, nullptr);
  871. std::snprintf(strBuf, 0xff, "\\\\.\\pipe\\carla-pipe2-%i-%li", randint, sCounter);
  872. pipe2[0] = ::CreateNamedPipeA(strBuf, PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_WAIT, 1, 4096, 4096, 120*1000, &sa);
  873. pipe2[1] = ::CreateFileA(strBuf, GENERIC_WRITE, 0x0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, nullptr);
  874. if (pipe1[0] == INVALID_HANDLE_VALUE || pipe1[1] == INVALID_HANDLE_VALUE || pipe2[0] == INVALID_HANDLE_VALUE || pipe2[1] == INVALID_HANDLE_VALUE)
  875. {
  876. if (pipe1[0] != INVALID_HANDLE_VALUE) {
  877. try { ::CloseHandle(pipe1[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[0])");
  878. }
  879. if (pipe1[1] != INVALID_HANDLE_VALUE) {
  880. try { ::CloseHandle(pipe1[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[1])");
  881. }
  882. if (pipe2[0] != INVALID_HANDLE_VALUE) {
  883. try { ::CloseHandle(pipe2[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[0])");
  884. }
  885. if (pipe2[1] != INVALID_HANDLE_VALUE) {
  886. try { ::CloseHandle(pipe2[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[1])");
  887. }
  888. fail("pipe creation failed");
  889. return false;
  890. }
  891. HANDLE pipeRecvServer = pipe1[0];
  892. HANDLE pipeRecvClient = pipe2[0];
  893. HANDLE pipeSendClient = pipe1[1];
  894. HANDLE pipeSendServer = pipe2[1];
  895. #else
  896. int pipe1[2]; // read by server, written by client
  897. int pipe2[2]; // read by client, written by server
  898. if (::pipe(pipe1) != 0)
  899. {
  900. fail("pipe1 creation failed");
  901. return false;
  902. }
  903. if (::pipe(pipe2) != 0)
  904. {
  905. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  906. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  907. fail("pipe2 creation failed");
  908. return false;
  909. }
  910. int pipeRecvServer = pipe1[0];
  911. int pipeRecvClient = pipe2[0];
  912. int pipeSendClient = pipe1[1];
  913. int pipeSendServer = pipe2[1];
  914. #endif
  915. //----------------------------------------------------------------
  916. // set arguments
  917. const char* argv[8];
  918. //----------------------------------------------------------------
  919. // argv[0] => filename
  920. argv[0] = filename;
  921. //----------------------------------------------------------------
  922. // argv[1-2] => args
  923. argv[1] = arg1;
  924. argv[2] = arg2;
  925. //----------------------------------------------------------------
  926. // argv[3-6] => pipes
  927. char pipeRecvServerStr[100+1];
  928. char pipeRecvClientStr[100+1];
  929. char pipeSendServerStr[100+1];
  930. char pipeSendClientStr[100+1];
  931. std::snprintf(pipeRecvServerStr, 100, P_INTPTR, (intptr_t)pipeRecvServer); // pipe1[0]
  932. std::snprintf(pipeRecvClientStr, 100, P_INTPTR, (intptr_t)pipeRecvClient); // pipe2[0]
  933. std::snprintf(pipeSendServerStr, 100, P_INTPTR, (intptr_t)pipeSendServer); // pipe2[1]
  934. std::snprintf(pipeSendClientStr, 100, P_INTPTR, (intptr_t)pipeSendClient); // pipe1[1]
  935. pipeRecvServerStr[100] = '\0';
  936. pipeRecvClientStr[100] = '\0';
  937. pipeSendServerStr[100] = '\0';
  938. pipeSendClientStr[100] = '\0';
  939. argv[3] = pipeRecvServerStr; // pipe1[0] close READ
  940. argv[4] = pipeRecvClientStr; // pipe2[0] READ
  941. argv[5] = pipeSendServerStr; // pipe2[1] close SEND
  942. argv[6] = pipeSendClientStr; // pipe1[1] SEND
  943. //----------------------------------------------------------------
  944. // argv[7] => null
  945. argv[7] = nullptr;
  946. //----------------------------------------------------------------
  947. // start process
  948. #ifdef CARLA_OS_WIN
  949. if (! startProcess(argv, pData->processInfo))
  950. {
  951. carla_zeroStruct(pData->processInfo);
  952. try { ::CloseHandle(pipe1[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[0])");
  953. try { ::CloseHandle(pipe1[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1[1])");
  954. try { ::CloseHandle(pipe2[0]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[0])");
  955. try { ::CloseHandle(pipe2[1]); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2[1])");
  956. fail("startProcess() failed");
  957. return false;
  958. }
  959. // just to make sure
  960. CARLA_SAFE_ASSERT(pData->processInfo.hThread != nullptr);
  961. CARLA_SAFE_ASSERT(pData->processInfo.hProcess != nullptr);
  962. #else
  963. if (! startProcess(argv, pData->pid))
  964. {
  965. pData->pid = -1;
  966. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  967. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  968. try { ::close(pipe2[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
  969. try { ::close(pipe2[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
  970. fail("startProcess() failed");
  971. return false;
  972. }
  973. #endif
  974. //----------------------------------------------------------------
  975. // close duplicated handles used by the client
  976. #ifdef CARLA_OS_WIN
  977. try { ::CloseHandle(pipeRecvServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvServer)");
  978. try { ::CloseHandle(pipeSendServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendServer)");
  979. #else
  980. try { ::close (pipeRecvServer); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
  981. try { ::close (pipeSendServer); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
  982. #endif
  983. pipeRecvServer = pipeSendServer = INVALID_PIPE_VALUE;
  984. #ifndef CARLA_OS_WIN
  985. //----------------------------------------------------------------
  986. // set non-block reading
  987. int ret = 0;
  988. try {
  989. ret = ::fcntl(pipeRecvClient, F_SETFL, ::fcntl(pipeRecvClient, F_GETFL) | O_NONBLOCK);
  990. } catch (...) {
  991. ret = -1;
  992. fail("failed to set pipe as non-block");
  993. }
  994. #endif
  995. //----------------------------------------------------------------
  996. // wait for client to say something
  997. #ifdef CARLA_OS_WIN
  998. struct { HANDLE handle; HANDLE cancel; } pipe;
  999. pipe.handle = pipeRecvClient;
  1000. pipe.cancel = pData->cancelEvent;
  1001. if ( waitForClientFirstMessage(pipe, 10*1000 /* 10 secs */))
  1002. #else
  1003. if (ret != -1 && waitForClientFirstMessage(pipeRecvClient, 10*1000 /* 10 secs */))
  1004. #endif
  1005. {
  1006. pData->pipeRecv = pipeRecvClient;
  1007. pData->pipeSend = pipeSendClient;
  1008. carla_stdout("ALL OK!");
  1009. return true;
  1010. }
  1011. //----------------------------------------------------------------
  1012. // failed to set non-block or get first child message, cannot continue
  1013. #ifdef CARLA_OS_WIN
  1014. if (TerminateProcess(pData->processInfo.hProcess, 0) != FALSE)
  1015. {
  1016. // wait for process to stop
  1017. waitForProcessToStop(pData->processInfo, 2*1000);
  1018. }
  1019. // clear pData->processInfo
  1020. try { CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1021. try { CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1022. carla_zeroStruct(pData->processInfo);
  1023. #else
  1024. if (::kill(pData->pid, SIGKILL) != -1)
  1025. {
  1026. // wait for killing to take place
  1027. waitForChildToStop(pData->pid, 2*1000);
  1028. }
  1029. pData->pid = -1;
  1030. #endif
  1031. //----------------------------------------------------------------
  1032. // close pipes
  1033. #ifdef CARLA_OS_WIN
  1034. try { ::CloseHandle(pipeRecvServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvServer)");
  1035. try { ::CloseHandle(pipeSendServer); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendServer)");
  1036. #else
  1037. try { ::close (pipeRecvServer); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
  1038. try { ::close (pipeSendServer); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
  1039. #endif
  1040. return false;
  1041. }
  1042. void CarlaPipeServer::stopPipeServer(const uint32_t timeOutMilliseconds) noexcept
  1043. {
  1044. carla_debug("CarlaPipeServer::stopPipeServer(%i)", timeOutMilliseconds);
  1045. #ifdef CARLA_OS_WIN
  1046. if (pData->processInfo.hProcess != INVALID_HANDLE_VALUE)
  1047. {
  1048. const CarlaMutexLocker cml(pData->writeLock);
  1049. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1050. _writeMsgBuffer("quit\n", 5);
  1051. waitForProcessToStopOrKillIt(pData->processInfo, timeOutMilliseconds);
  1052. try { CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1053. try { CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1054. carla_zeroStruct(pData->processInfo);
  1055. }
  1056. #else
  1057. if (pData->pid != -1)
  1058. {
  1059. const CarlaMutexLocker cml(pData->writeLock);
  1060. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1061. _writeMsgBuffer("quit\n", 5);
  1062. waitForChildToStopOrKillIt(pData->pid, timeOutMilliseconds);
  1063. pData->pid = -1;
  1064. }
  1065. #endif
  1066. closePipeServer();
  1067. }
  1068. void CarlaPipeServer::closePipeServer() noexcept
  1069. {
  1070. carla_debug("CarlaPipeServer::closePipeServer()");
  1071. const CarlaMutexLocker cml(pData->writeLock);
  1072. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1073. {
  1074. #ifdef CARLA_OS_WIN
  1075. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1076. #else
  1077. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1078. #endif
  1079. pData->pipeRecv = INVALID_PIPE_VALUE;
  1080. }
  1081. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1082. {
  1083. #ifdef CARLA_OS_WIN
  1084. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1085. #else
  1086. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1087. #endif
  1088. pData->pipeSend = INVALID_PIPE_VALUE;
  1089. }
  1090. }
  1091. void CarlaPipeServer::writeShowMessage() const noexcept
  1092. {
  1093. const CarlaMutexLocker cml(pData->writeLock);
  1094. _writeMsgBuffer("show\n", 5);
  1095. flushMessages();
  1096. }
  1097. void CarlaPipeServer::writeFocusMessage() const noexcept
  1098. {
  1099. const CarlaMutexLocker cml(pData->writeLock);
  1100. _writeMsgBuffer("focus\n", 6);
  1101. flushMessages();
  1102. }
  1103. void CarlaPipeServer::writeHideMessage() const noexcept
  1104. {
  1105. const CarlaMutexLocker cml(pData->writeLock);
  1106. _writeMsgBuffer("show\n", 5);
  1107. flushMessages();
  1108. }
  1109. // -----------------------------------------------------------------------
  1110. CarlaPipeClient::CarlaPipeClient() noexcept
  1111. : CarlaPipeCommon(),
  1112. leakDetector_CarlaPipeClient()
  1113. {
  1114. carla_debug("CarlaPipeClient::CarlaPipeClient()");
  1115. }
  1116. CarlaPipeClient::~CarlaPipeClient() /*noexcept*/
  1117. {
  1118. carla_debug("CarlaPipeClient::~CarlaPipeClient()");
  1119. closePipeClient();
  1120. }
  1121. bool CarlaPipeClient::initPipeClient(const char* argv[]) noexcept
  1122. {
  1123. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  1124. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  1125. carla_debug("CarlaPipeClient::initPipeClient(%p)", argv);
  1126. const CarlaMutexLocker cml(pData->writeLock);
  1127. //----------------------------------------------------------------
  1128. // read arguments
  1129. #ifdef CARLA_OS_WIN
  1130. HANDLE pipeRecvServer = (HANDLE)std::atoll(argv[3]); // READ
  1131. HANDLE pipeRecvClient = (HANDLE)std::atoll(argv[4]);
  1132. HANDLE pipeSendServer = (HANDLE)std::atoll(argv[5]); // SEND
  1133. HANDLE pipeSendClient = (HANDLE)std::atoll(argv[6]);
  1134. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer != INVALID_HANDLE_VALUE, false);
  1135. CARLA_SAFE_ASSERT_RETURN(pipeRecvClient != INVALID_HANDLE_VALUE, false);
  1136. CARLA_SAFE_ASSERT_RETURN(pipeSendServer != INVALID_HANDLE_VALUE, false);
  1137. CARLA_SAFE_ASSERT_RETURN(pipeSendClient != INVALID_HANDLE_VALUE, false);
  1138. #else
  1139. int pipeRecvServer = std::atoi(argv[3]); // READ
  1140. int pipeRecvClient = std::atoi(argv[4]);
  1141. int pipeSendServer = std::atoi(argv[5]); // SEND
  1142. int pipeSendClient = std::atoi(argv[6]);
  1143. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer > 0, false);
  1144. CARLA_SAFE_ASSERT_RETURN(pipeRecvClient > 0, false);
  1145. CARLA_SAFE_ASSERT_RETURN(pipeSendServer > 0, false);
  1146. CARLA_SAFE_ASSERT_RETURN(pipeSendClient > 0, false);
  1147. #endif
  1148. //----------------------------------------------------------------
  1149. // close duplicated handles used by the client
  1150. #ifdef CARLA_OS_WIN
  1151. try { ::CloseHandle(pipeRecvClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvClient)");
  1152. try { ::CloseHandle(pipeSendClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendClient)");
  1153. #else
  1154. try { ::close (pipeRecvClient); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
  1155. try { ::close (pipeSendClient); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
  1156. #endif
  1157. pipeRecvClient = pipeSendClient = INVALID_PIPE_VALUE;
  1158. #ifndef CARLA_OS_WIN
  1159. //----------------------------------------------------------------
  1160. // set non-block reading
  1161. int ret = 0;
  1162. try {
  1163. ret = ::fcntl(pipeRecvServer, F_SETFL, ::fcntl(pipeRecvServer, F_GETFL) | O_NONBLOCK);
  1164. } catch (...) {
  1165. ret = -1;
  1166. }
  1167. CARLA_SAFE_ASSERT_RETURN(ret != -1, false);
  1168. #endif
  1169. //----------------------------------------------------------------
  1170. // done
  1171. pData->pipeRecv = pipeRecvServer;
  1172. pData->pipeSend = pipeSendServer;
  1173. writeMessage("\n", 1);
  1174. flushMessages();
  1175. return true;
  1176. }
  1177. void CarlaPipeClient::closePipeClient() noexcept
  1178. {
  1179. carla_debug("CarlaPipeClient::closePipeClient()");
  1180. const CarlaMutexLocker cml(pData->writeLock);
  1181. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1182. {
  1183. #ifdef CARLA_OS_WIN
  1184. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1185. #else
  1186. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1187. #endif
  1188. pData->pipeRecv = INVALID_PIPE_VALUE;
  1189. }
  1190. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1191. {
  1192. #ifdef CARLA_OS_WIN
  1193. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1194. #else
  1195. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1196. #endif
  1197. pData->pipeSend = INVALID_PIPE_VALUE;
  1198. }
  1199. }
  1200. // -----------------------------------------------------------------------
  1201. ScopedLocale::ScopedLocale() noexcept
  1202. : fLocale(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr)))
  1203. {
  1204. ::setlocale(LC_NUMERIC, "C");
  1205. }
  1206. ScopedLocale::~ScopedLocale() noexcept
  1207. {
  1208. if (fLocale != nullptr)
  1209. {
  1210. ::setlocale(LC_NUMERIC, fLocale);
  1211. delete[] fLocale;
  1212. }
  1213. }
  1214. // -----------------------------------------------------------------------
  1215. #undef INVALID_PIPE_VALUE