@@ -55,57 +55,8 @@ void WebSocketClient::OnMessage(const std::string& message)
5555 return ;
5656 }
5757
58- g_TaskQueue.Push ([this , message]()
59- {
60- const size_t messageLength = message.length () + 1 ;
61-
62- switch (m_callback_type)
63- {
64- case Websocket_STRING:
65- {
66- pMessageForward->PushCell (m_websocket_handle);
67- pMessageForward->PushString (message.c_str ());
68- pMessageForward->PushCell (messageLength);
69- pMessageForward->Execute (nullptr );
70- break ;
71- }
72- case WebSocket_JSON:
73- {
74- auto pYYJsonWrapper = CreateWrapper ();
75-
76- yyjson_read_err readError;
77- yyjson_doc *idoc = yyjson_read_opts (const_cast <char *>(message.c_str ()), message.length (), 0 , nullptr , &readError);
78-
79- if (readError.code )
80- {
81- yyjson_doc_free (idoc);
82- smutils->LogError (myself, " parse JSON message error (%u): %s at position: %d" , readError.code , readError.msg , readError.pos );
83- return ;
84- }
85-
86- pYYJsonWrapper->m_pDocument = WrapImmutableDocument (idoc);
87- pYYJsonWrapper->m_pVal = yyjson_doc_get_root (idoc);
88-
89- HandleError err;
90- HandleSecurity pSec (nullptr , myself->GetIdentity ());
91- m_json_handle = handlesys->CreateHandleEx (g_htJSON, pYYJsonWrapper.release (), &pSec, nullptr , &err);
92-
93- if (!m_json_handle)
94- {
95- smutils->LogError (myself, " Could not create JSON handle (error %d)" , err);
96- return ;
97- }
98-
99- pMessageForward->PushCell (m_websocket_handle);
100- pMessageForward->PushCell (m_json_handle);
101- pMessageForward->PushCell (messageLength);
102- pMessageForward->Execute (nullptr );
103-
104- handlesys->FreeHandle (m_json_handle, &pSec);
105- break ;
106- }
107- }
108- });
58+ WsMessageTaskContext *context = new WsMessageTaskContext (this , message);
59+ g_WebsocketExt.AddTaskToQueue (context);
10960}
11061
11162void WebSocketClient::OnOpen (ix::WebSocketOpenInfo openInfo)
@@ -115,11 +66,8 @@ void WebSocketClient::OnOpen(ix::WebSocketOpenInfo openInfo)
11566 return ;
11667 }
11768
118- g_TaskQueue.Push ([this , openInfo]()
119- {
120- pOpenForward->PushCell (m_websocket_handle);
121- pOpenForward->Execute (nullptr );
122- });
69+ WsOpenTaskContext *context = new WsOpenTaskContext (this , openInfo);
70+ g_WebsocketExt.AddTaskToQueue (context);
12371}
12472
12573void WebSocketClient::OnClose (ix::WebSocketCloseInfo closeInfo)
@@ -129,15 +77,8 @@ void WebSocketClient::OnClose(ix::WebSocketCloseInfo closeInfo)
12977 return ;
13078 }
13179
132- // TODO: Fixed crash when unload extension after connecting
133- // 2024/06/30 - 23:09 - Fixed
134- g_TaskQueue.Push ([this , closeInfo]()
135- {
136- pCloseForward->PushCell (m_websocket_handle);
137- pCloseForward->PushCell (closeInfo.code );
138- pCloseForward->PushString (closeInfo.reason .c_str ());
139- pCloseForward->Execute (nullptr );
140- });
80+ WsCloseTaskContext *context = new WsCloseTaskContext (this , closeInfo);
81+ g_WebsocketExt.AddTaskToQueue (context);
14182}
14283
14384void WebSocketClient::OnError (ix::WebSocketErrorInfo errorInfo)
@@ -147,10 +88,79 @@ void WebSocketClient::OnError(ix::WebSocketErrorInfo errorInfo)
14788 return ;
14889 }
14990
150- g_TaskQueue.Push ([this , errorInfo]()
91+ WsErrorTaskContext *context = new WsErrorTaskContext (this , errorInfo);
92+ g_WebsocketExt.AddTaskToQueue (context);
93+ }
94+
95+ void WsMessageTaskContext::OnCompleted ()
96+ {
97+ const size_t messageLength = m_message.length () + 1 ;
98+
99+ switch (m_client->m_callback_type )
151100 {
152- pErrorForward->PushCell (m_websocket_handle);
153- pErrorForward->PushString (errorInfo.reason .c_str ());
154- pErrorForward->Execute (nullptr );
155- });
101+ case Websocket_STRING:
102+ {
103+ m_client->pMessageForward ->PushCell (m_client->m_websocket_handle );
104+ m_client->pMessageForward ->PushString (m_message.c_str ());
105+ m_client->pMessageForward ->PushCell (messageLength);
106+ m_client->pMessageForward ->Execute (nullptr );
107+ break ;
108+ }
109+ case WebSocket_JSON:
110+ {
111+ auto pYYJsonWrapper = CreateWrapper ();
112+
113+ yyjson_read_err readError;
114+ yyjson_doc *idoc = yyjson_read_opts (const_cast <char *>(m_message.c_str ()), messageLength, 0 , nullptr , &readError);
115+
116+ if (readError.code )
117+ {
118+ yyjson_doc_free (idoc);
119+ smutils->LogError (myself, " parse JSON message error (%u): %s at position: %d" , readError.code , readError.msg , readError.pos );
120+ return ;
121+ }
122+
123+ pYYJsonWrapper->m_pDocument = WrapImmutableDocument (idoc);
124+ pYYJsonWrapper->m_pVal = yyjson_doc_get_root (idoc);
125+
126+ HandleError err;
127+ HandleSecurity pSec (nullptr , myself->GetIdentity ());
128+ m_client->m_json_handle = handlesys->CreateHandleEx (g_htJSON, pYYJsonWrapper.release (), &pSec, nullptr , &err);
129+
130+ if (!m_client->m_json_handle )
131+ {
132+ smutils->LogError (myself, " Could not create JSON handle (error %d)" , err);
133+ return ;
134+ }
135+
136+ m_client->pMessageForward ->PushCell (m_client->m_websocket_handle );
137+ m_client->pMessageForward ->PushCell (m_client->m_json_handle );
138+ m_client->pMessageForward ->PushCell (messageLength);
139+ m_client->pMessageForward ->Execute (nullptr );
140+
141+ handlesys->FreeHandle (m_client->m_json_handle , &pSec);
142+ break ;
143+ }
144+ }
145+ }
146+
147+ void WsOpenTaskContext::OnCompleted ()
148+ {
149+ m_client->pOpenForward ->PushCell (m_client->m_websocket_handle );
150+ m_client->pOpenForward ->Execute (nullptr );
151+ }
152+
153+ void WsCloseTaskContext::OnCompleted ()
154+ {
155+ m_client->pCloseForward ->PushCell (m_client->m_websocket_handle );
156+ m_client->pCloseForward ->PushCell (m_closeInfo.code );
157+ m_client->pCloseForward ->PushString (m_closeInfo.reason .c_str ());
158+ m_client->pCloseForward ->Execute (nullptr );
159+ }
160+
161+ void WsErrorTaskContext::OnCompleted ()
162+ {
163+ m_client->pErrorForward ->PushCell (m_client->m_websocket_handle );
164+ m_client->pErrorForward ->PushString (m_errorInfo.reason .c_str ());
165+ m_client->pErrorForward ->Execute (nullptr );
156166}
0 commit comments