-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdb.hpp
268 lines (257 loc) · 6.05 KB
/
rdb.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef _RDB_H_
#define _RDB_H_
#include <cstdint>
//
void rdbPoint(
float x, float y, float z,
float r = 1.0f, float g = 1.0f, float b = 1.0f,
int32_t group = 0);
void rdbLine(
float x0, float y0, float z0,
float x1, float y1, float z1,
float r0 = 1.0f, float g0 = 1.0f, float b0 = 1.0f,
float r1 = 1.0f, float g1 = 1.0f, float b1 = 1.0f,
int32_t group = 0);
void rdbTriangle(
float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
float r = 1.0f, float g = 1.0f, float b = 1.0f,
int32_t group = 0);
#if defined(RDB_IMPLIMATATION)
//
#include <WinSock2.h>
#include <concurrent_queue.h>
#pragma comment(lib, "Ws2_32.lib")
//
#include <thread>
#include <mutex>
#include <unordered_map>
#include <array>
//
enum class RdbTaskType : int32_t
{
POINT,
LINE,
TRIANGLE,
};
//
struct RdbTask
{
RdbTaskType type;
union
{
struct
{
float x;
float y;
float z;
float r;
float g;
float b;
int32_t group;
}rdbPoint;
struct
{
float x0;
float y0;
float z0;
float x1;
float y1;
float z1;
float r0;
float g0;
float b0;
float r1;
float g1;
float b1;
int32_t group;
}rdbLine;
struct
{
float x0;
float y0;
float z0;
float x1;
float y1;
float z1;
float x2;
float y2;
float z2;
float r;
float g;
float b;
int32_t group;
}rdbTriangle;
};
};
Concurrency::concurrent_queue<RdbTask> rdbTasks;
constexpr int32_t RDB_BUFFER_SIZE = 1024 * 4;
struct RdbContext
{
public:
std::once_flag initialized;
size_t bytesToSend = 0;
SOCKET fd;
}rdbContext;
std::thread rdbMainThread;
std::atomic<bool> rdbFinishFlag = false;
//
void rdbInitCheck();
void rdbPrintf(const char* fmt, ...);
void rdbMain();
//
void rdbPrintf(const char* fmt, ...)
{
//
rdbInitCheck();
//
char buffer[RDB_BUFFER_SIZE];
va_list argp;
va_start(argp, fmt);
const int32_t bytesToSend = vsnprintf(buffer, RDB_BUFFER_SIZE, fmt, argp);
va_end(argp);
//
size_t sended = ::send(rdbContext.fd, buffer, bytesToSend, 0);
// TODO: -1が帰ったらもう何も送らない状態にする
//printf("%s,[%d]", buffer, sended);
}
//
void rdbMain()
{
while (!rdbFinishFlag)
{
if (rdbTasks.empty())
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(10ms);
}
//
RdbTask task;
while (rdbTasks.try_pop(task))
{
switch (task.type)
{
case RdbTaskType::POINT:
{
const auto& t = task.rdbPoint;
rdbPrintf("P %f,%f,%f,%f,%f,%f,%d\n",
t.x, t.y, t.z, t.r, t.g, t.b, t.group);
}
break;
case RdbTaskType::LINE:
{
const auto& t = task.rdbLine;
rdbPrintf("L %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d\n",
t.x0, t.y0, t.z0,
t.x1, t.y1, t.z1,
t.r0, t.g0, t.b0,
t.r1, t.g1, t.b1,
t.group);
}
break;
case RdbTaskType::TRIANGLE:
{
const auto& t = task.rdbTriangle;
rdbPrintf("T %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d\n",
t.x0, t.y0, t.z0,
t.x1, t.y1, t.z1,
t.x2, t.y2, t.z2,
t.r, t.g, t.b,
t.group);
}
break;
}
}
}
}
//
void rdbInitCheck()
{
std::call_once(rdbContext.initialized, []()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData); // TODO: エラー処理
rdbContext.fd = socket(AF_INET, SOCK_STREAM, 0);
//
struct sockaddr_in serv_name;
serv_name.sin_family = AF_INET;
serv_name.sin_addr.s_addr = ::htonl(0x7F000001L);
serv_name.sin_port = ::htons(10000);
::connect(rdbContext.fd, (struct sockaddr*) & serv_name, sizeof(serv_name));
::atexit([]()
{
rdbFinishFlag = true;
rdbMainThread.join();
::closesocket(rdbContext.fd);
});
//
rdbMainThread = std::thread([]() {rdbMain(); });
});
}
//
void rdbPoint(
float x, float y, float z,
float r, float g, float b,
int32_t group)
{
rdbInitCheck();
RdbTask task;
task.rdbPoint.x = x;
task.rdbPoint.y = y;
task.rdbPoint.z = z;
task.rdbPoint.r = r;
task.rdbPoint.g = g;
task.rdbPoint.b = b;
task.rdbPoint.group = group;
task.type = RdbTaskType::POINT;
rdbTasks.push(task);
}
//
void rdbLine(
float x0, float y0, float z0,
float x1, float y1, float z1,
float r0, float g0, float b0,
float r1, float g1, float b1,
int32_t group)
{
rdbInitCheck();
RdbTask task;
auto& t = task.rdbLine;
t.x0 = x0; t.y0 = y0; t.z0 = z0;
t.x1 = x1; t.y1 = y1; t.z1 = z1;
t.r0 = r0; t.g0 = g0; t.b0 = b0;
t.r1 = r1; t.g1 = g1; t.b1 = b1;
t.group = group;
task.type = RdbTaskType::LINE;
rdbTasks.push(task);
}
//
void rdbTriangle(
float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
float r, float g, float b,
int32_t group)
{
rdbInitCheck();
RdbTask task;
auto& t = task.rdbTriangle;
t.x0 = x0;
t.y0 = y0;
t.z0 = z0;
t.x1 = x1;
t.y1 = y1;
t.z1 = z1;
t.x2 = x2;
t.y2 = y2;
t.z2 = z2;
t.r = r;
t.g = g;
t.b = b;
t.group = group;
task.type = RdbTaskType::TRIANGLE;
rdbTasks.push(task);
}
#endif
#endif