@@ -10225,51 +10225,83 @@ static void _webui_receive(_webui_window_t* win, struct mg_connection* client,
1022510225 } else {
1022610226 arg_len = len ;
1022710227 if (len > 0 ) {
10228- if (win -> ws_block ) {
10229- // This event has data. And it will be processed
10230- // in this current thread, no need to copy data
10231- arg_ptr = data ;
10232- }
10233- else {
10234- // This event has data. And it will be processed
10235- // in a new thread, we should copy data once
10236- void * data_cpy = (void * )_webui_malloc (len );
10237- memcpy ((char * )data_cpy , data , len );
10238- arg_ptr = data_cpy ;
10239- }
10228+
10229+ // -- Old Method --
10230+ // if (win->ws_block) {
10231+ // // This event has data. And it will be processed
10232+ // // in this current thread, no need to copy data
10233+ // arg_ptr = data;
10234+ // }
10235+ // else {
10236+ // // This event has data. And it will be processed
10237+ // // in a new thread, we should copy data once
10238+ // void * data_cpy = (void*)_webui_malloc(len);
10239+ // memcpy((char*)data_cpy, data, len);
10240+ // arg_ptr = data_cpy;
10241+ // }
10242+
10243+ // -- New Method --
10244+ // This event has data, and it will be processed
10245+ // always in a new thread, we should copy data now
10246+ void * data_cpy = (void * )_webui_malloc (len );
10247+ memcpy ((char * )data_cpy , data , len );
10248+ arg_ptr = data_cpy ;
10249+
1024010250 } else {
1024110251 // This is a WS connect/disconnect event
1024210252 arg_ptr = NULL ;
1024310253 }
1024410254 }
1024510255
1024610256 // Process
10247- if (win -> ws_block ) {
10248- // Process the packet in this current thread
10249- _webui_ws_process (win , client , connection_id , arg_ptr , arg_len , ++ recvNum , event_type );
10250- if (arg_ptr != data )
10251- _webui_free_mem ((void * )arg_ptr );
10252- }
10253- else {
10254- // Process the packet in a new thread
10255- _webui_recv_arg_t * arg = (_webui_recv_arg_t * ) _webui_malloc (sizeof (_webui_recv_arg_t ));
10256- arg -> win = win ;
10257- arg -> ptr = arg_ptr ;
10258- arg -> len = arg_len ;
10259- arg -> recvNum = ++ recvNum ;
10260- arg -> event_type = event_type ;
10261- arg -> client = client ;
10262- arg -> connection_id = connection_id ;
10263- #ifdef _WIN32
10264- HANDLE thread = CreateThread (NULL , 0 , _webui_ws_process_thread , (void * )arg , 0 , NULL );
10265- if (thread != NULL )
10266- CloseHandle (thread );
10267- #else
10268- pthread_t thread ;
10269- pthread_create (& thread , NULL ,& _webui_ws_process_thread , (void * )arg );
10270- pthread_detach (thread );
10271- #endif
10272- }
10257+
10258+ // -- Old Method --
10259+ // if (win->ws_block) {
10260+ // // Process the packet in this current thread
10261+ // _webui_ws_process(win, client, connection_id, arg_ptr, arg_len, recvNum, event_type);
10262+ // if (arg_ptr != data)
10263+ // _webui_free_mem((void*)arg_ptr);
10264+ // }
10265+ // else {
10266+ // // Process the packet in a new thread
10267+ // _webui_recv_arg_t* arg = (_webui_recv_arg_t* ) _webui_malloc(sizeof(_webui_recv_arg_t));
10268+ // arg->win = win;
10269+ // arg->ptr = arg_ptr;
10270+ // arg->len = arg_len;
10271+ // arg->recvNum = recvNum;
10272+ // arg->event_type = event_type;
10273+ // arg->client = client;
10274+ // arg->connection_id = connection_id;
10275+ // #ifdef _WIN32
10276+ // HANDLE thread = CreateThread(NULL, 0, _webui_ws_process_thread, (void*)arg, 0, NULL);
10277+ // if (thread != NULL)
10278+ // CloseHandle(thread);
10279+ // #else
10280+ // pthread_t thread;
10281+ // pthread_create(&thread, NULL,&_webui_ws_process_thread, (void*)arg);
10282+ // pthread_detach(thread);
10283+ // #endif
10284+ // }
10285+
10286+ // -- New Method --
10287+ // Process the packet always in a new thread
10288+ _webui_recv_arg_t * arg = (_webui_recv_arg_t * ) _webui_malloc (sizeof (_webui_recv_arg_t ));
10289+ arg -> win = win ;
10290+ arg -> ptr = arg_ptr ;
10291+ arg -> len = arg_len ;
10292+ arg -> recvNum = recvNum ;
10293+ arg -> event_type = event_type ;
10294+ arg -> client = client ;
10295+ arg -> connection_id = connection_id ;
10296+ #ifdef _WIN32
10297+ HANDLE thread = CreateThread (NULL , 0 , _webui_ws_process_thread , (void * )arg , 0 , NULL );
10298+ if (thread != NULL )
10299+ CloseHandle (thread );
10300+ #else
10301+ pthread_t thread ;
10302+ pthread_create (& thread , NULL ,& _webui_ws_process_thread , (void * )arg );
10303+ pthread_detach (thread );
10304+ #endif
1027310305}
1027410306
1027510307static bool _webui_connection_save (_webui_window_t * win , struct mg_connection * client , size_t * connection_id ) {
@@ -10432,16 +10464,30 @@ static void _webui_ws_process(
1043210464 ((unsigned char )packet [WEBUI_PROTOCOL_SIGN ] == WEBUI_SIGNATURE ) &&
1043310465 (packet_token == win -> token )) {
1043410466
10435- // Mutex
10436- if (_webui .config .ws_block ) {
10437- // wait for previous event to finish
10438- if ((unsigned char )packet [WEBUI_PROTOCOL_CMD ] != WEBUI_CMD_JS ) {
10439- _webui_mutex_lock (& _webui .mutex_receive );
10440- }
10441- }
10442-
1044310467 if (!_webui_mutex_app_is_exit_now (WEBUI_MUTEX_GET_STATUS )) { // Check if previous event called exit()
1044410468
10469+ // Mutex
10470+ if (_webui .config .ws_block ) {
10471+ // The config is set to blocking mode, we should
10472+ // wait for the previous event to finish.
10473+ if ((unsigned char )packet [WEBUI_PROTOCOL_CMD ] != WEBUI_CMD_JS ) {
10474+ #ifdef WEBUI_LOG
10475+ _webui_log_debug (
10476+ "[Core] [WS #%zu]\t_webui_ws_process() -> Waiting for the previous event to finish...\n" ,
10477+ recvNum
10478+ );
10479+ #endif
10480+ _webui_mutex_lock (& _webui .mutex_receive );
10481+ #ifdef WEBUI_LOG
10482+ _webui_log_debug (
10483+ "[Core] [WS #%zu]\t_webui_ws_process() -> WS #%zu is locked.\n" ,
10484+ recvNum , recvNum
10485+ );
10486+ #endif
10487+ }
10488+ }
10489+
10490+ // Process Commands
1044510491 if ((unsigned char )packet [WEBUI_PROTOCOL_CMD ] == WEBUI_CMD_WIN_DRAG ) {
1044610492
1044710493 // Drag Window Event (WebUI `-webkit-app-region: drag;` custom implementation)
@@ -10953,6 +10999,19 @@ static void _webui_ws_process(
1095310999 );
1095411000 }
1095511001 #endif
11002+
11003+ // Unlock Mutex
11004+ if (_webui .config .ws_block ) {
11005+ if ((unsigned char )packet [WEBUI_PROTOCOL_CMD ] != WEBUI_CMD_JS ) {
11006+ #ifdef WEBUI_LOG
11007+ _webui_log_debug (
11008+ "[Core] [WS #%zu]\t_webui_ws_process() -> WS #%zu is unlocked.\n" ,
11009+ recvNum , recvNum
11010+ );
11011+ #endif
11012+ _webui_mutex_unlock (& _webui .mutex_receive );
11013+ }
11014+ }
1095611015 }
1095711016 #ifdef WEBUI_LOG
1095811017 else {
@@ -10962,13 +11021,6 @@ static void _webui_ws_process(
1096211021 );
1096311022 }
1096411023 #endif
10965-
10966- // Unlock Mutex
10967- if (_webui .config .ws_block ) {
10968- if ((unsigned char )packet [WEBUI_PROTOCOL_CMD ] != WEBUI_CMD_JS ) {
10969- _webui_mutex_unlock (& _webui .mutex_receive );
10970- }
10971- }
1097211024 } else {
1097311025
1097411026 #ifdef WEBUI_LOG
0 commit comments