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.

138 lines
4.1KB

  1. /*
  2. * Carla Pipe utils
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PIPE_UTILS_HPP_INCLUDED
  18. #define CARLA_PIPE_UTILS_HPP_INCLUDED
  19. #include "CarlaJuceUtils.hpp"
  20. #include "CarlaMutex.hpp"
  21. // -----------------------------------------------------------------------
  22. class CarlaPipeCommon
  23. {
  24. protected:
  25. CarlaPipeCommon() noexcept;
  26. virtual ~CarlaPipeCommon() noexcept;
  27. // returns true if msg handled
  28. virtual bool msgReceived(const char* const msg) noexcept = 0;
  29. // to possibly send errors somewhere
  30. virtual void fail(const char* const error) noexcept
  31. {
  32. carla_stderr2(error);
  33. }
  34. public:
  35. void idle() noexcept;
  36. bool isRunning() const noexcept;
  37. // -------------------------------------------------------------------
  38. // write lock
  39. void lock() const noexcept;
  40. bool tryLock() const noexcept;
  41. void unlock() const noexcept;
  42. CarlaMutex& getLock() noexcept;
  43. // -------------------------------------------------------------------
  44. bool readNextLineAsBool(bool& value) noexcept;
  45. bool readNextLineAsInt(int32_t& value) noexcept;
  46. bool readNextLineAsUInt(uint32_t& value) noexcept;
  47. bool readNextLineAsLong(int64_t& value) noexcept;
  48. bool readNextLineAsULong(uint64_t& value) noexcept;
  49. bool readNextLineAsFloat(float& value) noexcept;
  50. bool readNextLineAsDouble(double& value) noexcept;
  51. bool readNextLineAsString(const char*& value) noexcept;
  52. // -------------------------------------------------------------------
  53. // must be locked before calling
  54. bool writeMsg(const char* const msg) const noexcept;
  55. bool writeMsg(const char* const msg, std::size_t size) const noexcept;
  56. bool writeAndFixMsg(const char* const msg) const noexcept;
  57. bool flush() const noexcept;
  58. // -------------------------------------------------------------------
  59. private:
  60. struct PrivateData;
  61. PrivateData* const pData;
  62. // -------------------------------------------------------------------
  63. friend class CarlaPipeClient;
  64. friend class CarlaPipeServer;
  65. // internal
  66. const char* readline() noexcept;
  67. const char* readlineblock(const uint32_t timeOutMilliseconds = 50) noexcept;
  68. bool writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept;
  69. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeCommon)
  70. };
  71. // -----------------------------------------------------------------------
  72. class CarlaPipeServer : public CarlaPipeCommon
  73. {
  74. public:
  75. CarlaPipeServer() noexcept;
  76. ~CarlaPipeServer() noexcept override;
  77. bool start(const char* const filename, const char* const arg1, const char* const arg2) noexcept;
  78. void stop(const uint32_t timeOutMilliseconds) noexcept;
  79. void close() noexcept;
  80. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeServer)
  81. };
  82. // -----------------------------------------------------------------------
  83. class CarlaPipeClient : public CarlaPipeCommon
  84. {
  85. public:
  86. CarlaPipeClient() noexcept;
  87. ~CarlaPipeClient() noexcept override;
  88. bool init(char* argv[]) noexcept;
  89. void close() noexcept;
  90. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeClient)
  91. };
  92. // -----------------------------------------------------------------------
  93. class ScopedLocale {
  94. public:
  95. ScopedLocale() noexcept;
  96. ~ScopedLocale() noexcept;
  97. private:
  98. const char* const fLocale;
  99. CARLA_DECLARE_NON_COPY_CLASS(ScopedLocale)
  100. CARLA_PREVENT_HEAP_ALLOCATION
  101. };
  102. // -----------------------------------------------------------------------
  103. #endif // CARLA_PIPE_UTILS_HPP_INCLUDED