6
6
#include " ll/api/service/ServerInfo.h"
7
7
#include " main/SafeGuardRecord.h"
8
8
9
+ #include < ll/api/utils/ErrorUtils.h>
9
10
#include < string>
10
11
#include < vector>
11
12
12
13
using namespace cyanray ;
13
14
15
+ // Some script::Exception have a problem which can crash the server, and I have no idea, so not output message &
16
+ // stacktrace
17
+ #define CATCH_CALLBACK (LOG ) \
18
+ catch (const Exception& e) { \
19
+ lse::getSelfPluginInstance ().getLogger ().error (LOG); \
20
+ return ; \
21
+ } \
22
+ catch (...) { \
23
+ lse::getSelfPluginInstance ().getLogger ().error (LOG); \
24
+ ll::error_utils::printCurrentException (lse::getSelfPluginInstance ().getLogger ()); \
25
+ LOG_ERROR_WITH_SCRIPT_INFO (); \
26
+ return ; \
27
+ }
28
+
14
29
// ////////////////// Classes ////////////////////
15
30
16
31
ClassDefine<void > NetworkClassBuilder = defineClass(" network" )
@@ -153,14 +168,17 @@ void WSClientClass::initListeners_s() {
153
168
|| engine->isDestroying ())
154
169
return ;
155
170
std::thread ([nowList, engine, msg = std::move (msg)]() {
156
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
157
- || engine->isDestroying ())
158
- return ;
159
- EngineScope enter (engine);
160
- if (!nowList->empty ())
161
- for (auto & listener : *nowList) {
162
- listener.func .get ().call ({}, {String::newString (msg)});
163
- }
171
+ try {
172
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
173
+ || engine->isDestroying ())
174
+ return ;
175
+ EngineScope enter (engine);
176
+ if (!nowList->empty ())
177
+ for (auto & listener : *nowList) {
178
+ listener.func .get ().call ({}, {String::newString (msg)});
179
+ }
180
+ }
181
+ CATCH_CALLBACK (" Fail in OnTextReceived" )
164
182
}).detach ();
165
183
});
166
184
@@ -170,14 +188,17 @@ void WSClientClass::initListeners_s() {
170
188
|| engine->isDestroying ())
171
189
return ;
172
190
std::thread ([nowList, engine, data = std::move (data)]() mutable {
173
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
174
- || engine->isDestroying ())
175
- return ;
176
- EngineScope enter (engine);
177
- if (!nowList->empty ())
178
- for (auto & listener : *nowList) {
179
- listener.func .get ().call ({}, {ByteBuffer::newByteBuffer (data.data (), data.size ())});
180
- }
191
+ try {
192
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
193
+ || engine->isDestroying ())
194
+ return ;
195
+ EngineScope enter (engine);
196
+ if (!nowList->empty ())
197
+ for (auto & listener : *nowList) {
198
+ listener.func .get ().call ({}, {ByteBuffer::newByteBuffer (data.data (), data.size ())});
199
+ }
200
+ }
201
+ CATCH_CALLBACK (" Fail in OnBinaryReceived" )
181
202
}).detach ();
182
203
});
183
204
@@ -187,14 +208,17 @@ void WSClientClass::initListeners_s() {
187
208
|| engine->isDestroying ())
188
209
return ;
189
210
std::thread ([nowList, engine, msg = std::move (msg)]() {
190
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
191
- || engine->isDestroying ())
192
- return ;
193
- EngineScope enter (engine);
194
- if (!nowList->empty ())
195
- for (auto & listener : *nowList) {
196
- listener.func .get ().call ({}, {String::newString (msg)});
197
- }
211
+ try {
212
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
213
+ || engine->isDestroying ())
214
+ return ;
215
+ EngineScope enter (engine);
216
+ if (!nowList->empty ())
217
+ for (auto & listener : *nowList) {
218
+ listener.func .get ().call ({}, {String::newString (msg)});
219
+ }
220
+ }
221
+ CATCH_CALLBACK (" Fail in OnError" )
198
222
}).detach ();
199
223
});
200
224
@@ -204,14 +228,17 @@ void WSClientClass::initListeners_s() {
204
228
|| engine->isDestroying ())
205
229
return ;
206
230
std::thread ([nowList, engine, code]() {
207
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
208
- || engine->isDestroying ())
209
- return ;
210
- EngineScope enter (engine);
211
- if (!nowList->empty ())
212
- for (auto & listener : *nowList) {
213
- listener.func .get ().call ({}, {Number::newNumber (code)});
214
- }
231
+ try {
232
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
233
+ || engine->isDestroying ())
234
+ return ;
235
+ EngineScope enter (engine);
236
+ if (!nowList->empty ())
237
+ for (auto & listener : *nowList) {
238
+ listener.func .get ().call ({}, {Number::newNumber (code)});
239
+ }
240
+ }
241
+ CATCH_CALLBACK (" Fail in OnLostConnection" )
215
242
}).detach ();
216
243
});
217
244
}
@@ -289,6 +316,10 @@ Local<Value> WSClientClass::connectAsync(const Arguments& args) {
289
316
callback{std::move (callbackFunc)},
290
317
engine{EngineScope::currentEngine ()},
291
318
pluginName{ENGINE_OWN_DATA ()->pluginName }]() mutable {
319
+
320
+ #ifdef NDEBUG
321
+ ll::error_utils::setSehTranslator ();
322
+ #endif
292
323
try {
293
324
bool result = false ;
294
325
try {
@@ -397,24 +428,27 @@ using namespace httplib;
397
428
|| engine->isDestroying ()) \
398
429
return ; \
399
430
std::thread ([this , engine, req, &resp] { \
400
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine) \
401
- || engine->isDestroying ()) \
402
- return ; \
403
- EngineScope enter (engine); \
404
- for (auto & [k, v] : this ->callbacks ) { \
405
- if (v.type != HttpRequestType::method) return ; \
406
- std::regex rgx (k); \
407
- std::smatch matches; \
408
- if (std::regex_match (req.path , matches, rgx)) { \
409
- if (matches == req.matches ) { \
410
- auto reqObj = new HttpRequestClass (req); \
411
- auto respObj = new HttpResponseClass (resp); \
412
- v.func .get ().call ({}, reqObj, respObj); \
413
- resp = *respObj->get (); \
414
- break ; \
431
+ try { \
432
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine) \
433
+ || engine->isDestroying ()) \
434
+ return ; \
435
+ EngineScope enter (engine); \
436
+ for (auto & [k, v] : this ->callbacks ) { \
437
+ if (v.type != HttpRequestType::method) return ; \
438
+ std::regex rgx (k); \
439
+ std::smatch matches; \
440
+ if (std::regex_match (req.path , matches, rgx)) { \
441
+ if (matches == req.matches ) { \
442
+ auto reqObj = new HttpRequestClass (req); \
443
+ auto respObj = new HttpResponseClass (resp); \
444
+ v.func .get ().call ({}, reqObj, respObj); \
445
+ resp = *respObj->get (); \
446
+ break ; \
447
+ } \
415
448
} \
416
449
} \
417
450
} \
451
+ CATCH_CALLBACK (" Fail in NetworkAPI callback" ) \
418
452
}).join (); \
419
453
});
420
454
@@ -521,17 +555,20 @@ Local<Value> HttpServerClass::onPreRouting(const Arguments& args) {
521
555
return Server::HandlerResponse::Unhandled;
522
556
bool handled = false ;
523
557
std::thread ([this , engine, req, &resp, &handled] {
524
- if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
525
- || engine->isDestroying ())
526
- return ;
527
- EngineScope enter (engine);
528
- auto reqObj = new HttpRequestClass (req);
529
- auto respObj = new HttpResponseClass (resp);
530
- auto res = this ->preRoutingCallback .func .get ().call ({}, reqObj, respObj);
531
- if (res.isBoolean () && res.asBoolean ().value () == false ) {
532
- handled = true ;
558
+ try {
559
+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
560
+ || engine->isDestroying ())
561
+ return ;
562
+ EngineScope enter (engine);
563
+ auto reqObj = new HttpRequestClass (req);
564
+ auto respObj = new HttpResponseClass (resp);
565
+ auto res = this ->preRoutingCallback .func .get ().call ({}, reqObj, respObj);
566
+ if (res.isBoolean () && res.asBoolean ().value () == false ) {
567
+ handled = true ;
568
+ }
569
+ resp = *respObj->get ();
533
570
}
534
- resp = *respObj-> get ( );
571
+ CATCH_CALLBACK ( " Fail in onPreRouting " );
535
572
}).join ();
536
573
return handled ? Server::HandlerResponse::Handled : Server::HandlerResponse::Unhandled;
537
574
});
0 commit comments