@@ -334,8 +334,16 @@ POCOClient::POCOResult POCOClient::webSocketConnect(const std::string uri,
334334 try
335335 {
336336 result.addHTTPRequestInfo (request);
337- p_websocket_ = new WebSocket (http_client_session_, request, response);
338- p_websocket_->setReceiveTimeout (Poco::Timespan (timeout));
337+ {
338+ // We must have at least websocket_connect_mutext_.
339+ // If a connection already exists, we must also have websocket_use_mutex_.
340+ // If not, nobody should have the mutex anyway, so we should get it immediately.
341+ ScopedLock<Mutex> connect_lock (websocket_connect_mutex_);
342+ ScopedLock<Mutex> use_lock (websocket_use_mutex_);
343+ p_websocket_ = new WebSocket (http_client_session_, request, response);
344+
345+ p_websocket_->setReceiveTimeout (Poco::Timespan (timeout));
346+ }
339347
340348 result.addHTTPResponseInfo (response);
341349 result.status = POCOResult::OK;
@@ -372,7 +380,7 @@ POCOClient::POCOResult POCOClient::webSocketConnect(const std::string uri,
372380POCOClient::POCOResult POCOClient::webSocketRecieveFrame ()
373381{
374382 // Lock the object's mutex. It is released when the method goes out of scope.
375- ScopedLock<Mutex> lock (websocket_mutex_ );
383+ ScopedLock<Mutex> lock (websocket_use_mutex_ );
376384
377385 // Result of the communication.
378386 POCOResult result;
@@ -451,6 +459,26 @@ POCOClient::POCOResult POCOClient::webSocketRecieveFrame()
451459 return result;
452460}
453461
462+ void POCOClient::webSocketShutdown ()
463+ {
464+ // Make sure nobody is connecting while we're closing.
465+ ScopedLock<Mutex> connect_lock (websocket_connect_mutex_);
466+
467+ // Make sure there is actually a connection to close.
468+ if (!webSocketExist ())
469+ {
470+ return ;
471+ }
472+
473+ // Shut down the socket. This should make webSocketReceiveFrame() return as soon as possible.
474+ p_websocket_->shutdown ();
475+
476+ // Also acquire the websocket lock before invalidating the pointer,
477+ // or we will break running calls to webSocketRecieveFrame().
478+ ScopedLock<Mutex> use_lock (websocket_use_mutex_);
479+ p_websocket_ = Poco::SharedPtr<Poco::Net::WebSocket>();
480+ }
481+
454482/* ***********************************************************
455483 * Auxiliary methods
456484 */
0 commit comments