Compare commits

...

2 Commits

5 changed files with 90 additions and 88 deletions

View File

@ -18,11 +18,15 @@
"platforms": ["espressif8266", "espressif32"], "platforms": ["espressif8266", "espressif32"],
"dependencies": [ "dependencies": [
{ {
"owner": "me-no-dev",
"name": "ESPAsyncTCP", "name": "ESPAsyncTCP",
"version": "^1.2.2",
"platforms": "espressif8266" "platforms": "espressif8266"
}, },
{ {
"owner": "me-no-dev",
"name": "AsyncTCP", "name": "AsyncTCP",
"version": "^1.1.1",
"platforms": "espressif32" "platforms": "espressif32"
}, },
{ {

View File

@ -181,7 +181,7 @@ public:
*/ */
AsyncWebSocketMessage::AsyncWebSocketMessage(std::shared_ptr<std::vector<uint8_t>> buffer, uint8_t opcode, bool mask) : AsyncWebSocketMessage::AsyncWebSocketMessage(AsyncWebSocketMessageBuffer buffer, uint8_t opcode, bool mask) :
_WSbuffer{buffer}, _WSbuffer{buffer},
_opcode(opcode & 0x07), _opcode(opcode & 0x07),
_mask{mask}, _mask{mask},
@ -643,39 +643,12 @@ size_t AsyncWebSocketClient::printf_P(PGM_P formatP, ...)
} }
#endif #endif
namespace { AsyncWebSocketMessageBuffer AsyncWebSocketClient::makeBuffer(const uint8_t *message, size_t len)
std::shared_ptr<std::vector<uint8_t>> makeBuffer(const uint8_t *message, size_t len)
{ {
auto buffer = std::make_shared<std::vector<uint8_t>>(len); auto buffer = std::make_shared<std::vector<uint8_t>>(len);
std::memcpy(buffer->data(), message, len); std::memcpy(buffer->data(), message, len);
return buffer; return buffer;
} }
}
void AsyncWebSocketClient::text(std::shared_ptr<std::vector<uint8_t>> buffer)
{
_queueMessage(buffer);
}
void AsyncWebSocketClient::text(const uint8_t *message, size_t len)
{
text(makeBuffer(message, len));
}
void AsyncWebSocketClient::text(const char *message, size_t len)
{
text((const uint8_t *)message, len);
}
void AsyncWebSocketClient::text(const char *message)
{
text(message, strlen(message));
}
void AsyncWebSocketClient::text(const String &message)
{
text(message.c_str(), message.length());
}
void AsyncWebSocketClient::text(const __FlashStringHelper *data) void AsyncWebSocketClient::text(const __FlashStringHelper *data)
{ {
@ -698,31 +671,6 @@ void AsyncWebSocketClient::text(const __FlashStringHelper *data)
} }
} }
void AsyncWebSocketClient::binary(std::shared_ptr<std::vector<uint8_t>> buffer)
{
_queueMessage(buffer, WS_BINARY);
}
void AsyncWebSocketClient::binary(const uint8_t *message, size_t len)
{
binary(makeBuffer(message, len));
}
void AsyncWebSocketClient::binary(const char *message, size_t len)
{
binary((const uint8_t *)message, len);
}
void AsyncWebSocketClient::binary(const char *message)
{
binary(message, strlen(message));
}
void AsyncWebSocketClient::binary(const String &message)
{
binary(message.c_str(), message.length());
}
void AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len) void AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len)
{ {
PGM_P p = reinterpret_cast<PGM_P>(data); PGM_P p = reinterpret_cast<PGM_P>(data);
@ -850,23 +798,15 @@ void AsyncWebSocket::pingAll(const uint8_t *data, size_t len)
c.ping(data, len); c.ping(data, len);
} }
void AsyncWebSocket::text(uint32_t id, AsyncWebSocketMessageBuffer message){
if (AsyncWebSocketClient * c = client(id))
c->message(message);
};
void AsyncWebSocket::text(uint32_t id, const uint8_t *message, size_t len) void AsyncWebSocket::text(uint32_t id, const uint8_t *message, size_t len)
{ {
if (AsyncWebSocketClient * c = client(id)) if (AsyncWebSocketClient * c = client(id))
c->text(makeBuffer(message, len)); c->text(makeBuffer(message, len));
} }
void AsyncWebSocket::text(uint32_t id, const char *message, size_t len)
{
text(id, (const uint8_t *)message, len);
}
void AsyncWebSocket::text(uint32_t id, const char * message)
{
text(id, message, strlen(message));
}
void AsyncWebSocket::text(uint32_t id, const String &message)
{
text(id, message.c_str(), message.length());
}
void AsyncWebSocket::text(uint32_t id, const __FlashStringHelper *data) void AsyncWebSocket::text(uint32_t id, const __FlashStringHelper *data)
{ {
PGM_P p = reinterpret_cast<PGM_P>(data); PGM_P p = reinterpret_cast<PGM_P>(data);
@ -889,7 +829,7 @@ void AsyncWebSocket::text(uint32_t id, const __FlashStringHelper *data)
} }
} }
void AsyncWebSocket::textAll(std::shared_ptr<std::vector<uint8_t>> buffer) void AsyncWebSocket::textAll(AsyncWebSocketMessageBuffer buffer)
{ {
for (auto &c : _clients) for (auto &c : _clients)
if (c.status() == WS_CONNECTED) if (c.status() == WS_CONNECTED)
@ -932,6 +872,10 @@ void AsyncWebSocket::textAll(const __FlashStringHelper *data)
} }
} }
void AsyncWebSocket::binary(uint32_t id, AsyncWebSocketMessageBuffer message){
if (AsyncWebSocketClient * c = client(id))
c->binary(message);
};
void AsyncWebSocket::binary(uint32_t id, const uint8_t *message, size_t len) void AsyncWebSocket::binary(uint32_t id, const uint8_t *message, size_t len)
{ {
if (AsyncWebSocketClient *c = client(id)) if (AsyncWebSocketClient *c = client(id))
@ -961,7 +905,7 @@ void AsyncWebSocket::binary(uint32_t id, const __FlashStringHelper *data, size_t
} }
} }
void AsyncWebSocket::binaryAll(std::shared_ptr<std::vector<uint8_t>> buffer) void AsyncWebSocket::binaryAll(AsyncWebSocketMessageBuffer buffer)
{ {
for (auto &c : _clients) for (auto &c : _clients)
if (c.status() == WS_CONNECTED) if (c.status() == WS_CONNECTED)
@ -1022,7 +966,7 @@ size_t AsyncWebSocket::printfAll(const char *format, ...)
va_end(arg); va_end(arg);
delete[] temp; delete[] temp;
std::shared_ptr<std::vector<uint8_t>> buffer = std::make_shared<std::vector<uint8_t>>(len); AsyncWebSocketMessageBuffer buffer = std::make_shared<std::vector<uint8_t>>(len);
va_start(arg, format); va_start(arg, format);
vsnprintf( (char *)buffer->data(), len + 1, format, arg); vsnprintf( (char *)buffer->data(), len + 1, format, arg);
@ -1058,7 +1002,7 @@ size_t AsyncWebSocket::printfAll_P(PGM_P formatP, ...)
va_end(arg); va_end(arg);
delete[] temp; delete[] temp;
std::shared_ptr<std::vector<uint8_t>> buffer = std::make_shared<std::vector<uint8_t>>(len + 1); AsyncWebSocketMessageBuffer buffer = std::make_shared<std::vector<uint8_t>>(len + 1);
va_start(arg, formatP); va_start(arg, formatP);
vsnprintf_P((char *)buffer->data(), len + 1, formatP, arg); vsnprintf_P((char *)buffer->data(), len + 1, formatP, arg);
@ -1141,6 +1085,13 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request)
request->send(response); request->send(response);
} }
AsyncWebSocketMessageBuffer AsyncWebSocket::makeBuffer(const uint8_t *message, size_t len)
{
auto buffer = std::make_shared<std::vector<uint8_t>>(len);
std::memcpy(buffer->data(), message, len);
return buffer;
}
/* /*
* Response to Web Socket request - sends the authorization and detaches the TCP Client from the web server * Response to Web Socket request - sends the authorization and detaches the TCP Client from the web server
* Authentication code from https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.cpp#L480 * Authentication code from https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.cpp#L480
@ -1208,3 +1159,4 @@ size_t AsyncWebSocketResponse::_ack(AsyncWebServerRequest *request, size_t len,
return 0; return 0;
} }

View File

@ -50,6 +50,8 @@
#define DEFAULT_MAX_WS_CLIENTS 4 #define DEFAULT_MAX_WS_CLIENTS 4
#endif #endif
using AsyncWebSocketMessageBuffer = std::shared_ptr<std::vector<uint8_t>>;
class AsyncWebSocket; class AsyncWebSocket;
class AsyncWebSocketResponse; class AsyncWebSocketResponse;
class AsyncWebSocketClient; class AsyncWebSocketClient;
@ -84,6 +86,7 @@ typedef enum { WS_CONTINUATION, WS_TEXT, WS_BINARY, WS_DISCONNECT = 0x08, WS_PIN
typedef enum { WS_MSG_SENDING, WS_MSG_SENT, WS_MSG_ERROR } AwsMessageStatus; typedef enum { WS_MSG_SENDING, WS_MSG_SENT, WS_MSG_ERROR } AwsMessageStatus;
typedef enum { WS_EVT_CONNECT, WS_EVT_DISCONNECT, WS_EVT_PONG, WS_EVT_ERROR, WS_EVT_DATA } AwsEventType; typedef enum { WS_EVT_CONNECT, WS_EVT_DISCONNECT, WS_EVT_PONG, WS_EVT_ERROR, WS_EVT_DATA } AwsEventType;
class AsyncWebSocketMessage class AsyncWebSocketMessage
{ {
private: private:
@ -161,7 +164,26 @@ class AsyncWebSocketClient {
} }
//data packets //data packets
void message(std::shared_ptr<std::vector<uint8_t>> buffer, uint8_t opcode=WS_TEXT, bool mask=false) { _queueMessage(buffer, opcode, mask); } /**
* @brief allocate memory buffer owned by shared-pointer and copy provided data
* used to keep the data untill websocket send is complete for single/multiple clients
*
* @param message
* @param len
* @return AsyncWebSocketMessageBuffer
*/
AsyncWebSocketMessageBuffer makeBuffer(const uint8_t *message, size_t len);
/**
* @brief allocate empty memory buffer owned by shared-pointer
* used to keep the data untill websocket send is complete for single/multiple clients
*
* @param len
* @return AsyncWebSocketMessageBuffer
*/
inline AsyncWebSocketMessageBuffer makeBuffer(size_t len){ return std::make_shared<std::vector<uint8_t>>(len); };
void message(AsyncWebSocketMessageBuffer buffer, uint8_t opcode=WS_TEXT, bool mask=false) { _queueMessage(buffer, opcode, mask); }
bool queueIsFull() const; bool queueIsFull() const;
size_t queueLen() const; size_t queueLen() const;
@ -170,18 +192,18 @@ class AsyncWebSocketClient {
size_t printf_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3))); size_t printf_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3)));
#endif #endif
void text(std::shared_ptr<std::vector<uint8_t>> buffer); inline void text(AsyncWebSocketMessageBuffer buffer){ _queueMessage(buffer); };
void text(const uint8_t *message, size_t len); inline void text(const uint8_t *message, size_t len){ text(makeBuffer(message, len)); };
void text(const char *message, size_t len); inline void text(const char *message, size_t len){ text((const uint8_t *)message, len); };
void text(const char *message); inline void text(const char *message){ text(message, strlen(message)); };
void text(const String &message); inline void text(const String &message){ text(message.c_str(), message.length()); };
void text(const __FlashStringHelper *message); void text(const __FlashStringHelper *message);
void binary(std::shared_ptr<std::vector<uint8_t>> buffer); inline void binary(AsyncWebSocketMessageBuffer buffer){ _queueMessage(buffer, WS_BINARY); };
void binary(const uint8_t *message, size_t len); inline void binary(const uint8_t *message, size_t len){ binary(makeBuffer(message, len)); };
void binary(const char * message, size_t len); inline void binary(const char * message, size_t len){ binary((const uint8_t *)message, len); };
void binary(const char * message); inline void binary(const char * message){ binary(message, strlen(message)); };
void binary(const String &message); inline void binary(const String &message){ binary(message.c_str(), message.length()); };
void binary(const __FlashStringHelper *message, size_t len); void binary(const __FlashStringHelper *message, size_t len);
bool canSend() const; bool canSend() const;
@ -205,7 +227,7 @@ class AsyncWebSocket: public AsyncWebHandler {
std::list<AsyncWebSocketClient> _clients; std::list<AsyncWebSocketClient> _clients;
uint32_t _cNextId; uint32_t _cNextId;
AwsEventHandler _eventHandler; AwsEventHandler _eventHandler;
AwsHandshakeHandler _handshakeHandler; AwsHandshakeHandler _handshakeHandler;
bool _enabled; bool _enabled;
AsyncWebLock _lock; AsyncWebLock _lock;
@ -229,26 +251,48 @@ class AsyncWebSocket: public AsyncWebHandler {
void ping(uint32_t id, const uint8_t *data=NULL, size_t len=0); void ping(uint32_t id, const uint8_t *data=NULL, size_t len=0);
void pingAll(const uint8_t *data=NULL, size_t len=0); // done void pingAll(const uint8_t *data=NULL, size_t len=0); // done
//data packets
/**
* @brief allocate memory buffer owned by shared-pointer and copy provided data
* used to keep the data untill websocket send is complete for single/multiple clients
*
* @param message
* @param len
* @return AsyncWebSocketMessageBuffer
*/
AsyncWebSocketMessageBuffer makeBuffer(const uint8_t *message, size_t len);
/**
* @brief allocate empty memory buffer owned by shared-pointer
* used to keep the data untill websocket send is complete for single/multiple clients
*
* @param len
* @return AsyncWebSocketMessageBuffer
*/
inline AsyncWebSocketMessageBuffer makeBuffer(size_t len){ return std::make_shared<std::vector<uint8_t>>(len); };
void text(uint32_t id, AsyncWebSocketMessageBuffer message);
void text(uint32_t id, const uint8_t * message, size_t len); void text(uint32_t id, const uint8_t * message, size_t len);
void text(uint32_t id, const char *message, size_t len); inline void text(uint32_t id, const char *message, size_t len){ text(id, (const uint8_t *)message, len); };
void text(uint32_t id, const char *message); inline void text(uint32_t id, const char *message){ text(id, message, strlen(message)); };
void text(uint32_t id, const String &message); inline void text(uint32_t id, const String &message){ text(id, message.c_str(), message.length()); };
void text(uint32_t id, const __FlashStringHelper *message); void text(uint32_t id, const __FlashStringHelper *message);
void textAll(std::shared_ptr<std::vector<uint8_t>> buffer); void textAll(AsyncWebSocketMessageBuffer buffer);
void textAll(const uint8_t *message, size_t len); void textAll(const uint8_t *message, size_t len);
void textAll(const char * message, size_t len); void textAll(const char * message, size_t len);
void textAll(const char * message); void textAll(const char * message);
void textAll(const String &message); void textAll(const String &message);
void textAll(const __FlashStringHelper *message); // need to convert void textAll(const __FlashStringHelper *message); // need to convert
void binary(uint32_t id, AsyncWebSocketMessageBuffer message);
void binary(uint32_t id, const uint8_t *message, size_t len); void binary(uint32_t id, const uint8_t *message, size_t len);
void binary(uint32_t id, const char *message, size_t len); void binary(uint32_t id, const char *message, size_t len);
void binary(uint32_t id, const char *message); void binary(uint32_t id, const char *message);
void binary(uint32_t id, const String &message); void binary(uint32_t id, const String &message);
void binary(uint32_t id, const __FlashStringHelper *message, size_t len); void binary(uint32_t id, const __FlashStringHelper *message, size_t len);
void binaryAll(std::shared_ptr<std::vector<uint8_t>> buffer); void binaryAll(AsyncWebSocketMessageBuffer buffer);
void binaryAll(const uint8_t *message, size_t len); void binaryAll(const uint8_t *message, size_t len);
void binaryAll(const char *message, size_t len); void binaryAll(const char *message, size_t len);
void binaryAll(const char *message); void binaryAll(const char *message);

View File

@ -1,6 +1,8 @@
#include "SPIFFSEditor.h" #include "SPIFFSEditor.h"
#include <FS.h> #include <FS.h>
#define EDFS
#ifndef EDFS #ifndef EDFS
#include "edit.htm.gz.h" #include "edit.htm.gz.h"
#endif #endif

View File

@ -37,7 +37,7 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
bool _fileExists(AsyncWebServerRequest *request, const String& path); bool _fileExists(AsyncWebServerRequest *request, const String& path);
uint8_t _countBits(const uint8_t value) const; uint8_t _countBits(const uint8_t value) const;
protected: protected:
FS &_fs; FS _fs;
String _uri; String _uri;
String _path; String _path;
String _default_file; String _default_file;