33#include " api/APIHelp.h"
44#include " engine/EngineManager.h"
55#include " engine/TimeTaskSystem.h"
6+ #include " ll/api/chrono/GameChrono.h"
7+ #include " ll/api/schedule/Scheduler.h"
8+ #include " ll/api/schedule/Task.h"
69#include " ll/api/service/ServerInfo.h"
710#include " main/SafeGuardRecord.h"
811
1114#include < vector>
1215
1316using namespace cyanray ;
17+ ll::schedule::GameTickScheduler WSScheduler;
1418
1519// Some script::Exception have a problem which can crash the server, and I have no idea, so not output message &
1620// stacktrace
@@ -167,7 +171,7 @@ void WSClientClass::initListeners_s() {
167171 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
168172 || engine->isDestroying ())
169173 return ;
170- std::thread ( [nowList, engine, msg = std::move (msg)]() {
174+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [nowList, engine, msg = std::move (msg)]() {
171175 try {
172176 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
173177 || engine->isDestroying ())
@@ -179,35 +183,38 @@ void WSClientClass::initListeners_s() {
179183 }
180184 }
181185 CATCH_CALLBACK (" Fail in OnTextReceived" )
182- }). detach () ;
186+ });
183187 });
184188
185189 ws->OnBinaryReceived ([nowList{&listeners[int (WSClientEvents::onBinaryReceived)]},
186190 engine = EngineScope::currentEngine ()](WebSocketClient& client, vector<uint8_t > data) {
187191 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
188192 || engine->isDestroying ())
189193 return ;
190- std::thread ([nowList, engine, data = std::move (data)]() mutable {
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- }
194+ WSScheduler.add <ll::schedule::DelayTask>(
195+ ll::chrono::ticks (1 ),
196+ [nowList, engine, data = std::move (data)]() mutable {
197+ try {
198+ if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
199+ || engine->isDestroying ())
200+ return ;
201+ EngineScope enter (engine);
202+ if (!nowList->empty ())
203+ for (auto & listener : *nowList) {
204+ listener.func .get ().call ({}, {ByteBuffer::newByteBuffer (data.data (), data.size ())});
205+ }
206+ }
207+ CATCH_CALLBACK (" Fail in OnBinaryReceived" )
200208 }
201- CATCH_CALLBACK (" Fail in OnBinaryReceived" )
202- }).detach ();
209+ );
203210 });
204211
205212 ws->OnError ([nowList{&listeners[int (WSClientEvents::onError)]},
206213 engine = EngineScope::currentEngine ()](WebSocketClient& client, string msg) {
207214 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
208215 || engine->isDestroying ())
209216 return ;
210- std::thread ( [nowList, engine, msg = std::move (msg)]() {
217+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [nowList, engine, msg = std::move (msg)]() {
211218 try {
212219 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
213220 || engine->isDestroying ())
@@ -219,15 +226,15 @@ void WSClientClass::initListeners_s() {
219226 }
220227 }
221228 CATCH_CALLBACK (" Fail in OnError" )
222- }). detach () ;
229+ });
223230 });
224231
225232 ws->OnLostConnection ([nowList{&listeners[int (WSClientEvents::onLostConnection)]},
226233 engine = EngineScope::currentEngine ()](WebSocketClient& client, int code) {
227234 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
228235 || engine->isDestroying ())
229236 return ;
230- std::thread ( [nowList, engine, code]() {
237+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [nowList, engine, code]() {
231238 try {
232239 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
233240 || engine->isDestroying ())
@@ -239,7 +246,7 @@ void WSClientClass::initListeners_s() {
239246 }
240247 }
241248 CATCH_CALLBACK (" Fail in OnLostConnection" )
242- }). detach () ;
249+ });
243250 });
244251}
245252
@@ -413,28 +420,28 @@ Local<Value> WSClientClass::errorCode(const Arguments& args) {
413420
414421using namespace httplib ;
415422
416- #define ADD_CALLBACK (method , path, func ) \
423+ #define ADD_CALLBACK (METHOD , path, func ) \
417424 callbacks.emplace(make_pair( \
418425 path, \
419426 HttpServerCallback{ \
420427 EngineScope::currentEngine (), \
421428 script::Global<Function>{func}, \
422- HttpRequestType::method , \
429+ HttpRequestType::METHOD , \
423430 path \
424431 } \
425432 )); \
426- svr->##method## (path.c_str(), [this , engine = EngineScope::currentEngine()](const Request& req, Response& resp) { \
433+ svr->METHOD (path.c_str(), [this, engine = EngineScope::currentEngine()](const Request& req, Response& resp) { \
427434 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine) \
428435 || engine->isDestroying ()) \
429436 return ; \
430- std::thread ( [this , engine, req, &resp] { \
437+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [this , engine, req, &resp] { \
431438 try { \
432439 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine) \
433440 || engine->isDestroying ()) \
434441 return ; \
435442 EngineScope enter (engine); \
436443 for (auto & [k, v] : this ->callbacks ) { \
437- if (v.type != HttpRequestType::method ) return ; \
444+ if (v.type != HttpRequestType::METHOD ) return ; \
438445 std::regex rgx (k); \
439446 std::smatch matches; \
440447 if (std::regex_match (req.path , matches, rgx)) { \
@@ -449,7 +456,7 @@ using namespace httplib;
449456 } \
450457 } \
451458 CATCH_CALLBACK (" Fail in NetworkAPI callback" ) \
452- }). join (); \
459+ }); \
453460 });
454461
455462HttpServerClass::HttpServerClass (const Local<Object>& scriptObj) : ScriptClass(scriptObj), svr(new Server) {}
@@ -554,7 +561,7 @@ Local<Value> HttpServerClass::onPreRouting(const Arguments& args) {
554561 || engine->isDestroying ())
555562 return Server::HandlerResponse::Unhandled;
556563 bool handled = false ;
557- std::thread ( [this , engine, req, &resp, &handled] {
564+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [this , engine, req, &resp, &handled] {
558565 try {
559566 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
560567 || engine->isDestroying ())
@@ -569,7 +576,7 @@ Local<Value> HttpServerClass::onPreRouting(const Arguments& args) {
569576 resp = *respObj->get ();
570577 }
571578 CATCH_CALLBACK (" Fail in onPreRouting" );
572- }). join () ;
579+ });
573580 return handled ? Server::HandlerResponse::Handled : Server::HandlerResponse::Unhandled;
574581 });
575582 return this ->getScriptObject ();
@@ -588,7 +595,7 @@ Local<Value> HttpServerClass::onPostRouting(const Arguments& args) {
588595 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
589596 || engine->isDestroying ())
590597 return ;
591- std::thread ( [this , engine, req, &resp] {
598+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [this , engine, req, &resp] {
592599 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
593600 || engine->isDestroying ())
594601 return ;
@@ -597,7 +604,7 @@ Local<Value> HttpServerClass::onPostRouting(const Arguments& args) {
597604 auto respObj = new HttpResponseClass (resp);
598605 this ->postRoutingCallback .func .get ().call ({}, reqObj, respObj);
599606 resp = *respObj->get ();
600- }). join () ;
607+ });
601608 });
602609 return this ->getScriptObject ();
603610 }
@@ -614,7 +621,7 @@ Local<Value> HttpServerClass::onError(const Arguments& args) {
614621 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
615622 || engine->isDestroying ())
616623 return ;
617- std::thread ( [this , engine, req, &resp] {
624+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [this , engine, req, &resp] {
618625 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
619626 || engine->isDestroying ())
620627 return ;
@@ -623,7 +630,7 @@ Local<Value> HttpServerClass::onError(const Arguments& args) {
623630 auto respObj = new HttpResponseClass (resp);
624631 this ->errorCallback .func .get ().call ({}, reqObj, respObj);
625632 resp = *respObj->get ();
626- }). join () ;
633+ });
627634 });
628635 return this ->getScriptObject ();
629636 }
@@ -641,7 +648,7 @@ Local<Value> HttpServerClass::onException(const Arguments& args) {
641648 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
642649 || engine->isDestroying ())
643650 return ;
644- std::thread ( [this , engine, req, &resp, e] {
651+ WSScheduler. add <ll::schedule::DelayTask>( ll::chrono::ticks ( 1 ), [this , engine, req, &resp, e] {
645652 if ((ll::getServerStatus () != ll::ServerStatus::Running) || !EngineManager::isValid (engine)
646653 || engine->isDestroying ())
647654 return ;
@@ -656,7 +663,7 @@ Local<Value> HttpServerClass::onException(const Arguments& args) {
656663 this ->exceptionCallback .func .get ().call ({}, reqObj, respObj, String::newString (exp.what ()));
657664 }
658665 resp = *respObj->get ();
659- }). join () ;
666+ });
660667 }
661668 );
662669 return this ->getScriptObject ();
@@ -983,7 +990,7 @@ Local<Value> HttpResponseClass::getVersion() {
983990 CATCH (" Fail in getVersion!" );
984991}
985992
986- // ////////////////// APIs ////////////////////
993+ // ////////////////// Ported from LiteLoaderBDS ////////////////////
987994
988995void SplitHttpUrl (const std::string& url, string& host, string& path) {
989996 host = url;
@@ -1089,6 +1096,8 @@ bool HttpGetSync(const std::string& url, int* statusRtn, std::string* dataRtn, i
10891096 return true ;
10901097}
10911098
1099+ // ////////////////// APIs ////////////////////
1100+
10921101Local<Value> NetworkClass::httpGet (const Arguments& args) {
10931102 CHECK_ARGS_COUNT (args, 2 );
10941103 CHECK_ARG_TYPE (args[0 ], ValueKind::kString );
0 commit comments