2016-06-28 11:51:48 -07:00
|
|
|
/*
|
|
|
|
Asynchronous WebServer library for Espressif MCUs
|
|
|
|
|
|
|
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "AsyncEventSource.h"
|
|
|
|
|
2016-06-29 09:29:39 -07:00
|
|
|
static String generateEventMessage(const char *message, const char *event, uint32_t id, uint32_t reconnect){
|
|
|
|
String ev = "";
|
|
|
|
|
|
|
|
if(reconnect){
|
|
|
|
ev += "retry: ";
|
|
|
|
ev += String(reconnect);
|
|
|
|
ev += "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(id){
|
|
|
|
ev += "id: ";
|
2016-06-29 11:26:41 -07:00
|
|
|
ev += String(id);
|
2016-06-29 09:29:39 -07:00
|
|
|
ev += "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event != NULL){
|
|
|
|
ev += "event: ";
|
|
|
|
ev += String(event);
|
|
|
|
ev += "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(message != NULL){
|
|
|
|
size_t messageLen = strlen(message);
|
|
|
|
char * lineStart = (char *)message;
|
|
|
|
char * lineEnd;
|
|
|
|
do {
|
|
|
|
char * nextN = strchr(lineStart, '\n');
|
|
|
|
char * nextR = strchr(lineStart, '\r');
|
|
|
|
if(nextN == NULL && nextR == NULL){
|
|
|
|
size_t llen = ((char *)message + messageLen) - lineStart;
|
|
|
|
char * ldata = (char *)malloc(llen+1);
|
|
|
|
if(ldata != NULL){
|
|
|
|
memcpy(ldata, lineStart, llen);
|
|
|
|
ldata[llen] = 0;
|
|
|
|
ev += "data: ";
|
|
|
|
ev += ldata;
|
|
|
|
ev += "\r\n\r\n";
|
|
|
|
free(ldata);
|
|
|
|
}
|
|
|
|
lineStart = (char *)message + messageLen;
|
|
|
|
} else {
|
|
|
|
char * nextLine = NULL;
|
|
|
|
if(nextN != NULL && nextR != NULL){
|
|
|
|
if(nextR < nextN){
|
|
|
|
lineEnd = nextR;
|
|
|
|
if(nextN == (nextR + 1))
|
|
|
|
nextLine = nextN + 1;
|
|
|
|
else
|
|
|
|
nextLine = nextR + 1;
|
|
|
|
} else {
|
|
|
|
lineEnd = nextN;
|
|
|
|
if(nextR == (nextN + 1))
|
|
|
|
nextLine = nextR + 1;
|
|
|
|
else
|
|
|
|
nextLine = nextN + 1;
|
|
|
|
}
|
|
|
|
} else if(nextN != NULL){
|
|
|
|
lineEnd = nextN;
|
|
|
|
nextLine = nextN + 1;
|
|
|
|
} else {
|
|
|
|
lineEnd = nextR;
|
|
|
|
nextLine = nextR + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t llen = lineEnd - lineStart;
|
|
|
|
char * ldata = (char *)malloc(llen+1);
|
|
|
|
if(ldata != NULL){
|
|
|
|
memcpy(ldata, lineStart, llen);
|
|
|
|
ldata[llen] = 0;
|
|
|
|
ev += "data: ";
|
|
|
|
ev += ldata;
|
|
|
|
ev += "\r\n";
|
|
|
|
free(ldata);
|
|
|
|
}
|
|
|
|
lineStart = nextLine;
|
|
|
|
if(lineStart == ((char *)message + messageLen))
|
|
|
|
ev += "\r\n";
|
|
|
|
}
|
|
|
|
} while(lineStart < ((char *)message + messageLen));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2016-06-28 11:51:48 -07:00
|
|
|
// Client
|
|
|
|
|
|
|
|
AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server){
|
|
|
|
_client = request->client();
|
|
|
|
_server = server;
|
2016-06-29 11:21:10 -07:00
|
|
|
_lastId = 0;
|
|
|
|
if(request->hasHeader("Last-Event-ID"))
|
|
|
|
_lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str());
|
2016-09-23 11:56:28 -07:00
|
|
|
|
|
|
|
_client->setRxTimeout(0);
|
2016-06-28 11:51:48 -07:00
|
|
|
_client->onError(NULL, NULL);
|
|
|
|
_client->onAck(NULL, NULL);
|
|
|
|
_client->onPoll(NULL, NULL);
|
|
|
|
_client->onData(NULL, NULL);
|
2016-11-27 07:42:09 -08:00
|
|
|
_client->onTimeout([](void *r, AsyncClient* c __attribute__((unused)), uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout(time); }, this);
|
2016-07-13 14:01:13 -07:00
|
|
|
_client->onDisconnect([](void *r, AsyncClient* c){ ((AsyncEventSourceClient*)(r))->_onDisconnect(); delete c; }, this);
|
2016-06-28 11:51:48 -07:00
|
|
|
_server->_addClient(this);
|
|
|
|
delete request;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncEventSourceClient::~AsyncEventSourceClient(){
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2016-11-27 07:42:09 -08:00
|
|
|
void AsyncEventSourceClient::_onTimeout(uint32_t time __attribute__((unused))){
|
2016-06-28 11:51:48 -07:00
|
|
|
_client->close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSourceClient::_onDisconnect(){
|
|
|
|
_client = NULL;
|
|
|
|
_server->_handleDisconnect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSourceClient::close(){
|
|
|
|
if(_client != NULL)
|
2016-06-29 11:44:33 -07:00
|
|
|
_client->close();
|
2016-06-28 11:51:48 -07:00
|
|
|
}
|
|
|
|
|
2016-06-29 09:29:39 -07:00
|
|
|
void AsyncEventSourceClient::write(const char * message, size_t len){
|
2016-06-28 11:51:48 -07:00
|
|
|
if(!_client->canSend()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(_client->space() < len){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_client->write(message, len);
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:29:39 -07:00
|
|
|
void AsyncEventSourceClient::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){
|
|
|
|
String ev = generateEventMessage(message, event, id, reconnect);
|
|
|
|
write(ev.c_str(), ev.length());
|
|
|
|
}
|
|
|
|
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
// Handler
|
|
|
|
|
2016-11-27 07:42:09 -08:00
|
|
|
AsyncEventSource::AsyncEventSource(const String& url)
|
2016-06-29 09:29:39 -07:00
|
|
|
: _url(url)
|
2016-11-27 07:42:09 -08:00
|
|
|
, _clients(LinkedList<AsyncEventSourceClient *>([](AsyncEventSourceClient *c){ delete c; }))
|
2016-06-29 09:29:39 -07:00
|
|
|
, _connectcb(NULL)
|
|
|
|
{}
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
AsyncEventSource::~AsyncEventSource(){
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:29:39 -07:00
|
|
|
void AsyncEventSource::onConnect(ArEventHandlerFunction cb){
|
|
|
|
_connectcb = cb;
|
|
|
|
}
|
|
|
|
|
2016-06-28 11:51:48 -07:00
|
|
|
void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
|
2016-06-29 11:21:10 -07:00
|
|
|
/*char * temp = (char *)malloc(2054);
|
|
|
|
if(temp != NULL){
|
|
|
|
memset(temp+1,' ',2048);
|
|
|
|
temp[0] = ':';
|
|
|
|
temp[2049] = '\r';
|
|
|
|
temp[2050] = '\n';
|
|
|
|
temp[2051] = '\r';
|
|
|
|
temp[2052] = '\n';
|
|
|
|
temp[2053] = 0;
|
|
|
|
client->write((const char *)temp, 2053);
|
|
|
|
free(temp);
|
|
|
|
}*/
|
2016-11-27 07:42:09 -08:00
|
|
|
|
|
|
|
_clients.add(client);
|
2016-06-29 09:29:39 -07:00
|
|
|
if(_connectcb)
|
|
|
|
_connectcb(client);
|
2016-06-28 11:51:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){
|
2016-11-27 07:42:09 -08:00
|
|
|
_clients.remove(client);
|
2016-06-28 11:51:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSource::close(){
|
2016-11-27 07:42:09 -08:00
|
|
|
for(const auto &c: _clients){
|
2016-06-28 11:51:48 -07:00
|
|
|
if(c->connected())
|
|
|
|
c->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){
|
2016-11-27 07:42:09 -08:00
|
|
|
if(_clients.isEmpty())
|
2016-06-28 11:51:48 -07:00
|
|
|
return;
|
|
|
|
|
2016-06-29 09:29:39 -07:00
|
|
|
String ev = generateEventMessage(message, event, id, reconnect);
|
2016-11-27 07:42:09 -08:00
|
|
|
for(const auto &c: _clients){
|
2016-06-28 11:51:48 -07:00
|
|
|
if(c->connected())
|
2016-06-29 09:29:39 -07:00
|
|
|
c->write(ev.c_str(), ev.length());
|
2016-06-28 11:51:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 07:42:09 -08:00
|
|
|
size_t AsyncEventSource::count() const {
|
|
|
|
return _clients.count_if([](AsyncEventSourceClient *c){
|
|
|
|
return c->connected();
|
|
|
|
});
|
2016-06-30 01:26:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-28 11:51:48 -07:00
|
|
|
bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){
|
|
|
|
if(request->method() != HTTP_GET || !request->url().equals(_url))
|
|
|
|
return false;
|
2016-06-29 11:21:10 -07:00
|
|
|
request->addInterestingHeader("Last-Event-ID");
|
2016-06-28 11:51:48 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request){
|
|
|
|
request->send(new AsyncEventSourceResponse(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
|
|
|
|
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncEventSource *server){
|
|
|
|
_server = server;
|
|
|
|
_code = 200;
|
|
|
|
_contentType = "text/event-stream";
|
|
|
|
_sendContentLength = false;
|
|
|
|
addHeader("Cache-Control", "no-cache");
|
|
|
|
addHeader("Connection","keep-alive");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncEventSourceResponse::_respond(AsyncWebServerRequest *request){
|
|
|
|
String out = _assembleHead(request->version());
|
|
|
|
request->client()->write(out.c_str(), _headLength);
|
|
|
|
_state = RESPONSE_WAIT_ACK;
|
|
|
|
}
|
|
|
|
|
2016-11-27 07:42:09 -08:00
|
|
|
size_t AsyncEventSourceResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time __attribute__((unused))){
|
2016-06-28 11:51:48 -07:00
|
|
|
if(len){
|
|
|
|
new AsyncEventSourceClient(request, _server);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|