-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqwebsocket_wrapper.h
More file actions
237 lines (203 loc) · 9.76 KB
/
Copy pathqwebsocket_wrapper.h
File metadata and controls
237 lines (203 loc) · 9.76 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
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
#pragma once
#include "framework.h"
#include <QWebSocket>
#include "qobject_wrapper.h"
namespace shelllet {
namespace network {
template<typename T, bool> class QWebSocketPrivate;
template<typename T, typename U, bool C>
class QWebSocketWrapper : public core::QObjectWrapper<T, U, C> {
public:
template<bool M>
QWebSocketWrapper(QWebSocketPrivate<T, M>& d, const v8::FunctionCallbackInfo<v8::Value>& args)
: QObjectWrapper(d, args)
{
}
QWebSocketWrapper(v8::Isolate* isolate, const v8::Local<v8::FunctionTemplate>& tpl) : QObjectWrapper(isolate, tpl) {}
QWebSocketWrapper(v8::Isolate* isolate, const v8::Local<v8::ObjectTemplate>& proto) : QObjectWrapper(isolate, proto) {
tpl->PrototypeTemplate()->Set(isolate, "open", v8::FunctionTemplate::New(isolate, Open));
tpl->PrototypeTemplate()->Set(isolate, "sendTextMessage", v8::FunctionTemplate::New(isolate, SendTextMessage));
tpl->PrototypeTemplate()->Set(isolate, "close", v8::FunctionTemplate::New(isolate, Close));
//tpl->PrototypeTemplate()->Set(isolate, "receive", v8::FunctionTemplate::New(isolate, Receive));
//tpl->PrototypeTemplate()->Set(isolate, "exit", v8::FunctionTemplate::New(isolate, Exit));
//tpl->PrototypeTemplate()->SetAccessor(convert::String::New(isolate, "connectTimeOut"), nullptr, SetConnectTimeOut);
}
private:
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
if (args.Length() == 0 || !args[0]->IsString()) {
qCCritical(shelllet_network) << "#no valid argument for open";
return;
}
v8::String::Utf8Value address{ isolate, args[0] };
auto* p = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
p->socket_->open(QUrl(*address));
}
V8_CREATE_LOCAL_CONTEXT_END
//if (async) {
// signal = std::make_shared<boost::signals2::signal<void(const std::unique_ptr<boost::system::system_error>&)>>();
// auto resolver = v8::Promise::Resolver::New(isolate->GetCurrentContext()).ToLocalChecked();
// auto holder = Holder::newHolder(isolate, resolver);
// args.GetReturnValue().Set(resolver->GetPromise());
// signal->connect([holder](const std::unique_ptr<boost::system::system_error>& err) {
// v8::Locker locker(holder->isolate);
// v8::Isolate::Scope scope(holder->isolate);
// v8::HandleScope handle_scope(holder->isolate);
// if (err)
// {
// //THROW_EXCEPTION_VAR(holder->isolate, err->what());
// holder->resolver.Get(holder->isolate)->Reject(holder->isolate->GetCurrentContext(), v8::False(holder->isolate));
// // v8::Isolate::GetCurrent()->RunMicrotasks();
// return;
// }
// holder->resolver.Get(holder->isolate)->Resolve(holder->isolate->GetCurrentContext(), v8::True(holder->isolate));
// v8::Isolate::GetCurrent()->RunMicrotasks();
// });
//}
//async ? client->connect(*address, port, target, signal) : client->connect(*address, port, target);
}
static void SendTextMessage(const v8::FunctionCallbackInfo<v8::Value>& args)
{
//v8::Isolate* isolate = args.GetIsolate();
//v8::Locker locker(isolate);
//if (args.Length() < 1) {
// // THROW_EXCEPTION_VAR(isolate, CONTEXT_CAST(isolate)->getLanguageText("plugin.l1").c_str());
// return;
//}
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
if (args.Length() == 0 || !args[0]->IsString()) {
qCCritical(shelllet_network) << "#no valid argument for open";
return;
}
v8::String::Utf8Value value{ isolate, args[0] };
auto* p = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
p->socket_->sendTextMessage(*value);
}
//LOG_INFO(kMainModuleName) << "boost::this_thread::interruption_enabled: " << boost::this_thread::interruption_enabled() << std::endl;
//boost::this_thread::interruption_point();
//WebSocket* client = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
////bool async = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This())->async_;
//if (!client) {
// return;
//}
//std::shared_ptr<boost::signals2::signal<void(const std::unique_ptr<boost::system::system_error>&)>> signal;
//if (client->async_) {
// auto resolver = v8::Promise::Resolver::New(isolate->GetCurrentContext()).ToLocalChecked();
// args.GetReturnValue().Set(resolver->GetPromise());
// signal = std::make_shared<boost::signals2::signal<void(const std::unique_ptr<boost::system::system_error>&)>>();
// auto holder = Holder::newHolder(isolate, resolver);
// signal->connect([holder](const std::unique_ptr<boost::system::system_error>& err) {
// v8::Locker locker(holder->isolate);
// if (err)
// {
// holder->resolver.Get(holder->isolate)->Reject(holder->isolate->GetCurrentContext(), v8::False(holder->isolate));
// return;
// }
// else {
// holder->resolver.Get(holder->isolate)->Resolve(holder->isolate->GetCurrentContext(), v8::True(holder->isolate));
// v8::Isolate::GetCurrent()->RunMicrotasks();
// }
// });
//}
//if (args[0]->IsString()) {
// v8::String::Utf8Value value{ isolate, args[0] };
// //std::string string = *value;
// client->buffer_ = *value;
// try
// {
// client->async_ ? client->send(boost::asio::buffer(client->buffer_), signal) : client->send(boost::asio::buffer(client->buffer_));
// }
// catch (const std::exception & err)
// {
// LOG_ERROR(kMainModuleName) << err.what() << std::endl;
// }
//}
//else if (args[0]->IsUint8Array()) {
// v8::Local<v8::Uint8Array> uint8_array = v8::Local<v8::Uint8Array>::Cast(args[0]);
// boost::beast::flat_buffer buffer(uint8_array->Length());
// uint8_array->CopyContents(buffer.data().data(), uint8_array->Length());
// client->async_ ? client->send(buffer.cdata(), signal) : client->send(buffer.cdata());
//}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args)
{
/*v8::Isolate* isolate = args.GetIsolate();
v8::Locker locker(isolate);
bool async = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This())->async_;
std::shared_ptr<boost::signals2::signal<void()>> signal;
if (async) {
auto resolver = v8::Promise::Resolver::New(isolate->GetCurrentContext()).ToLocalChecked();
args.GetReturnValue().Set(resolver->GetPromise());
signal = std::make_shared<boost::signals2::signal<void()>>();
auto holder = Holder::newHolder(isolate, resolver);
signal->connect([holder] {
v8::Locker locker(holder->isolate);
holder->resolver.Get(holder->isolate)->Resolve(holder->isolate->GetCurrentContext(), v8::True(holder->isolate));
v8::Isolate::GetCurrent()->RunMicrotasks();
});
}*/
//WebSocketClient* client = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
//async ? client->close(signal) : client->close();
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* p = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
p->socket_->close();
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args)
{
/*v8::Isolate* isolate = args.GetIsolate();
v8::Locker locker(isolate);
bool async = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This())->async_;
std::shared_ptr<boost::signals2::signal<void(boost::beast::flat_buffer&, const std::unique_ptr<boost::system::system_error>&)>> signal;
WebSocketClient* websocket = convert::Object::UnWrap<QWebSocketWrapper<T, U, C>>(args.This());
if (async) {
auto resolver = v8::Promise::Resolver::New(isolate->GetCurrentContext()).ToLocalChecked();
args.GetReturnValue().Set(resolver->GetPromise());
signal = std::make_shared<boost::signals2::signal<void(boost::beast::flat_buffer&, const std::unique_ptr<boost::system::system_error>&)>>();
auto holder = Holder::newHolder(isolate, resolver);
signal->connect([holder, websocket](boost::beast::flat_buffer& buffer, const std::unique_ptr<boost::system::system_error>& err) {
v8::Locker locker(holder->isolate);
if (err)
{
holder->resolver.Get(holder->isolate)->Reject(holder->isolate->GetCurrentContext(), v8::False(holder->isolate));
}
else {
if (websocket->isText()) {
holder->resolver.Get(holder->isolate)->Resolve(holder->isolate->GetCurrentContext(),
v8::convert::String::NewFromUtf8(holder->isolate, static_cast<const char*>(buffer.data().data()),
v8::NewStringType::kNormal,
static_cast<int>(buffer.size())).ToLocalChecked());
v8::Isolate::GetCurrent()->RunMicrotasks();
}
else {
v8::Local<v8::ArrayBuffer> array_buffer = v8::ArrayBuffer::New(holder->isolate, buffer.data().data(), buffer.size());
v8::Local<v8::Uint8Array> array = v8::Uint8Array::New(array_buffer, 0, buffer.size());
holder->resolver.Get(holder->isolate)->Resolve(holder->isolate->GetCurrentContext(), array);
v8::Isolate::GetCurrent()->RunMicrotasks();
}
}
});
}
boost::beast::flat_buffer buffer;
async ? websocket->receive(signal) : websocket->receive(buffer);
if (!async) {
if (websocket->isText()) {
args.GetReturnValue().Set(convert::String::New(isolate, static_cast<const char*>(buffer.data().data()), v8::NewStringType::kNormal, static_cast<int>(buffer.size())));
}
else {
v8::Local<v8::ArrayBuffer> array_buffer = v8::ArrayBuffer::New(isolate, buffer.data().data(), buffer.size());
args.GetReturnValue().Set(v8::Uint8Array::New(array_buffer, 0, buffer.size()));
}
}*/
}
private:
QWebSocket* socket_;
};
}
}