Added printf_P method (#31)

* Prevent buffer overflow on received data

* pass to 7 char to avoid save to flash by SDK

* return _contentLength, avoid array reparse to know len

* Added FlashStringHelper for text and binary

* Added FlashStringHelper also to AsyncWebSocketClient

* Added PROGMEM doc

* Corrected binary was sending PSTR as text, addded len

* Server calls client method and code as asked @me-no-dev

* server calls client method and code as asked by @me-no-dev

* Changed Code presentation

* Added printf_P methods
This commit is contained in:
Charles 2016-05-15 02:00:33 +02:00 committed by Me No Dev
parent 937d442fce
commit 29b3f72e80
3 changed files with 83 additions and 1 deletions

View File

@ -473,6 +473,10 @@ AsyncWebSocket ws("/ws");
ws.printf([client id], [arguments...]);
//printf to all clients
ws.printfAll([arguments...]);
//printf_P to a client
ws.printf_P([client id], PSTR([format]), [arguments...]);
//printfAll_P to all clients
ws.printf_P(PSTR([format]), [arguments...]);
//send text to a client
ws.text([client id], [(char*)text]);
ws.text([client id], [text], [len]);
@ -495,6 +499,8 @@ ws.binaryAll([binary], [len]);
AsyncWebSocketClient * client;
//printf to a client
client->printf([arguments...]);
//printf_P to a client
client->printf_P( PSTR([format]), [arguments...]);
//send text to a client
client->text([(char*)text]);
client->text([text], [len]);

View File

@ -489,6 +489,36 @@ size_t AsyncWebSocketClient::printf(const char *format, ...) {
return len;
}
size_t AsyncWebSocketClient::printf_P(PGM_P formatP, ...) {
char* format;
va_list arg;
va_start(arg, formatP);
#ifdef ESP8266
//ToDo: figure out a way around this
size_t len = 1440;
#else
size_t len = vsnprintf(NULL, 0, format, arg)+1;
#endif
size_t fmtLen = strlen_P(formatP);
format = (char*)calloc(fmtLen+1, sizeof(char));
if ( format ) {
strcpy_P(format, formatP);
char * msg = (char*)malloc(len+1);
if(msg == NULL){
va_end(arg);
return 0;
}
len = vsnprintf(msg, len, format, arg);
msg[len] = 0;
text(msg);
va_end(arg);
free(msg);
free(format);
}
return len;
}
void AsyncWebSocketClient::text(const char * message, size_t len){
_queueMessage(new AsyncWebSocketBasicMessage(message, len));
}
@ -740,6 +770,49 @@ size_t AsyncWebSocket::printfAll(const char *format, ...) {
return len;
}
size_t AsyncWebSocket::printf_P(uint32_t id, PGM_P formatP, ...){
AsyncWebSocketClient * c = client(id);
if(c != NULL){
va_list arg;
va_start(arg, formatP);
size_t len = c->printf_P(formatP, arg);
va_end(arg);
return len;
}
return 0;
}
size_t AsyncWebSocket::printfAll_P(PGM_P formatP, ...) {
char* format;
va_list arg;
va_start(arg, formatP);
#ifdef ESP8266
//ToDo: figure out a way around this
size_t len = 1440;
#else
size_t len = vsnprintf(NULL, 0, format, arg)+1;
#endif
size_t fmtLen = strlen_P(formatP);
format = (char*)calloc(fmtLen+1, sizeof(char));
if ( format ) {
strcpy_P(format, formatP);
char * msg = (char*)malloc(len+1);
if(msg == NULL){
va_end(arg);
free(format);
return 0;
}
len = vsnprintf(msg, len, format, arg);
msg[len] = 0;
textAll(msg);
va_end(arg);
free(msg);
free(format);
}
return len;
}
void AsyncWebSocket::text(uint32_t id, const char * message){
text(id, message, strlen(message));
}

View File

@ -100,7 +100,8 @@ class AsyncWebSocketClient {
void message(AsyncWebSocketMessage *message){ _queueMessage(message); }
size_t printf(const char *format, ...);
size_t printf_P(PGM_P formatP, ...);
void text(const char * message, size_t len);
void text(const char * message);
void text(uint8_t * message, size_t len);
@ -181,6 +182,8 @@ class AsyncWebSocket: public AsyncWebHandler {
size_t printf(uint32_t id, const char *format, ...);
size_t printfAll(const char *format, ...);
size_t printf_P(uint32_t id, PGM_P formatP, ...);
size_t printfAll_P(PGM_P formatP, ...);
//event listener
void onEvent(AwsEventHandler handler){