3 * Copyright (C) 2013-2024 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaPipeUtils.hpp"
19 #include "CarlaProcessUtils.hpp"
20 #include "CarlaString.hpp"
21 #include "CarlaTimeUtils.hpp"
22 #include "CarlaMIDI.h"
24 // needed for atom-util
31 # include "lv2/atom-util.h"
33 # include "lv2/atom/util.h"
39 # include "water/text/String.h"
44 # include <sys/wait.h>
45 # ifdef CARLA_OS_LINUX
46 # include <sys/prctl.h>
48 # define F_SETPIPE_SZ 1031
54 # define INVALID_PIPE_VALUE INVALID_HANDLE_VALUE
56 # define INVALID_PIPE_VALUE -1
60 // -----------------------------------------------------------------------
64 bool waitForAsyncObject(const HANDLE object
, const HANDLE process
= INVALID_HANDLE_VALUE
)
70 for (int i
=20000; --i
>=0;)
72 if (process
!= INVALID_HANDLE_VALUE
)
74 switch (::WaitForSingleObject(process
, 0))
78 carla_stderr("waitForAsyncObject process has stopped");
83 carla_debug("waitForAsyncObject loop start");
84 dw
= ::MsgWaitForMultipleObjectsEx(1, &object
, INFINITE
, QS_POSTMESSAGE
|QS_TIMER
, 0);
85 carla_debug("waitForAsyncObject initial code is: %u", dw
);
87 if (dw
== WAIT_OBJECT_0
)
89 carla_debug("waitForAsyncObject WAIT_OBJECT_0");
93 dw2
= ::GetLastError();
95 if (dw
== WAIT_OBJECT_0
+ 1)
97 carla_debug("waitForAsyncObject loop +1");
99 while (::PeekMessage(&msg
, nullptr, 0, 0, PM_REMOVE
))
100 ::DispatchMessage(&msg
);
107 carla_debug("waitForAsyncObject loop stop");
111 carla_stderr2("waitForAsyncObject loop end reached, error was: %u", dw2
);
115 carla_stderr2("waitForAsyncObject reached the end, this should not happen");
120 ssize_t
ReadFileWin32(const HANDLE pipeh
, const HANDLE event
, void* const buf
, const DWORD numBytes
)
122 DWORD dw
, dsize
= numBytes
;
125 if (::PeekNamedPipe(pipeh
, nullptr, 0, nullptr, &available
, nullptr) == FALSE
|| available
== 0)
129 carla_zeroStruct(ov
);
132 if (::ReadFile(pipeh
, buf
, dsize
, nullptr, &ov
))
134 if (! ::GetOverlappedResult(pipeh
, &ov
, &dw
, FALSE
))
136 carla_stderr("ReadFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
140 return static_cast<ssize_t
>(dsize
);
143 dw
= ::GetLastError();
145 if (dw
== ERROR_IO_PENDING
)
147 if (! waitForAsyncObject(event
))
149 carla_stderr("ReadFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
153 if (! ::GetOverlappedResult(pipeh
, &ov
, &dw
, FALSE
))
155 carla_stderr("ReadFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
159 return static_cast<ssize_t
>(dsize
);
162 carla_stderr("ReadFileWin32 failed, error was: %u", dw
);
167 ssize_t
WriteFileWin32(const HANDLE pipeh
, const HANDLE event
, const void* const buf
, const DWORD numBytes
)
169 DWORD dw
, dsize
= numBytes
;
172 carla_zeroStruct(ov
);
175 if (::WriteFile(pipeh
, buf
, dsize
, nullptr, &ov
))
177 if (! ::GetOverlappedResult(pipeh
, &ov
, &dw
, FALSE
))
179 carla_stderr("WriteFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
183 return static_cast<ssize_t
>(dsize
);
186 dw
= ::GetLastError();
188 if (dw
== ERROR_IO_PENDING
)
190 if (! waitForAsyncObject(event
))
192 carla_stderr("WriteFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
196 if (! ::GetOverlappedResult(pipeh
, &ov
, &dw
, FALSE
))
198 carla_stderr("WriteFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
202 return static_cast<ssize_t
>(dsize
);
205 if (dw
== ERROR_PIPE_NOT_CONNECTED
)
207 carla_stdout("WriteFileWin32 failed, client has closed");
211 carla_stderr("WriteFileWin32 failed, error was: %u", dw
);
214 #endif // CARLA_OS_WIN
216 // -----------------------------------------------------------------------
221 bool startProcess(const char* const argv
[], PROCESS_INFORMATION
* const processInfo
)
223 CARLA_SAFE_ASSERT_RETURN(processInfo
!= nullptr, false);
229 for (int i
=0; argv
[i
] != nullptr; ++i
)
233 // If there are spaces, surround it with quotes. If there are quotes,
234 // replace them with \" so that CommandLineToArgv will correctly parse them.
235 if (arg
.containsAnyOf("\" "))
236 arg
= arg
.replace("\"", "\\\"").quoted();
238 command
<< arg
<< ' ';
241 command
= command
.trim();
243 STARTUPINFOA startupInfo
;
244 carla_zeroStruct(startupInfo
);
245 startupInfo
.cb
= sizeof(startupInfo
);
247 if (::CreateProcessA(nullptr, const_cast<LPSTR
>(command
.toRawUTF8()),
248 nullptr, nullptr, TRUE
, CREATE_NO_WINDOW
| CREATE_UNICODE_ENVIRONMENT
,
249 nullptr, nullptr, &startupInfo
, processInfo
) != FALSE
)
252 carla_stderr2("startProcess failed, error was: %u", ::GetLastError());
257 bool waitForClientConnect(const HANDLE pipe
, const HANDLE event
, const HANDLE process
, const uint32_t timeOutMilliseconds
) noexcept
259 CARLA_SAFE_ASSERT_RETURN(pipe
!= INVALID_PIPE_VALUE
, false);
260 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0, false);
265 carla_zeroStruct(ov
);
268 const uint32_t timeoutEnd
= carla_gettime_ms() + timeOutMilliseconds
;
272 if (::ConnectNamedPipe(pipe
, &ov
))
274 if (! ::GetOverlappedResult(pipe
, &ov
, &dw
, FALSE
))
276 carla_stderr2("ConnectNamedPipe GetOverlappedResult failed, error was: %u", ::GetLastError());
283 const DWORD err
= ::GetLastError();
287 case ERROR_PIPE_CONNECTED
:
290 case ERROR_IO_PENDING
:
291 if (! waitForAsyncObject(event
, process
))
293 carla_stderr2("ConnectNamedPipe waitForAsyncObject failed, error was: %u", ::GetLastError());
297 if (! ::GetOverlappedResult(pipe
, &ov
, &dw
, FALSE
))
299 carla_stderr2("ConnectNamedPipe GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
305 case ERROR_PIPE_LISTENING
:
306 if (carla_gettime_ms() < timeoutEnd
)
311 carla_stderr2("ConnectNamedPipe listening timed out");
315 carla_stderr2("ConnectNamedPipe failed, error was: %u", err
);
324 bool startProcess(const char* const argv
[], pid_t
& pidinst
) noexcept
326 const CarlaScopedEnvVar
sev1("LD_LIBRARY_PATH", nullptr);
327 const CarlaScopedEnvVar
sev2("LD_PRELOAD", nullptr);
330 #pragma clang diagnostic push
331 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
333 const pid_t ret
= pidinst
= vfork();
335 #pragma clang diagnostic pop
340 case 0: { // child process
341 execvp(argv
[0], const_cast<char* const*>(argv
));
343 CarlaString
error(std::strerror(errno
));
344 carla_stderr2("exec failed: %s", error
.buffer());
346 _exit(1); // this is not noexcept safe but doesn't matter anyway
350 CarlaString
error(std::strerror(errno
));
351 carla_stderr2("vfork() failed: %s", error
.buffer());
359 // -----------------------------------------------------------------------
360 // waitForClientFirstMessage
364 bool waitForClientFirstMessage(const P
& pipe
, void* const ovRecv
, void* const process
, const uint32_t timeOutMilliseconds
) noexcept
366 CARLA_SAFE_ASSERT_RETURN(pipe
!= INVALID_PIPE_VALUE
, false);
367 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0, false);
371 const uint32_t timeoutEnd
= carla_gettime_ms() + timeOutMilliseconds
;
374 if (! waitForClientConnect(pipe
, (HANDLE
)ovRecv
, (HANDLE
)process
, timeOutMilliseconds
))
382 ret
= ReadFileWin32(pipe
, (HANDLE
)ovRecv
, &c
, 1);
384 ret
= ::read(pipe
, &c
, 1);
386 } CARLA_SAFE_EXCEPTION_RETURN("read pipe", false);
394 carla_stderr("waitForClientFirstMessage() - read has wrong first char '%c'", c
);return false;
397 case -1: // failed to read
399 if (::GetLastError() == ERROR_NO_DATA
)
404 if (carla_gettime_ms() < timeoutEnd
)
409 carla_stderr("waitForClientFirstMessage() - read timed out");
414 carla_stderr("waitForClientFirstMessage() - read failed");
416 CarlaString
error(std::strerror(errno
));
417 carla_stderr("waitForClientFirstMessage() - read failed: %s", error
.buffer());
423 carla_stderr("waitForClientFirstMessage() - read returned %i", int(ret
));
429 (void)ovRecv
; (void)process
;
432 // -----------------------------------------------------------------------
433 // waitForChildToStop / waitForProcessToStop
437 bool waitForProcessToStop(const HANDLE process
, const uint32_t timeOutMilliseconds
, bool sendTerminate
) noexcept
439 CARLA_SAFE_ASSERT_RETURN(process
!= INVALID_HANDLE_VALUE
, false);
440 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0, false);
442 const uint32_t timeoutEnd
= carla_gettime_ms() + timeOutMilliseconds
;
446 switch (::WaitForSingleObject(process
, 0))
455 sendTerminate
= false;
456 ::TerminateProcess(process
, 15);
459 if (carla_gettime_ms() >= timeoutEnd
)
469 void waitForProcessToStopOrKillIt(const HANDLE process
, const uint32_t timeOutMilliseconds
) noexcept
471 CARLA_SAFE_ASSERT_RETURN(process
!= INVALID_HANDLE_VALUE
,);
472 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0,);
474 if (! waitForProcessToStop(process
, timeOutMilliseconds
, true))
476 carla_stderr("waitForProcessToStopOrKillIt() - process didn't stop, force termination");
478 if (::TerminateProcess(process
, 9) != FALSE
)
480 // wait for process to stop
481 waitForProcessToStop(process
, timeOutMilliseconds
, false);
487 bool waitForChildToStop(const pid_t pid
, const uint32_t timeOutMilliseconds
, bool sendTerminate
) noexcept
489 CARLA_SAFE_ASSERT_RETURN(pid
> 0, false);
490 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0, false);
493 const uint32_t timeoutEnd
= carla_gettime_ms() + timeOutMilliseconds
;
498 ret
= ::waitpid(pid
, nullptr, WNOHANG
);
499 } CARLA_SAFE_EXCEPTION_BREAK("waitpid");
506 // success, child doesn't exist
511 CarlaString
error(std::strerror(errno
));
512 carla_stderr("waitForChildToStop() - waitpid failed: %s", error
.buffer());
520 sendTerminate
= false;
521 ::kill(pid
, SIGTERM
);
523 if (carla_gettime_ms() < timeoutEnd
)
528 carla_stderr("waitForChildToStop() - timed out");
539 carla_stderr("waitForChildToStop() - got wrong pid %i (requested was %i)", int(ret
), int(pid
));
551 void waitForChildToStopOrKillIt(pid_t
& pid
, const uint32_t timeOutMilliseconds
) noexcept
553 CARLA_SAFE_ASSERT_RETURN(pid
> 0,);
554 CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds
> 0,);
556 if (! waitForChildToStop(pid
, timeOutMilliseconds
, true))
558 carla_stderr("waitForChildToStopOrKillIt() - process didn't stop, force killing");
560 if (::kill(pid
, SIGKILL
) != -1)
562 // wait for killing to take place
563 waitForChildToStop(pid
, timeOutMilliseconds
, false);
567 CarlaString
error(std::strerror(errno
));
568 carla_stderr("waitForChildToStopOrKillIt() - kill failed: %s", error
.buffer());
574 // -----------------------------------------------------------------------
576 struct CarlaPipeCommon::PrivateData
{
579 PROCESS_INFORMATION processInfo
;
590 // read functions must only be called in context of idlePipe()
593 // the client side is closing down, only waiting for response from server
594 bool clientClosingDown
;
596 // other side of pipe has closed
599 // print error only once
600 bool lastMessageFailed
;
606 CarlaMutex writeLock
;
608 // temporary buffers for _readline()
609 mutable char tmpBuf
[0xffff];
610 mutable CarlaString tmpStr
;
612 PrivateData() noexcept
618 pipeRecv(INVALID_PIPE_VALUE
),
619 pipeSend(INVALID_PIPE_VALUE
),
621 clientClosingDown(false),
623 lastMessageFailed(false),
630 carla_zeroStruct(processInfo
);
631 processInfo
.hProcess
= INVALID_HANDLE_VALUE
;
632 processInfo
.hThread
= INVALID_HANDLE_VALUE
;
634 ovRecv
= ::CreateEvent(nullptr, FALSE
, FALSE
, nullptr);
635 ovSend
= ::CreateEvent(nullptr, FALSE
, FALSE
, nullptr);
638 carla_zeroChars(tmpBuf
, 0xffff);
641 CARLA_DECLARE_NON_COPYABLE(PrivateData
)
644 // -----------------------------------------------------------------------
646 CarlaPipeCommon::CarlaPipeCommon() noexcept
647 : pData(new PrivateData())
649 carla_debug("CarlaPipeCommon::CarlaPipeCommon()");
652 CarlaPipeCommon::~CarlaPipeCommon() /*noexcept*/
654 carla_debug("CarlaPipeCommon::~CarlaPipeCommon()");
659 // -------------------------------------------------------------------
661 bool CarlaPipeCommon::isPipeRunning() const noexcept
663 return (pData
->pipeRecv
!= INVALID_PIPE_VALUE
&& pData
->pipeSend
!= INVALID_PIPE_VALUE
&& ! pData
->pipeClosed
);
666 void CarlaPipeCommon::idlePipe(const bool onlyOnce
) noexcept
673 const char* const msg
= _readline(true, 0, readSucess
);
680 pData
->isReading
= true;
682 if (std::strcmp(msg
, "__carla-quit__") == 0)
684 pData
->pipeClosed
= true;
686 else if (! pData
->clientClosingDown
)
690 } CARLA_SAFE_EXCEPTION("msgReceived");
693 pData
->isReading
= false;
695 std::free(const_cast<char*>(msg
));
697 if (onlyOnce
|| pData
->pipeRecv
== INVALID_PIPE_VALUE
)
702 // -------------------------------------------------------------------
704 void CarlaPipeCommon::lockPipe() const noexcept
706 pData
->writeLock
.lock();
709 bool CarlaPipeCommon::tryLockPipe() const noexcept
711 return pData
->writeLock
.tryLock();
714 void CarlaPipeCommon::unlockPipe() const noexcept
716 pData
->writeLock
.unlock();
719 CarlaMutex
& CarlaPipeCommon::getPipeLock() const noexcept
721 return pData
->writeLock
;
724 // -------------------------------------------------------------------
726 bool CarlaPipeCommon::readNextLineAsBool(bool& value
) const noexcept
728 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
730 if (const char* const msg
= _readlineblock(false))
732 value
= (std::strcmp(msg
, "true") == 0);
739 bool CarlaPipeCommon::readNextLineAsByte(uint8_t& value
) const noexcept
741 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
743 if (const char* const msg
= _readlineblock(false))
745 const int asint
= std::atoi(msg
);
747 if (asint
>= 0 && asint
<= 0xFF)
749 value
= static_cast<uint8_t>(asint
);
757 bool CarlaPipeCommon::readNextLineAsInt(int32_t& value
) const noexcept
759 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
761 if (const char* const msg
= _readlineblock(false))
763 value
= std::atoi(msg
);
770 bool CarlaPipeCommon::readNextLineAsUInt(uint32_t& value
) const noexcept
772 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
774 if (const char* const msg
= _readlineblock(false))
776 #if (defined(__WORDSIZE) && __WORDSIZE < 64) || (defined(__SIZE_WIDTH__) && __SIZE_WIDTH__ < 64) || \
777 defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
778 const long long aslong
= std::atoll(msg
);
780 const long aslong
= std::atol(msg
);
785 value
= static_cast<uint32_t>(aslong
);
793 bool CarlaPipeCommon::readNextLineAsLong(int64_t& value
) const noexcept
795 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
797 if (const char* const msg
= _readlineblock(false))
799 value
= std::atol(msg
);
806 bool CarlaPipeCommon::readNextLineAsULong(uint64_t& value
) const noexcept
808 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
810 if (const char* const msg
= _readlineblock(false))
812 const int64_t asint64
= std::atol(msg
);
816 value
= static_cast<uint64_t>(asint64
);
824 bool CarlaPipeCommon::readNextLineAsFloat(float& value
) const noexcept
826 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
828 if (const char* const msg
= _readlineblock(false))
831 const CarlaScopedLocale csl
;
832 value
= static_cast<float>(std::atof(msg
));
840 bool CarlaPipeCommon::readNextLineAsDouble(double& value
) const noexcept
842 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
844 if (const char* const msg
= _readlineblock(false))
847 const CarlaScopedLocale csl
;
848 value
= std::atof(msg
);
856 bool CarlaPipeCommon::readNextLineAsString(const char*& value
, const bool allocateString
, uint32_t size
) const noexcept
858 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, false);
863 if (const char* const msg
= _readlineblock(allocateString
, static_cast<uint16_t>(size
)))
872 char* CarlaPipeCommon::readNextLineAsString() const noexcept
874 CARLA_SAFE_ASSERT_RETURN(pData
->isReading
, nullptr);
876 return const_cast<char*>(_readlineblock(true, 0));
879 // -------------------------------------------------------------------
880 // must be locked before calling
882 bool CarlaPipeCommon::writeMessage(const char* const msg
) const noexcept
884 CARLA_SAFE_ASSERT_RETURN(msg
!= nullptr && msg
[0] != '\0', false);
886 if (pData
->pipeClosed
)
889 const std::size_t size(std::strlen(msg
));
890 CARLA_SAFE_ASSERT_RETURN(size
> 0, false);
891 CARLA_SAFE_ASSERT_RETURN(msg
[size
-1] == '\n', false);
893 return _writeMsgBuffer(msg
, size
);
896 bool CarlaPipeCommon::writeMessage(const char* const msg
, std::size_t size
) const noexcept
898 CARLA_SAFE_ASSERT_RETURN(msg
!= nullptr && msg
[0] != '\0', false);
899 CARLA_SAFE_ASSERT_RETURN(size
> 0, false);
900 CARLA_SAFE_ASSERT_RETURN(msg
[size
-1] == '\n', false);
902 if (pData
->pipeClosed
)
905 return _writeMsgBuffer(msg
, size
);
908 bool CarlaPipeCommon::writeAndFixMessage(const char* const msg
) const noexcept
910 CARLA_SAFE_ASSERT_RETURN(msg
!= nullptr, false);
912 if (pData
->pipeClosed
)
915 const std::size_t size(std::strlen(msg
));
917 char* const fixedMsg
= static_cast<char*>(std::malloc(size
+2));
918 CARLA_SAFE_ASSERT_RETURN(fixedMsg
!= nullptr, false);
922 std::strcpy(fixedMsg
, msg
);
924 for (std::size_t i
=0; i
<size
; ++i
)
926 if (fixedMsg
[i
] == '\n')
930 if (fixedMsg
[size
-1] == '\r')
932 fixedMsg
[size
-1] = '\n';
933 fixedMsg
[size
] = '\0';
934 fixedMsg
[size
+1] = '\0';
938 fixedMsg
[size
] = '\n';
939 fixedMsg
[size
+1] = '\0';
948 const bool ret
= _writeMsgBuffer(fixedMsg
, size
+1);
953 bool CarlaPipeCommon::writeEmptyMessage() const noexcept
955 if (pData
->pipeClosed
)
958 return _writeMsgBuffer("\n", 1);
961 bool CarlaPipeCommon::syncMessages() const noexcept
963 CARLA_SAFE_ASSERT_RETURN(pData
->pipeSend
!= INVALID_PIPE_VALUE
, false);
965 #if defined(CARLA_OS_LINUX) || defined(CARLA_OS_GNU_HURD)
966 # if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2014
967 // the only call that seems to do something
968 return ::syncfs(pData
->pipeSend
) == 0;
970 #elif 0 // defined(CARLA_OS_WIN)
971 // FIXME causes issues
973 return (::FlushFileBuffers(pData
->pipeSend
) != FALSE
);
974 } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
980 // -------------------------------------------------------------------
982 bool CarlaPipeCommon::writeErrorMessage(const char* const error
) const noexcept
984 CARLA_SAFE_ASSERT_RETURN(error
!= nullptr && error
[0] != '\0', false);
986 const CarlaMutexLocker
cml(pData
->writeLock
);
988 if (! _writeMsgBuffer("error\n", 6))
990 if (! writeAndFixMessage(error
))
997 bool CarlaPipeCommon::writeControlMessage(const uint32_t index
, const float value
, const bool withWriteLock
) const noexcept
1001 const CarlaMutexLocker
cml(pData
->writeLock
);
1002 return writeControlMessage(index
, value
, false);
1006 tmpBuf
[0xfe] = '\0';
1008 if (! _writeMsgBuffer("control\n", 8))
1011 std::snprintf(tmpBuf
, 0xfe, "%i\n", index
);
1012 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1016 const CarlaScopedLocale csl
;
1017 std::snprintf(tmpBuf
, 0xfe, "%.12g\n", static_cast<double>(value
));
1020 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1027 bool CarlaPipeCommon::writeConfigureMessage(const char* const key
, const char* const value
) const noexcept
1029 CARLA_SAFE_ASSERT_RETURN(key
!= nullptr && key
[0] != '\0', false);
1030 CARLA_SAFE_ASSERT_RETURN(value
!= nullptr, false);
1032 const CarlaMutexLocker
cml(pData
->writeLock
);
1034 if (! _writeMsgBuffer("configure\n", 10))
1036 if (! writeAndFixMessage(key
))
1038 if (! writeAndFixMessage(value
))
1045 bool CarlaPipeCommon::writeProgramMessage(const uint32_t index
) const noexcept
1048 tmpBuf
[0xfe] = '\0';
1050 const CarlaMutexLocker
cml(pData
->writeLock
);
1052 if (! _writeMsgBuffer("program\n", 8))
1055 std::snprintf(tmpBuf
, 0xfe, "%i\n", index
);
1056 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1063 bool CarlaPipeCommon::writeProgramMessage(const uint8_t channel
, const uint32_t bank
, const uint32_t program
) const noexcept
1066 tmpBuf
[0xfe] = '\0';
1068 const CarlaMutexLocker
cml(pData
->writeLock
);
1070 if (! _writeMsgBuffer("program\n", 8))
1073 std::snprintf(tmpBuf
, 0xfe, "%i\n", channel
);
1074 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1077 std::snprintf(tmpBuf
, 0xfe, "%i\n", bank
);
1078 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1081 std::snprintf(tmpBuf
, 0xfe, "%i\n", program
);
1082 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1089 bool CarlaPipeCommon::writeMidiProgramMessage(const uint32_t bank
, const uint32_t program
) const noexcept
1092 tmpBuf
[0xfe] = '\0';
1094 const CarlaMutexLocker
cml(pData
->writeLock
);
1096 if (! _writeMsgBuffer("midiprogram\n", 12))
1099 std::snprintf(tmpBuf
, 0xfe, "%i\n", bank
);
1100 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1103 std::snprintf(tmpBuf
, 0xfe, "%i\n", program
);
1104 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1111 bool CarlaPipeCommon::writeReloadProgramsMessage(const int32_t index
) const noexcept
1114 tmpBuf
[0xfe] = '\0';
1116 const CarlaMutexLocker
cml(pData
->writeLock
);
1118 if (! _writeMsgBuffer("reloadprograms\n", 15))
1121 std::snprintf(tmpBuf
, 0xfe, "%i\n", index
);
1122 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1129 bool CarlaPipeCommon::writeMidiNoteMessage(const bool onOff
, const uint8_t channel
, const uint8_t note
, const uint8_t velocity
) const noexcept
1131 CARLA_SAFE_ASSERT_RETURN(channel
< MAX_MIDI_CHANNELS
, false);
1132 CARLA_SAFE_ASSERT_RETURN(note
< MAX_MIDI_NOTE
, false);
1133 CARLA_SAFE_ASSERT_RETURN(velocity
< MAX_MIDI_VALUE
, false);
1136 tmpBuf
[0xfe] = '\0';
1138 const CarlaMutexLocker
cml(pData
->writeLock
);
1140 if (! _writeMsgBuffer("note\n", 5))
1143 std::snprintf(tmpBuf
, 0xfe, "%s\n", bool2str(onOff
));
1144 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1147 std::snprintf(tmpBuf
, 0xfe, "%i\n", channel
);
1148 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1151 std::snprintf(tmpBuf
, 0xfe, "%i\n", note
);
1152 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1155 std::snprintf(tmpBuf
, 0xfe, "%i\n", velocity
);
1156 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1163 bool CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index
, const LV2_Atom
* const atom
) const noexcept
1165 CARLA_SAFE_ASSERT_RETURN(atom
!= nullptr, false);
1168 tmpBuf
[0xfe] = '\0';
1170 const uint32_t atomTotalSize(lv2_atom_total_size(atom
));
1171 CarlaString
base64atom(CarlaString::asBase64(atom
, atomTotalSize
));
1173 const CarlaMutexLocker
cml(pData
->writeLock
);
1175 if (! _writeMsgBuffer("atom\n", 5))
1178 std::snprintf(tmpBuf
, 0xfe, "%i\n", index
);
1179 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1182 std::snprintf(tmpBuf
, 0xfe, "%i\n", atomTotalSize
);
1183 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1186 std::snprintf(tmpBuf
, 0xfe, "%lu\n", static_cast<long unsigned>(base64atom
.length()));
1187 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1190 if (! writeAndFixMessage(base64atom
.buffer()))
1197 bool CarlaPipeCommon::writeLv2ParameterMessage(const char* const uri
, const float value
, const bool withWriteLock
) const noexcept
1201 const CarlaMutexLocker
cml(pData
->writeLock
);
1202 return writeLv2ParameterMessage(uri
, value
, false);
1206 tmpBuf
[0xfe] = '\0';
1208 if (! _writeMsgBuffer("parameter\n", 10))
1211 if (! writeAndFixMessage(uri
))
1215 const CarlaScopedLocale csl
;
1216 std::snprintf(tmpBuf
, 0xfe, "%.12g\n", static_cast<double>(value
));
1219 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1226 bool CarlaPipeCommon::writeLv2UridMessage(const uint32_t urid
, const char* const uri
) const noexcept
1228 CARLA_SAFE_ASSERT_RETURN(urid
!= 0, false);
1229 CARLA_SAFE_ASSERT_RETURN(uri
!= nullptr && uri
[0] != '\0', false);
1232 tmpBuf
[0xfe] = '\0';
1234 const CarlaMutexLocker
cml(pData
->writeLock
);
1236 if (! _writeMsgBuffer("urid\n", 5))
1239 std::snprintf(tmpBuf
, 0xfe, "%i\n", urid
);
1240 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1243 std::snprintf(tmpBuf
, 0xfe, "%lu\n", static_cast<long unsigned>(std::strlen(uri
)));
1244 if (! _writeMsgBuffer(tmpBuf
, std::strlen(tmpBuf
)))
1247 if (! writeAndFixMessage(uri
))
1254 // -------------------------------------------------------------------
1257 const char* CarlaPipeCommon::_readline(const bool allocReturn
, const uint16_t size
, bool& readSucess
) const noexcept
1259 CARLA_SAFE_ASSERT_RETURN(pData
->pipeRecv
!= INVALID_PIPE_VALUE
, nullptr);
1262 char* ptr
= pData
->tmpBuf
;
1264 bool tooBig
= false;
1266 pData
->tmpStr
.clear();
1268 if (size
== 0 || size
== 1)
1270 for (int i
=0; i
<0xfffe; ++i
)
1274 ret
= ReadFileWin32(pData
->pipeRecv
, pData
->ovRecv
, &c
, 1);
1276 ret
= ::read(pData
->pipeRecv
, &c
, 1);
1278 } CARLA_SAFE_EXCEPTION_BREAK("CarlaPipeCommon::readline() - read");
1299 pData
->tmpStr
+= pData
->tmpBuf
;
1300 ptr
= pData
->tmpBuf
;
1306 uint16_t remaining
= size
;
1313 ret
= ReadFileWin32(pData
->pipeRecv
, pData
->ovRecv
, ptr
, remaining
);
1315 ret
= ::read(pData
->pipeRecv
, ptr
, remaining
);
1317 } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - read", nullptr);
1319 if (ret
== -1 && errno
== EAGAIN
)
1322 CARLA_SAFE_ASSERT_INT2_RETURN(ret
> 0, ret
, remaining
, nullptr);
1323 CARLA_SAFE_ASSERT_INT2_RETURN(ret
<= (ssize_t
)remaining
, ret
, remaining
, nullptr);
1325 for (ssize_t i
=0; i
<ret
; ++i
)
1333 remaining
= static_cast<uint16_t>(remaining
- ret
);
1342 pData
->tmpStr
= pData
->tmpBuf
;
1343 return pData
->tmpStr
.releaseBufferPointer();
1346 return pData
->tmpBuf
;
1350 if (ptr
!= pData
->tmpBuf
)
1354 if (! allocReturn
&& ! tooBig
)
1357 return pData
->tmpBuf
;
1360 pData
->tmpStr
+= pData
->tmpBuf
;
1362 else if (pData
->tmpStr
.isEmpty() && ret
!= 1)
1370 if (! allocReturn
&& ! tooBig
)
1371 return pData
->tmpBuf
;
1373 return allocReturn
? pData
->tmpStr
.releaseBufferPointer() : pData
->tmpStr
.buffer();
1376 const char* CarlaPipeCommon::_readlineblock(const bool allocReturn
,
1377 const uint16_t size
,
1378 const uint32_t timeOutMilliseconds
) const noexcept
1380 const uint32_t timeoutEnd
= carla_gettime_ms() + timeOutMilliseconds
;
1386 const char* const msg
= _readline(allocReturn
, size
, readSucess
);
1391 if (carla_gettime_ms() >= timeoutEnd
)
1397 static const bool testingForValgrind
= std::getenv("CARLA_VALGRIND_TEST") != nullptr;
1399 if (testingForValgrind
)
1401 const uint32_t timeoutEnd2
= carla_gettime_ms() + 1000;
1406 const char* const msg
= _readline(allocReturn
, size
, readSucess
);
1411 if (carla_gettime_ms() >= timeoutEnd2
)
1418 carla_stderr("readlineblock timed out");
1422 bool CarlaPipeCommon::_writeMsgBuffer(const char* const msg
, const std::size_t size
) const noexcept
1424 if (pData
->pipeClosed
)
1427 if (pData
->pipeSend
== INVALID_PIPE_VALUE
)
1429 carla_stderr2("CarlaPipe write error, isServer:%s, message was:\n%s", bool2str(pData
->isServer
), msg
);
1437 ret
= WriteFileWin32(pData
->pipeSend
, pData
->ovSend
, msg
, static_cast<DWORD
>(size
));
1439 ret
= ::write(pData
->pipeSend
, msg
, size
);
1441 } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
1446 pData
->pipeClosed
= true;
1451 if (ret
== static_cast<ssize_t
>(size
))
1453 if (pData
->lastMessageFailed
)
1454 pData
->lastMessageFailed
= false;
1458 if (! pData
->lastMessageFailed
)
1460 pData
->lastMessageFailed
= true;
1462 "CarlaPipeCommon::_writeMsgBuffer(..., " P_SIZE
") - failed with " P_SSIZE
" (%s), message was:\n%s",
1463 size
, ret
, bool2str(pData
->isServer
), msg
);
1469 // -----------------------------------------------------------------------
1471 CarlaPipeServer::CarlaPipeServer() noexcept
1474 carla_debug("CarlaPipeServer::CarlaPipeServer()");
1475 pData
->isServer
= true;
1478 CarlaPipeServer::~CarlaPipeServer() /*noexcept*/
1480 carla_debug("CarlaPipeServer::~CarlaPipeServer()");
1482 stopPipeServer(5*1000);
1485 uintptr_t CarlaPipeServer::getPID() const noexcept
1487 #ifndef CARLA_OS_WIN
1488 return static_cast<uintptr_t>(pData
->pid
);
1494 // --------------------------------------------------------------------------------------------------------------------
1496 bool CarlaPipeServer::startPipeServer(const char* const helperTool
,
1497 const char* const filename
,
1498 const char* const arg1
,
1499 const char* const arg2
,
1501 int timeOutMilliseconds
) noexcept
1503 CARLA_SAFE_ASSERT_RETURN(pData
->pipeRecv
== INVALID_PIPE_VALUE
, false);
1504 CARLA_SAFE_ASSERT_RETURN(pData
->pipeSend
== INVALID_PIPE_VALUE
, false);
1506 CARLA_SAFE_ASSERT_RETURN(pData
->processInfo
.hThread
== INVALID_HANDLE_VALUE
, false);
1507 CARLA_SAFE_ASSERT_RETURN(pData
->processInfo
.hProcess
== INVALID_HANDLE_VALUE
, false);
1509 CARLA_SAFE_ASSERT_RETURN(pData
->pid
== -1, false);
1511 CARLA_SAFE_ASSERT_RETURN(filename
!= nullptr && filename
[0] != '\0', false);
1512 CARLA_SAFE_ASSERT_RETURN(arg1
!= nullptr, false);
1513 CARLA_SAFE_ASSERT_RETURN(arg2
!= nullptr, false);
1514 carla_debug("CarlaPipeServer::startPipeServer(\"%s\", \"%s\", \"%s\")", filename
, arg1
, arg2
);
1516 if (timeOutMilliseconds
< 0)
1517 timeOutMilliseconds
= 10 * 1000;
1519 char pipeRecvServerStr
[100+1];
1520 char pipeSendServerStr
[100+1];
1521 char pipeRecvClientStr
[100+1];
1522 char pipeSendClientStr
[100+1];
1524 pipeRecvServerStr
[100] = '\0';
1525 pipeSendServerStr
[100] = '\0';
1526 pipeRecvClientStr
[100] = '\0';
1527 pipeSendClientStr
[100] = '\0';
1529 const CarlaMutexLocker
cml(pData
->writeLock
);
1531 //-----------------------------------------------------------------------------------------------------------------
1535 HANDLE pipe1
, pipe2
;
1537 std::srand(static_cast<uint
>(std::time(nullptr)));
1539 static ulong sCounter
= 0;
1542 const int randint
= std::rand();
1544 std::snprintf(pipeRecvServerStr
, 100, "\\\\.\\pipe\\carla-pipe1-%i-%li", randint
, sCounter
);
1545 std::snprintf(pipeSendServerStr
, 100, "\\\\.\\pipe\\carla-pipe2-%i-%li", randint
, sCounter
);
1546 std::snprintf(pipeRecvClientStr
, 100, "ignored");
1547 std::snprintf(pipeSendClientStr
, 100, "ignored");
1549 SECURITY_ATTRIBUTES sa
;
1550 carla_zeroStruct(sa
);
1551 sa
.nLength
= sizeof(sa
);
1552 sa
.bInheritHandle
= TRUE
;
1554 pipe1
= ::CreateNamedPipeA(pipeRecvServerStr
, PIPE_ACCESS_DUPLEX
|FILE_FLAG_FIRST_PIPE_INSTANCE
|FILE_FLAG_OVERLAPPED
, PIPE_TYPE_BYTE
|PIPE_READMODE_BYTE
, 1, size
, size
, 0, &sa
);
1556 if (pipe1
== INVALID_HANDLE_VALUE
)
1558 fail("pipe creation failed");
1562 pipe2
= ::CreateNamedPipeA(pipeSendServerStr
, PIPE_ACCESS_DUPLEX
|FILE_FLAG_FIRST_PIPE_INSTANCE
|FILE_FLAG_OVERLAPPED
, PIPE_TYPE_BYTE
|PIPE_READMODE_BYTE
, 1, size
, size
, 0, &sa
);
1564 if (pipe2
== INVALID_HANDLE_VALUE
)
1566 try { ::CloseHandle(pipe1
); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1)");
1567 fail("pipe creation failed");
1571 const HANDLE pipeRecvClient
= pipe2
;
1572 const HANDLE pipeSendClient
= pipe1
;
1574 int pipe1
[2]; // read by server, written by client
1575 int pipe2
[2]; // read by client, written by server
1577 if (::pipe(pipe1
) != 0)
1579 fail("pipe1 creation failed");
1583 if (::pipe(pipe2
) != 0)
1585 try { ::close(pipe1
[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
1586 try { ::close(pipe1
[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
1587 fail("pipe2 creation failed");
1591 /* */ int pipeRecvServer
= pipe1
[0];
1592 /* */ int pipeSendServer
= pipe2
[1];
1593 const int pipeRecvClient
= pipe2
[0];
1594 const int pipeSendClient
= pipe1
[1];
1596 std::snprintf(pipeRecvServerStr
, 100, "%i", pipeRecvServer
);
1597 std::snprintf(pipeSendServerStr
, 100, "%i", pipeSendServer
);
1598 std::snprintf(pipeRecvClientStr
, 100, "%i", pipeRecvClient
);
1599 std::snprintf(pipeSendClientStr
, 100, "%i", pipeSendClient
);
1601 //-----------------------------------------------------------------------------------------------------------------
1602 // set size, non-fatal
1604 # ifdef CARLA_OS_LINUX
1606 ::fcntl(pipeRecvClient
, F_SETPIPE_SZ
, size
);
1607 } CARLA_SAFE_EXCEPTION("Set pipe size");
1610 ::fcntl(pipeRecvServer
, F_SETPIPE_SZ
, size
);
1611 } CARLA_SAFE_EXCEPTION("Set pipe size");
1614 //-----------------------------------------------------------------------------------------------------------------
1620 ret
= ::fcntl(pipeRecvClient
, F_SETFL
, ::fcntl(pipeRecvClient
, F_GETFL
) | O_NONBLOCK
);
1623 fail("failed to set pipe as non-block");
1629 ret
= ::fcntl(pipeRecvServer
, F_SETFL
, ::fcntl(pipeRecvServer
, F_GETFL
) | O_NONBLOCK
);
1632 fail("failed to set pipe as non-block");
1638 try { ::close(pipe1
[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
1639 try { ::close(pipe1
[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
1640 try { ::close(pipe2
[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
1641 try { ::close(pipe2
[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
1646 //-----------------------------------------------------------------------------------------------------------------
1649 const char* argv
[9] = {};
1652 if (helperTool
!= nullptr)
1653 argv
[i
++] = helperTool
;
1655 //-----------------------------------------------------------------------------------------------------------------
1656 // argv[0] => filename
1658 argv
[i
++] = filename
;
1660 //-----------------------------------------------------------------------------------------------------------------
1661 // argv[1-2] => args
1666 //-----------------------------------------------------------------------------------------------------------------
1667 // argv[3-6] => pipes
1669 argv
[i
++] = pipeRecvServerStr
;
1670 argv
[i
++] = pipeSendServerStr
;
1671 argv
[i
++] = pipeRecvClientStr
;
1672 argv
[i
++] = pipeSendClientStr
;
1674 //-----------------------------------------------------------------------------------------------------------------
1678 if (! startProcess(argv
, &pData
->processInfo
))
1680 carla_zeroStruct(pData
->processInfo
);
1681 pData
->processInfo
.hProcess
= INVALID_HANDLE_VALUE
;
1682 pData
->processInfo
.hThread
= INVALID_HANDLE_VALUE
;
1683 try { ::CloseHandle(pipe1
); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1)");
1684 try { ::CloseHandle(pipe2
); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2)");
1688 // just to make sure
1689 CARLA_SAFE_ASSERT(pData
->processInfo
.hThread
!= INVALID_HANDLE_VALUE
);
1690 CARLA_SAFE_ASSERT(pData
->processInfo
.hProcess
!= INVALID_HANDLE_VALUE
);
1692 if (! startProcess(argv
, pData
->pid
))
1695 try { ::close(pipe1
[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
1696 try { ::close(pipe1
[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
1697 try { ::close(pipe2
[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
1698 try { ::close(pipe2
[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
1699 fail("startProcess() failed");
1703 //-----------------------------------------------------------------------------------------------------------------
1704 // close duplicated handles used by the client
1706 try { ::close(pipeRecvServer
); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
1707 try { ::close(pipeSendServer
); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
1710 //-----------------------------------------------------------------------------------------------------------------
1711 // wait for client to say something
1714 void* const ovRecv
= pData
->ovRecv
;
1715 void* const process
= pData
->processInfo
.hProcess
;
1717 void* const ovRecv
= nullptr;
1718 void* const process
= nullptr;
1721 if (waitForClientFirstMessage(pipeRecvClient
, ovRecv
, process
, timeOutMilliseconds
))
1723 pData
->pipeRecv
= pipeRecvClient
;
1724 pData
->pipeSend
= pipeSendClient
;
1725 pData
->pipeClosed
= false;
1726 carla_debug("ALL OK!");
1730 //-----------------------------------------------------------------------------------------------------------------
1731 // failed to set non-block or get first child message, cannot continue
1734 if (::TerminateProcess(pData
->processInfo
.hProcess
, 9) != FALSE
)
1736 // wait for process to stop
1737 waitForProcessToStop(pData
->processInfo
.hProcess
, 2*1000, false);
1740 // clear pData->processInfo
1741 try { ::CloseHandle(pData
->processInfo
.hThread
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
1742 try { ::CloseHandle(pData
->processInfo
.hProcess
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
1743 carla_zeroStruct(pData
->processInfo
);
1744 pData
->processInfo
.hProcess
= INVALID_HANDLE_VALUE
;
1745 pData
->processInfo
.hThread
= INVALID_HANDLE_VALUE
;
1747 if (::kill(pData
->pid
, SIGKILL
) != -1)
1749 // wait for killing to take place
1750 waitForChildToStop(pData
->pid
, 2*1000, false);
1755 //-----------------------------------------------------------------------------------------------------------------
1759 try { ::CloseHandle(pipeRecvClient
); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvClient)");
1760 try { ::CloseHandle(pipeSendClient
); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendClient)");
1762 try { ::close (pipeRecvClient
); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
1763 try { ::close (pipeSendClient
); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
1769 (void)size
; (void)ovRecv
; (void)process
;
1772 bool CarlaPipeServer::startPipeServer(const char* const filename
,
1773 const char* const arg1
,
1774 const char* const arg2
,
1776 const int timeOutMilliseconds
) noexcept
1778 return startPipeServer(nullptr, filename
, arg1
, arg2
, size
, timeOutMilliseconds
);
1781 void CarlaPipeServer::stopPipeServer(const uint32_t timeOutMilliseconds
) noexcept
1783 carla_debug("CarlaPipeServer::stopPipeServer(%i)", timeOutMilliseconds
);
1786 if (pData
->processInfo
.hThread
!= INVALID_HANDLE_VALUE
|| pData
->processInfo
.hProcess
!= INVALID_HANDLE_VALUE
)
1788 const CarlaMutexLocker
cml(pData
->writeLock
);
1790 if (pData
->pipeSend
!= INVALID_PIPE_VALUE
&& ! pData
->pipeClosed
)
1792 if (_writeMsgBuffer("__carla-quit__\n", 15))
1796 waitForProcessToStopOrKillIt(pData
->processInfo
.hProcess
, timeOutMilliseconds
);
1797 try { ::CloseHandle(pData
->processInfo
.hThread
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
1798 try { ::CloseHandle(pData
->processInfo
.hProcess
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
1799 carla_zeroStruct(pData
->processInfo
);
1800 pData
->processInfo
.hProcess
= INVALID_HANDLE_VALUE
;
1801 pData
->processInfo
.hThread
= INVALID_HANDLE_VALUE
;
1804 if (pData
->pid
!= -1)
1806 const CarlaMutexLocker
cml(pData
->writeLock
);
1808 if (pData
->pipeSend
!= INVALID_PIPE_VALUE
&& ! pData
->pipeClosed
)
1810 if (_writeMsgBuffer("__carla-quit__\n", 15))
1814 waitForChildToStopOrKillIt(pData
->pid
, timeOutMilliseconds
);
1822 void CarlaPipeServer::closePipeServer() noexcept
1824 carla_debug("CarlaPipeServer::closePipeServer()");
1826 pData
->pipeClosed
= true;
1828 const CarlaMutexLocker
cml(pData
->writeLock
);
1830 if (pData
->pipeRecv
!= INVALID_PIPE_VALUE
)
1833 DisconnectNamedPipe(pData
->pipeRecv
);
1835 try { ::CloseHandle(pData
->pipeRecv
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
1837 try { ::close (pData
->pipeRecv
); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
1839 pData
->pipeRecv
= INVALID_PIPE_VALUE
;
1842 if (pData
->pipeSend
!= INVALID_PIPE_VALUE
)
1845 DisconnectNamedPipe(pData
->pipeSend
);
1847 try { ::CloseHandle(pData
->pipeSend
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
1849 try { ::close (pData
->pipeSend
); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
1851 pData
->pipeSend
= INVALID_PIPE_VALUE
;
1855 void CarlaPipeServer::writeShowMessage() const noexcept
1857 const CarlaMutexLocker
cml(pData
->writeLock
);
1859 if (! _writeMsgBuffer("show\n", 5))
1865 void CarlaPipeServer::writeFocusMessage() const noexcept
1867 const CarlaMutexLocker
cml(pData
->writeLock
);
1869 if (! _writeMsgBuffer("focus\n", 6))
1875 void CarlaPipeServer::writeHideMessage() const noexcept
1877 const CarlaMutexLocker
cml(pData
->writeLock
);
1879 if (! _writeMsgBuffer("show\n", 5))
1885 // -----------------------------------------------------------------------
1887 CarlaPipeClient::CarlaPipeClient() noexcept
1890 carla_debug("CarlaPipeClient::CarlaPipeClient()");
1893 CarlaPipeClient::~CarlaPipeClient() /*noexcept*/
1895 carla_debug("CarlaPipeClient::~CarlaPipeClient()");
1900 bool CarlaPipeClient::initPipeClient(const char* argv
[]) noexcept
1902 CARLA_SAFE_ASSERT_RETURN(pData
->pipeRecv
== INVALID_PIPE_VALUE
, false);
1903 CARLA_SAFE_ASSERT_RETURN(pData
->pipeSend
== INVALID_PIPE_VALUE
, false);
1904 carla_debug("CarlaPipeClient::initPipeClient(%p)", argv
);
1906 const CarlaMutexLocker
cml(pData
->writeLock
);
1908 //----------------------------------------------------------------
1912 const char* const pipeRecvServerStr
= argv
[3];
1913 const char* const pipeSendServerStr
= argv
[4];
1915 HANDLE pipeRecvServer
= ::CreateFileA(pipeRecvServerStr
, GENERIC_READ
, 0x0, nullptr, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, nullptr);
1916 HANDLE pipeSendServer
= ::CreateFileA(pipeSendServerStr
, GENERIC_WRITE
, 0x0, nullptr, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, nullptr);
1918 CARLA_SAFE_ASSERT_RETURN(pipeRecvServer
!= INVALID_HANDLE_VALUE
, false);
1919 CARLA_SAFE_ASSERT_RETURN(pipeSendServer
!= INVALID_HANDLE_VALUE
, false);
1921 const int pipeRecvServer
= std::atoi(argv
[3]);
1922 const int pipeSendServer
= std::atoi(argv
[4]);
1923 /* */ int pipeRecvClient
= std::atoi(argv
[5]);
1924 /* */ int pipeSendClient
= std::atoi(argv
[6]);
1926 CARLA_SAFE_ASSERT_RETURN(pipeRecvServer
> 0, false);
1927 CARLA_SAFE_ASSERT_RETURN(pipeSendServer
> 0, false);
1928 CARLA_SAFE_ASSERT_RETURN(pipeRecvClient
> 0, false);
1929 CARLA_SAFE_ASSERT_RETURN(pipeSendClient
> 0, false);
1931 //----------------------------------------------------------------
1932 // close duplicated handles used by the client
1934 try { ::close(pipeRecvClient
); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
1935 try { ::close(pipeSendClient
); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
1937 //----------------------------------------------------------------
1938 // kill ourselves if parent dies
1940 carla_terminateProcessOnParentExit(false);
1943 //----------------------------------------------------------------
1946 pData
->pipeRecv
= pipeRecvServer
;
1947 pData
->pipeSend
= pipeSendServer
;
1948 pData
->pipeClosed
= false;
1949 pData
->clientClosingDown
= false;
1951 if (writeMessage("\n", 1))
1957 void CarlaPipeClient::closePipeClient() noexcept
1959 carla_debug("CarlaPipeClient::closePipeClient()");
1961 pData
->pipeClosed
= true;
1963 const CarlaMutexLocker
cml(pData
->writeLock
);
1965 if (pData
->pipeRecv
!= INVALID_PIPE_VALUE
)
1968 try { ::CloseHandle(pData
->pipeRecv
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
1970 try { ::close (pData
->pipeRecv
); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
1972 pData
->pipeRecv
= INVALID_PIPE_VALUE
;
1975 if (pData
->pipeSend
!= INVALID_PIPE_VALUE
)
1978 try { ::CloseHandle(pData
->pipeSend
); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
1980 try { ::close (pData
->pipeSend
); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
1982 pData
->pipeSend
= INVALID_PIPE_VALUE
;
1986 void CarlaPipeClient::writeExitingMessageAndWait() noexcept
1989 const CarlaMutexLocker
cml(pData
->writeLock
);
1991 if (_writeMsgBuffer("exiting\n", 8))
1995 // NOTE: no more messages are handled after this point
1996 pData
->clientClosingDown
= true;
1998 for (int i
=0; i
< 100 && ! pData
->pipeClosed
; ++i
)
2004 if (! pData
->pipeClosed
)
2005 carla_stderr2("writeExitingMessageAndWait pipe is still running!");
2008 // -----------------------------------------------------------------------
2010 #undef INVALID_PIPE_VALUE