Mema
Memory Matrix — multi-channel audio matrix monitor and router
Loading...
Searching...
No Matches
InterprocessConnection.h
Go to the documentation of this file.
1/* Copyright (c) 2024, Christian Ahrens
2 *
3 * This file is part of Mema <https://github.com/ChristianAhrens/Mema>
4 *
5 * This tool is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3.0 as published
7 * by the Free Software Foundation.
8 *
9 * This tool is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this tool; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#pragma once
20
21#include <JuceHeader.h>
22
23
24namespace Mema
25{
26
27
31class InterprocessConnectionImpl : public juce::InterprocessConnection
32{
33public:
36
37 void connectionMade() override;
38
39 void connectionLost() override;
40
41 void messageReceived(const MemoryBlock& message) override;
42
43 int getId();
44
45 std::function<void(int)> onConnectionMade;
46 std::function<void(int)> onConnectionLost;
47 std::function<void(int, const MemoryBlock&)> onMessageReceived;
48
49private:
50 int m_id = -1;
51
52};
53
57class InterprocessConnectionServerImpl : public juce::InterprocessConnectionServer
58{
59public:
62
63 void createMessageThread(int id);
64
65 std::map<int, std::pair<double, bool>> getListHealth();
66
67 bool hasActiveConnection(int id);
69 const std::unique_ptr<InterprocessConnectionImpl>& getActiveConnection(int id);
70 const std::vector<int> cleanupDeadConnections();
71
72 const std::vector<int> getActiveConnectionIds();
73
74 bool enqueueMessage(const MemoryBlock& message, std::vector<int> sendIds = {});
75
76 std::function<void(int)> onConnectionCreated;
77
78 static constexpr double s_listSizeThreshold = 35.0;
79
80private:
81 InterprocessConnection* createConnectionObject();
82 void endMessageThread(int id);
83
84 std::map<int, std::mutex> m_sendMessageMutexs;
85 std::map<int, std::queue<juce::MemoryBlock>> m_sendMessageLists;
86 std::map<int, bool> m_sendMessageListClipped;
87 std::map<int, std::atomic<bool>> m_sendMessageResults;
88
89 std::map<int, std::atomic<bool>> m_sendMessageThreadsActive;
90 std::map<int, std::unique_ptr<std::thread>> m_sendMessageThreads;
91 std::map<int, std::condition_variable> m_sendMessageCVs;
92 std::map<int, std::mutex> m_sendMessageCVMutexs;
93
94 std::map<int, std::unique_ptr<InterprocessConnectionImpl>> m_connections;
95 int m_connectionIdIter = 0;
96};
97
98
99}; // namespace Mema
Client-side TCP connection wrapper that forwards JUCE IPC events to std::function callbacks.
std::function< void(int)> onConnectionMade
std::function< void(int)> onConnectionLost
std::function< void(int, const MemoryBlock &)> onMessageReceived
void messageReceived(const MemoryBlock &message) override
TCP server that accepts multiple simultaneous client connections on a fixed port.
std::map< int, std::pair< double, bool > > getListHealth()
const std::unique_ptr< InterprocessConnectionImpl > & getActiveConnection(int id)
bool enqueueMessage(const MemoryBlock &message, std::vector< int > sendIds={})
Definition Mema.cpp:27