forked from team-unstablers/ulalaca-xrdp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IPCConnection.hpp
95 lines (68 loc) · 2.22 KB
/
IPCConnection.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by Gyuhwan Park on 2022/05/24.
//
#ifndef ULALACA_IPCCONNECTION_HPP
#define ULALACA_IPCCONNECTION_HPP
#include <memory>
#include <thread>
#include <queue>
#include <future>
#include "UnixSocket.hpp"
#include "messages/projector.h"
#include "ulalaca.hpp"
class IPCConnection {
public:
using MallocFreeDeleter = std::function<void(void *)>;
explicit IPCConnection(std::string socketPath);
IPCConnection(IPCConnection &) = delete;
FD descriptor();
/**
* @throws SystemCallException
*/
void connect();
void disconnect();
bool isGood() const;
std::unique_ptr<ULIPCHeader, MallocFreeDeleter> nextHeader();
template <typename T>
void writeMessage(uint16_t messageType, T message) {
auto header = ULIPCHeader {
(uint16_t) messageType,
_messageId,
0, // FIXME
0, // FIXME
sizeof(T)
};
write(&header, sizeof(header));
write(&message, sizeof(T));
}
template<typename T>
std::unique_ptr<T, MallocFreeDeleter> read(size_t size) {
assert(size != 0);
auto promise = std::make_shared<std::promise<std::unique_ptr<uint8_t>>>();
{
std::scoped_lock<std::mutex> scopedReadTasksLock(_readTasksLock);
_readTasks.emplace(size, promise);
}
auto source = promise->get_future().get();
auto destination = std::unique_ptr<T, MallocFreeDeleter>(
(T *) malloc(size),
free
);
std::memmove(destination.get(), source.get(), size);
return std::move(destination);
}
void write(const void *pointer, size_t size);
private:
void workerLoop();
std::atomic_uint64_t _messageId;
std::atomic_uint64_t _ackId;
UnixSocket _socket;
std::thread _workerThread;
bool _isWorkerTerminated;
bool _isGood;
std::mutex _writeTasksLock;
std::mutex _readTasksLock;
std::queue<std::pair<size_t, std::unique_ptr<uint8_t, MallocFreeDeleter>>> _writeTasks;
std::queue<std::pair<size_t, std::shared_ptr<std::promise<std::unique_ptr<uint8_t>>> >> _readTasks;
};
#endif //XRDP_IPCCONNECTION_HPP