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.

CarlaPipeUtils.cpp 42KB

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