Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct WebSocketClient::Impl final : public std::enable_shared_from_this<Impl> {
std::mutex mutexOut_;
std::queue<std::string> messagesOut_;
std::atomic<bool> isWriting_{false};
std::atomic<bool> isConnected_{false};
std::atomic<bool> isClosing_{false};
};

Expand Down Expand Up @@ -215,6 +216,7 @@ void WebSocketClient::Impl::onHandshakeCompleted(boost::system::error_code ec) {
return;
}

isConnected_ = true;
onConnectCallback(true, "Connected");

// Listen for any messages from the server
Expand Down Expand Up @@ -252,26 +254,22 @@ void WebSocketClient::Impl::listen() {
}

void WebSocketClient::Impl::write() {
if (isClosing_) {
if (isClosing_ || !isConnected_) {
return;
}
if (isWriting_) {
if (isWriting_.exchange(true)) {
return;
}
isWriting_ = true;

std::shared_ptr<std::string> message;
{
std::lock_guard<std::mutex> lock(mutexOut_);
if (!messagesOut_.empty()) {
message = std::make_shared<std::string>(messagesOut_.front());
messagesOut_.pop();
if (messagesOut_.empty()) {
isWriting_ = false;
return;
}
}

if (!message || message->empty()) {
isWriting_ = false;
return;
message = std::make_shared<std::string>(messagesOut_.front());
messagesOut_.pop();
}

auto ws = ws_.wlock();
Expand All @@ -281,14 +279,17 @@ void WebSocketClient::Impl::write() {
boost::beast::error_code ec,
std::size_t /*bytes_transferred*/) mutable {
auto impl = weakImpl.lock();
if (!impl || impl->isClosing_) {
if (!impl) {
return;
}
impl->isWriting_ = false;
if (impl->isClosing_) {
return;
}
if (ec) {
LOG(ERROR) << "Error writing to websocket: " << ec.message();
return;
}
impl->isWriting_ = false;
impl->write();
message.reset(); // Release the message after it's been sent
});
Expand Down
Loading