Skip to content
Draft
Show file tree
Hide file tree
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
38 changes: 11 additions & 27 deletions src/AsyncEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ void AsyncEventSourceClient::_onDisconnect() {
}

void AsyncEventSourceClient::close() {
if (_client) {
_client->close();
}
_client->close();
}

void AsyncEventSourceClient::_detach() {
Expand Down Expand Up @@ -328,19 +326,9 @@ void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb) {
}

void AsyncEventSource::_addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request) {
if (!client) {
return;
}
if (!client->_client && request) {
// Adopt the connection from the request. The client was pre-allocated
// (before any bytes were written to the socket) in AsyncEventSourceResponse::_respond().
client->_client = request->clientRelease();
}
if (!client->_client) {
// No connection to adopt (e.g. the request was already gone): drop the
// half-constructed client instead of registering a zombie.
return;
}
// The client was pre-allocated in AsyncEventSourceResponse::_respond(), before
// any bytes were written to the socket.
client->_client = request->clientRelease();

// Network callbacks are attached here (not in the constructor) because they rely on
// shared_from_this(), which requires shared_ptr ownership.
Expand Down Expand Up @@ -383,20 +371,16 @@ void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient *client) {
}

void AsyncEventSource::close() {
// Snapshot the list under the lock, close outside it: a synchronous
// _onDisconnect() inside c->close() erases list nodes, which would
// otherwise invalidate the loop iterator (and could deadlock).
std::vector<AsyncEventSourceClientPtr> live;
// Take the list, then close outside the lock. Close unconditionally: a client
// can be disconnected with its report still queued, and closing reports it
// while we hold the handle, rather than from its destructor.
std::list<AsyncEventSourceClientPtr> closing;
{
asyncsrv::lock_guard_type lock(_client_queue_lock);
for (const auto &c : _clients) {
live.push_back(c);
}
closing.splice(closing.end(), _clients);
}
for (const auto &c : live) {
if (c->connected()) {
c->close();
}
for (const auto &c : closing) {
c->close();
}
}

Expand Down
42 changes: 18 additions & 24 deletions src/AsyncEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ using AsyncEvent_SharedData_t = std::shared_ptr<String>;
class AsyncEventSourceClient : public std::enable_shared_from_this<AsyncEventSourceClient> {
private:
friend class AsyncEventSource;
friend class AsyncEventSourceResponse;
AsyncClient *_client;
AsyncEventSource *_server;
uint32_t _lastId{0};
Expand All @@ -89,19 +90,16 @@ class AsyncEventSourceClient : public std::enable_shared_from_this<AsyncEventSou
// detach from the owning server (used when the server dies before the client)
// and close the connection
void _detach();
void _onAck(size_t len, uint32_t time);
void _onPoll();
void _onTimeout(uint32_t time);
void _onDisconnect();

public:
/**
* @brief Construct a new Async Event Source Client object
* @note constructor is normally passed a client object from AsyncWebServerRequest::releaseClient(); see AsyncEventSourceResponse::_respond()
* @note the client does not register itself with the server; ownership passes to
* AsyncEventSource::_addClient(), which stores it in the shared client list
*
* @param client
* @param server
* @param lastId
*/
// Created by AsyncEventSourceResponse::_respond() without a connection;
// AsyncEventSource::_addClient() adopts the request's and lists the client.
AsyncEventSourceClient(AsyncClient *client, AsyncEventSource *server, uint32_t lastId = 0);

public:
~AsyncEventSourceClient();

/**
Expand Down Expand Up @@ -161,7 +159,7 @@ class AsyncEventSourceClient : public std::enable_shared_from_this<AsyncEventSou
return _client;
}
bool connected() const {
return _client && _client->connected();
return _client->connected();
}
uint32_t lastId() const {
return _lastId;
Expand All @@ -188,12 +186,6 @@ class AsyncEventSourceClient : public std::enable_shared_from_this<AsyncEventSou
size_t get_max_inflight_bytes() const {
return _max_inflight;
}

// system callbacks (do not call if from user code!)
void _onAck(size_t len, uint32_t time);
void _onPoll();
void _onTimeout(uint32_t time);
void _onDisconnect();
};

/**
Expand All @@ -203,6 +195,8 @@ class AsyncEventSourceClient : public std::enable_shared_from_this<AsyncEventSou
*/
class AsyncEventSource : public AsyncWebHandler {
private:
friend class AsyncEventSourceClient;
friend class AsyncEventSourceResponse;
String _url;
std::list<AsyncEventSourceClientPtr> _clients;
// Same as for individual messages, protect mutations of _clients list
Expand All @@ -214,6 +208,10 @@ class AsyncEventSource : public AsyncWebHandler {
// this method manipulates in-fligh data size for connected client depending on number of active connections
void _adjust_inflight_window();

// gives a pre-allocated client the request's connection, then registers it
void _addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request);
void _handleDisconnect(AsyncEventSourceClient *client);

public:
typedef enum {
DISCARDED = 0,
Expand Down Expand Up @@ -293,21 +291,17 @@ class AsyncEventSource : public AsyncWebHandler {
*/
std::vector<AsyncEventSourceClientPtr> getClients() const;

// system callbacks (do not call from user code!)
// registers a pre-allocated client; when a request is supplied, adopts its
// AsyncClient (ownership transfer) before registration
void _addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request = nullptr);
void _handleDisconnect(AsyncEventSourceClient *client);
bool canHandle(AsyncWebServerRequest *request) const final;
void handleRequest(AsyncWebServerRequest *request) final;
};

class AsyncEventSourceResponse : public AsyncWebServerResponse {
private:
friend class AsyncEventSource;
AsyncEventSource *_server;
AsyncEventSourceResponse(AsyncEventSource *server);

public:
AsyncEventSourceResponse(AsyncEventSource *server);
void _respond(AsyncWebServerRequest *request) override;
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time) override {
return 0;
Expand Down
Loading