Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add queueLength() #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions src/AsyncWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ class AsyncWebSocketClient {
//data packets
void message(AsyncWebSocketMessage *message){ _queueMessage(message); }
bool queueIsFull();
size_t queueLength() { //added by ewowi
return _messageQueue.length();
}
Comment on lines +212 to +214
Copy link

@mathieucarbou mathieucarbou Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a great idea for these reasons:

  1. Traversal is O(N), not O(1) like with std::list on c++ >= 11
  2. No mutex protection to block concurrent access

The internal structure (LinkedList) should be changed IMO because it is not efficient.

In our community maintained fork (https://github.com/mathieucarbou/ESPAsyncWebServer), which is far more evolved than this fork, we have implemented it like this:

size_t AsyncWebSocketClient::queueLen() const {
#ifdef ESP32
  std::lock_guard<std::mutex> lock(_lock);
#endif

  return _messageQueue.size();
}

_messageQueue being a std::list

size_t queueLen() { //added by ewowi, compatible with https://github.com/mathieucarbou/ESPAsyncWebServer.git
return _messageQueue.length();
}

size_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
#ifndef ESP32
Expand Down