This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcppBinding.cpp
More file actions
53 lines (49 loc) · 1.41 KB
/
cppBinding.cpp
File metadata and controls
53 lines (49 loc) · 1.41 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
#include "cppBinding.hpp"
#include "interface.hpp"
#include "wispServer.hpp"
#include "wispValidation.hpp"
#include <bits/types/error_t.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <sys/types.h>
#include <thread>
#include <uWebSockets/Loop.h>
#include <uWebSockets/WebSocketProtocol.h>
#include <vector>
void send_callback(void *buffer, size_t size, void *id, bool exit = false) {
uWS::WebSocket<SSL, true, PerSocketData> *ws =
(uWS::WebSocket<SSL, true, PerSocketData> *)id;
#ifdef DEBUG
if (validatePacket((char *)buffer, (size_t)size) == WISP_NULL) {
std::cout << "Bad packet from server\n";
}
#endif // DEBUG
ws->getUserData()->loop->defer([=]() {
std::string_view message((char *)buffer, size);
if (ws != NULL) {
ws->send(message, uWS::OpCode::BINARY);
if (exit) {
ws->close();
}
}
free(buffer);
});
}
void on_open(uWS::TemplatedApp<false> *app,
uWS::WebSocket<SSL, true, PerSocketData> *ws) {
ws->getUserData()->loop = app->getLoop();
std::thread(open_interface, send_callback, ws).detach();
}
void on_message(uWS::WebSocket<SSL, true, PerSocketData> *ws,
std::string_view message, uWS::OpCode opCode) {
message_interface(send_callback, std::string(message), ws);
}