-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRenderQueue.cpp
More file actions
151 lines (129 loc) · 3.01 KB
/
CRenderQueue.cpp
File metadata and controls
151 lines (129 loc) · 3.01 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "CRenderQueue.h"
void CRenderQueue::WriteCommand(ERQCommand cmd)
{
semWrite.acquire();
std::cout << "Send command =====> " << cmd << "\n";
buffer.push(static_cast<char>(cmd));
semRead.release();
};
void CRenderQueue::WriteValue(uint32_t value)
{
semWrite.acquire();
std::cout << "Send value =====> " << value << "\n";
for (size_t i = 0; i < 4; ++i)
{
buffer.push(value & 0xFF);
value >>= 8;
}
semRead.release();
};
void CRenderQueue::WriteData(const char* data, size_t size)
{
semWrite.acquire();
std::cout << "Send data =====> \"" << std::string(data) << "\"\n";
for (size_t i = 0; i < size; ++i)
{
buffer.push(data[i]);
}
semRead.release();
};
ERQCommand CRenderQueue::ReadCommand()
{
if (!semRead.try_acquire_for(std::chrono::milliseconds(1000)))
return ERQCommand::INVALID;
ERQCommand cmd{ 0 };
if (!buffer.empty())
{
cmd = static_cast<ERQCommand>(buffer.front());
buffer.pop();
try
{
std::cout << "\t\t\t\t\t====> Read command: " << cmd << "\n";
}
catch (...)
{
std::cout << "INVALID\n";
cmd = ERQCommand::INVALID;
}
}
if (!isFlushing.load() && !isSendingDone.load())
semWrite.release();
else
semRead.release();
return cmd;
};
uint32_t CRenderQueue::ReadValue()
{
semRead.acquire();
uint32_t res{ 0 };
if (!buffer.empty())
{
if (buffer.size() < 4)
throw "Not enough data in buffer";
for (size_t i = 0; i < 4; ++i)
{
unsigned char oneByte = static_cast<unsigned char>(buffer.front());
res |= oneByte << (8 * i);
buffer.pop();
}
std::cout << "\t\t\t\t\t====> Read value: " << res << "\n";
}
if (!isFlushing.load() && !isSendingDone.load())
semWrite.release();
else
semRead.release();
return res;
};
const char* CRenderQueue::ReadData(size_t size)
{
semRead.acquire();
char* res = new char[size + 1] {};
if (!buffer.empty())
{
if (size > buffer.size())
throw "Not enough data in buffer";
for (size_t i = 0; i < size; i++)
{
res[i] = buffer.front();
buffer.pop();
}
res[size] = '\0';
std::cout << "\t\t\t\t\t====> Read data: \"" << std::string(res) << "\"\n";
}
if (!isFlushing.load() && !isSendingDone.load())
semWrite.release();
else
semRead.release();
return res;
};
void CRenderQueue::Flush()
{
isFlushing.store(true);
std::cout << "\n.....waiting for flush....... \n\n";
// wait current command to be processed
while (!isProcessingDone.load())
{
}
std::cout << "\n.....done...................... \n\n";
isFlushing.store(false);
// continue
semWrite.release();
}
void CRenderQueue::Reset()
{
isFlushing.store(true);
std::cout << "\n.....waiting for reset....... \n\n";
// wait for current command being processed
while (!isProcessingDone.load())
{
}
std::cout << "\n.....done...................... \n\n";
// clear buffer
buffer = {};
// clear semaphores
while (semRead.try_acquire())
{
semWrite.release();
}
isFlushing.store(false);
}