From dc10be7e8f00e75037444a2d941d874da9f52530 Mon Sep 17 00:00:00 2001 From: lbovay Date: Wed, 23 Nov 2016 16:16:22 +0100 Subject: [PATCH 01/21] Replaced quotes --- library.json | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library.json b/library.json index 20d34fe..f3e510d 100644 --- a/library.json +++ b/library.json @@ -1,14 +1,13 @@ { - "name": “easyWebSocket”, - "keywords": “esp8266 web socket websocket”, - "description": “A simple websocket server for esp8266. Creates and manages websocket connections using the esp8266 openSDK tcp functions. Interfaces to Arduino (and other) applications through straight forward callbacks.“, + "name": "easyWebSocket", + "keywords": "esp8266 web socket websocket", + "description": "A simple websocket server for esp8266. Creates and manages websocket connections using the esp8266 openSDK tcp functions. Interfaces to Arduino (and other) applications through straight forward callbacks.", "repository": { "type": "git", "url": "https://github.com/Coopdis/easyWebSocket" }, - "version": “1.0.0”, - "frameworks": “*”, + "version": "1.0.0", + "frameworks": "*", "platforms": "esp8266" } - From f39b6dfb6f455678aa7545a623d654eaa8f27671 Mon Sep 17 00:00:00 2001 From: aessig Date: Wed, 23 Nov 2016 16:18:11 +0100 Subject: [PATCH 02/21] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..7f65a82 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# easyWebSocket +A simple web socket deamon for use with the esp8266 From 7231f272264868ec77725c6a87ada7f5a8c6d4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 18:08:42 +0100 Subject: [PATCH 03/21] update protocol --- src/easyWebSocket.cpp | 93 ++++++++++++++++++++++++++++++------------- src/easyWebSocket.h | 13 ++++-- 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index b0c9891..bb38012 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -26,15 +26,15 @@ void ICACHE_FLASH_ATTR webSocketInit( void ) { webSocketConn.proto.tcp = &webSocketTcp; webSocketConn.proto.tcp->local_port = WEB_SOCKET_PORT; espconn_regist_connectcb(&webSocketConn, webSocketConnectCb); - + espconn_set_opt( &webSocketConn, ESPCONN_NODELAY ); // remove nagle for low latency - + sint8 ret = espconn_accept(&webSocketConn); if ( ret == 0 ) webSocketDebug("webSocket server established on port %d\n", WEB_SOCKET_PORT ); else webSocketDebug("webSocket server on port %d FAILED ret=%d\n", WEB_SOCKET_PORT, ret); - + return; } @@ -56,7 +56,7 @@ void ICACHE_FLASH_ATTR webSocketConnectCb(void *arg) { // set time out for this connection in seconds espconn_regist_time( connection, 120, 1); - + //find an empty slot uint8_t slotId = 0; while (wsConnections[slotId].connection != NULL && wsConnections[slotId].status != STATUS_CLOSED && slotId < WS_MAXCONN) { @@ -306,30 +306,72 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, uint8_t options) { // webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); - uint8_t payloadLengthField[9]; - uint8_t payloadLengthFieldLength = 0; - - if (payloadLength > ((1 << 16) - 1)) { - payloadLengthField[0] = 127; - os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); - payloadLengthFieldLength = sizeof(uint32_t) + 1; - } else if (payloadLength > ((1 << 8) - 1)) { - payloadLengthField[0] = 126; - os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); - payloadLengthFieldLength = sizeof(uint16_t) + 1; + // uint8_t payloadLengthField[9]; + // uint8_t payloadLengthFieldLength = 0; + // + // if (payloadLength > ((1 << 16) - 1)) { + // payloadLengthField[0] = 127; + // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); + // payloadLengthFieldLength = sizeof(uint32_t) + 1; + // } else if (payloadLength > ((1 << 8) - 1)) { + // payloadLengthField[0] = 126; + // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); + // payloadLengthFieldLength = sizeof(uint16_t) + 1; + // } else { + // payloadLengthField[0] = payloadLength; + // payloadLengthFieldLength = 1; + // } + // + // uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size + // char message[maximumPossibleMessageSize]; + // message[0] = FLAG_FIN | options; + // + // os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); + // os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); + // + // espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + + ////////////// + + uint8_t mask[4]; + uint32_t size = payloadLength; + + uint64_t maximumPossibleMessageSize = 14 + size; //14 bytes is the biggest frame header size + char message[maximumPossibleMessageSize]; + + // Opcode; final fragment + message[0] = FLAG_FIN | options; + + // NOTE: no support for > 16-bit sized messages + int i = 0; + if (size > 125) { + message[1] = WS_SIZE16 | WS_MASK; + message[2] = (uint8_t) (size >> 8); + message[3] = (uint8_t) (size & 0xFF); + i = 3; } else { - payloadLengthField[0] = payloadLength; - payloadLengthFieldLength = 1; + message[1] = (uint8_t) size | WS_MASK; + i = 1; } - uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size - char message[maximumPossibleMessageSize]; - message[0] = FLAG_FIN | options; + mask[0] = random(0, 256); + mask[1] = random(0, 256); + mask[2] = random(0, 256); + mask[3] = random(0, 256); + + message[i+1] = mask[0]; + message[i+2] = mask[1]; + message[i+3] = mask[2]; + message[i+4] = mask[3]; - os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); - os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); + int payloadSize = 0; + for (int j=0; jconnection, (uint8_t *)&message, payloadSize); - espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); } //*********************************************************************** @@ -359,8 +401,3 @@ void ICACHE_FLASH_ATTR webSocketDisconCb(void *arg) { void ICACHE_FLASH_ATTR webSocketReconCb(void *arg, sint8 err) { webSocketDebug("In webSocket_server_recon_cb err=%d\n", err ); } - - - - - diff --git a/src/easyWebSocket.h b/src/easyWebSocket.h index c66f4d8..67f1ae2 100644 --- a/src/easyWebSocket.h +++ b/src/easyWebSocket.h @@ -10,7 +10,7 @@ #define WS_RESPONSE "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n" #define HTML_HEADER_LINEEND "\r\n" -//we normally dont need that many connection, however a single +//we normally dont need that many connection, however a single //connection only allocates a WSConnection struct and is therefore really small #define WS_MAXCONN 4 #define CONN_TIMEOUT 60*60*12 @@ -48,6 +48,11 @@ #define OPCODE_PING 0x9 #define OPCODE_PONG 0xA +#define WS_MASK 0x80 + +#define WS_SIZE16 126 +#define WS_SIZE64 127 + #define FLAGS_MASK ((uint8_t)0xF0) #define OPCODE_MASK ((uint8_t)0x0F) #define IS_MASKED ((uint8_t)(1<<7)) @@ -83,14 +88,14 @@ struct WSConnection { void inline webSocketDebug( const char* format ... ) { char str[200]; - + va_list args; va_start(args, format); - + vsnprintf(str, sizeof(str), format, args); Serial.print( str ); // broadcastWsMessage(str, strlen(str), OPCODE_TEXT); - + va_end(args); } From e6858d1610593e0d2eeae3c6d5badbca87f4298e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 18:19:35 +0100 Subject: [PATCH 04/21] revert to previous version with small trick --- src/easyWebSocket.cpp | 116 +++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index bb38012..5c9e7e2 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -306,71 +306,71 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, uint8_t options) { // webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); - // uint8_t payloadLengthField[9]; - // uint8_t payloadLengthFieldLength = 0; - // - // if (payloadLength > ((1 << 16) - 1)) { - // payloadLengthField[0] = 127; - // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); - // payloadLengthFieldLength = sizeof(uint32_t) + 1; - // } else if (payloadLength > ((1 << 8) - 1)) { - // payloadLengthField[0] = 126; - // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); - // payloadLengthFieldLength = sizeof(uint16_t) + 1; - // } else { - // payloadLengthField[0] = payloadLength; - // payloadLengthFieldLength = 1; - // } - // - // uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size - // char message[maximumPossibleMessageSize]; - // message[0] = FLAG_FIN | options; - // - // os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); - // os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - // - // espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); - - ////////////// - - uint8_t mask[4]; - uint32_t size = payloadLength; + uint8_t payloadLengthField[9]; + uint8_t payloadLengthFieldLength = 0; + + if (payloadLength > ((1 << 16) - 1)) { + payloadLengthField[0] = 127; + os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); + payloadLengthFieldLength = sizeof(uint32_t) + 1; + } else if (payloadLength > 125) { //((1 << 8) - 1) + payloadLengthField[0] = 126; + os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); + payloadLengthFieldLength = sizeof(uint16_t) + 1; + } else { + payloadLengthField[0] = payloadLength; + payloadLengthFieldLength = 1; + } - uint64_t maximumPossibleMessageSize = 14 + size; //14 bytes is the biggest frame header size + uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size char message[maximumPossibleMessageSize]; - - // Opcode; final fragment message[0] = FLAG_FIN | options; - // NOTE: no support for > 16-bit sized messages - int i = 0; - if (size > 125) { - message[1] = WS_SIZE16 | WS_MASK; - message[2] = (uint8_t) (size >> 8); - message[3] = (uint8_t) (size & 0xFF); - i = 3; - } else { - message[1] = (uint8_t) size | WS_MASK; - i = 1; - } - - mask[0] = random(0, 256); - mask[1] = random(0, 256); - mask[2] = random(0, 256); - mask[3] = random(0, 256); + os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); + os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - message[i+1] = mask[0]; - message[i+2] = mask[1]; - message[i+3] = mask[2]; - message[i+4] = mask[3]; + espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); - int payloadSize = 0; - for (int j=0; jconnection, (uint8_t *)&message, payloadSize); + // uint8_t mask[4]; + // uint32_t size = payloadLength; + // + // uint64_t maximumPossibleMessageSize = 14 + size; //14 bytes is the biggest frame header size + // char message[maximumPossibleMessageSize]; + // + // // Opcode; final fragment + // message[0] = FLAG_FIN | options; + // + // // NOTE: no support for > 16-bit sized messages + // int i = 0; + // if (size > 125) { + // message[1] = WS_SIZE16 | WS_MASK; + // message[2] = (uint8_t) (size >> 8); + // message[3] = (uint8_t) (size & 0xFF); + // i = 3; + // } else { + // message[1] = (uint8_t) size | WS_MASK; + // i = 1; + // } + // + // // mask[0] = random(0, 256); + // // mask[1] = random(0, 256); + // // mask[2] = random(0, 256); + // // mask[3] = random(0, 256); + // // + // // message[i+1] = mask[0]; + // // message[i+2] = mask[1]; + // // message[i+3] = mask[2]; + // // message[i+4] = mask[3]; + // + // int payloadSize = 0; + // for (int j=0; jconnection, (uint8_t *)&message, payloadSize); } From 4b49d81972f37223cc3dceb9b5c6193cd804553a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 18:49:51 +0100 Subject: [PATCH 05/21] quick fix --- src/easyWebSocket.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 5c9e7e2..c00cd67 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -304,7 +304,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, const char *payload, uint32_t payloadLength, uint8_t options) { - // webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); + webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; @@ -322,6 +322,9 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthFieldLength = 1; } + webSocketDebug(payloadLengthField); + webSocketDebug(payloadLengthFieldLength); + uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size char message[maximumPossibleMessageSize]; message[0] = FLAG_FIN | options; @@ -329,6 +332,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); + webSocketDebug(message); + espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); ////////////// From 9f207a165416ae72cd68bbe88a88c047b6347214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 18:52:51 +0100 Subject: [PATCH 06/21] wtf --- src/easyWebSocket.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index c00cd67..49a48d6 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -322,8 +322,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthFieldLength = 1; } - webSocketDebug(payloadLengthField); - webSocketDebug(payloadLengthFieldLength); + webSocketDebug("%d %d %d %d %d %d %d %d %d\n", payloadLengthField[0], payloadLengthField[1], payloadLengthField[2], payloadLengthField[3], payloadLengthField[4], payloadLengthField[5], payloadLengthField[6],payloadLengthField[7], payloadLengthField[8] ); + webSocketDebug("%d\n", payloadLengthFieldLength); uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size char message[maximumPossibleMessageSize]; @@ -332,7 +332,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - webSocketDebug(message); + webSocketDebug("%s\n", message); espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); From d34e0bdee5a8401d64a774e2dbe091139d7c5118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 19:09:41 +0100 Subject: [PATCH 07/21] wtf2 --- src/easyWebSocket.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 49a48d6..cea4490 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -315,7 +315,12 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthFieldLength = sizeof(uint32_t) + 1; } else if (payloadLength > 125) { //((1 << 8) - 1) payloadLengthField[0] = 126; - os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); + if (payloadLength > ((1 << 8) - 1)) { + os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); + } else { + payloadLengthField[1] = 0; + os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); + } payloadLengthFieldLength = sizeof(uint16_t) + 1; } else { payloadLengthField[0] = payloadLength; From 0228853fe11cd895fc2888272711c01debb356d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 19:24:22 +0100 Subject: [PATCH 08/21] fixe2 --- src/easyWebSocket.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index cea4490..7df626a 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -315,12 +315,13 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthFieldLength = sizeof(uint32_t) + 1; } else if (payloadLength > 125) { //((1 << 8) - 1) payloadLengthField[0] = 126; - if (payloadLength > ((1 << 8) - 1)) { - os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint16_t)); - } else { - payloadLengthField[1] = 0; - os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); - } + os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); + // if (payloadLength > ((1 << 8) - 1)) { + // + // } else { + // payloadLengthField[1] = 0; + // os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); + // } payloadLengthFieldLength = sizeof(uint16_t) + 1; } else { payloadLengthField[0] = payloadLength; From 6c2034f41bb5586f42aeb80455f9b5646063c4b0 Mon Sep 17 00:00:00 2001 From: lbovay Date: Fri, 25 Nov 2016 21:17:45 +0100 Subject: [PATCH 09/21] Fix for numbers up to 255 --- src/easyWebSocket.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 7df626a..e0e81e4 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -311,18 +311,24 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, if (payloadLength > ((1 << 16) - 1)) { payloadLengthField[0] = 127; - os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); - payloadLengthFieldLength = sizeof(uint32_t) + 1; + // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); + // payloadLengthFieldLength = sizeof(uint32_t) + 1; + payloadLengthFieldLength = 9; } else if (payloadLength > 125) { //((1 << 8) - 1) payloadLengthField[0] = 126; - os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); + + for (int i =2; i>0; i--){ + uint8_t b = (payloadLength >>((i-1) *8)) & 0xff; + os_memcpy(payloadLengthField +(3 -i), &b, sizeof(uint8_t)); + } // if (payloadLength > ((1 << 8) - 1)) { // // } else { // payloadLengthField[1] = 0; // os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); // } - payloadLengthFieldLength = sizeof(uint16_t) + 1; + // payloadLengthFieldLength = sizeof(uint16_t) + 1; + payloadLengthFieldLength = 3; } else { payloadLengthField[0] = payloadLength; payloadLengthFieldLength = 1; From f6ba1d89e1ac75922b20ce8685c1d531e0b9874d Mon Sep 17 00:00:00 2001 From: lbovay Date: Fri, 25 Nov 2016 21:22:03 +0100 Subject: [PATCH 10/21] Tentative fix for payloadlength up to 9 bytes --- src/easyWebSocket.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index e0e81e4..c2d3f6d 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -313,6 +313,10 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthField[0] = 127; // os_memcpy(payloadLengthField + 1, &payloadLength, sizeof(uint32_t)); // payloadLengthFieldLength = sizeof(uint32_t) + 1; + for (int i =9; i>0; i--){ + uint8_t b = (payloadLength >>((i-1) *8)) & 0xff; + os_memcpy(payloadLengthField +(10 -i), &b, sizeof(uint8_t)); + } payloadLengthFieldLength = 9; } else if (payloadLength > 125) { //((1 << 8) - 1) payloadLengthField[0] = 126; @@ -321,13 +325,6 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, uint8_t b = (payloadLength >>((i-1) *8)) & 0xff; os_memcpy(payloadLengthField +(3 -i), &b, sizeof(uint8_t)); } - // if (payloadLength > ((1 << 8) - 1)) { - // - // } else { - // payloadLengthField[1] = 0; - // os_memcpy(payloadLengthField + 2, &payloadLength, sizeof(uint16_t)); - // } - // payloadLengthFieldLength = sizeof(uint16_t) + 1; payloadLengthFieldLength = 3; } else { payloadLengthField[0] = payloadLength; From 46be567640bd6ebdcccc548c8da6cc5dd7cf0f1a Mon Sep 17 00:00:00 2001 From: lbovay Date: Fri, 25 Nov 2016 21:36:02 +0100 Subject: [PATCH 11/21] Seems to work, +cleanup --- src/easyWebSocket.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index c2d3f6d..019dbe6 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -304,7 +304,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, const char *payload, uint32_t payloadLength, uint8_t options) { - webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); + // webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; @@ -331,8 +331,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, payloadLengthFieldLength = 1; } - webSocketDebug("%d %d %d %d %d %d %d %d %d\n", payloadLengthField[0], payloadLengthField[1], payloadLengthField[2], payloadLengthField[3], payloadLengthField[4], payloadLengthField[5], payloadLengthField[6],payloadLengthField[7], payloadLengthField[8] ); - webSocketDebug("%d\n", payloadLengthFieldLength); + // webSocketDebug("%d %d %d %d %d %d %d %d %d\n", payloadLengthField[0], payloadLengthField[1], payloadLengthField[2], payloadLengthField[3], payloadLengthField[4], payloadLengthField[5], payloadLengthField[6],payloadLengthField[7], payloadLengthField[8] ); + // webSocketDebug("%d\n", payloadLengthFieldLength); uint64_t maximumPossibleMessageSize = 14 + payloadLength; //14 bytes is the biggest frame header size char message[maximumPossibleMessageSize]; @@ -341,7 +341,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - webSocketDebug("%s\n", message); + // webSocketDebug("%s\n", message); espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); From 7bc418d6b7bda3524547447abdb073a8035fd314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Fri, 25 Nov 2016 23:57:27 +0100 Subject: [PATCH 12/21] added comment line for debug purpose --- src/easyWebSocket.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 019dbe6..577470d 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -304,7 +304,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, const char *payload, uint32_t payloadLength, uint8_t options) { - // webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); + + webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; From df6a0156b0fc81554a0d1fdf285fcae5f8ef02ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Sat, 26 Nov 2016 00:57:13 +0100 Subject: [PATCH 13/21] fix for debug --- src/easyWebSocket.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 577470d..dbac5d8 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -342,7 +342,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - // webSocketDebug("%s\n", message); + webSocketDebug("message -> %s\n", message); espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); From 464ff67dfbfbcbf898c428356bbdda85dd9e6eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Sat, 26 Nov 2016 01:10:59 +0100 Subject: [PATCH 14/21] comment for debug --- src/easyWebSocket.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index dbac5d8..be3acdd 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -342,9 +342,11 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - webSocketDebug("message -> %s\n", message); + // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); - espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + int result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + + webSocketDebug("sendMessageResult -> %d \n", result); ////////////// From 090e4a732ee05cddfac0098199b4dd9eb3e449b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 10:57:19 +0100 Subject: [PATCH 15/21] added full mem protection loop --- src/easyWebSocket.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index be3acdd..4fa790f 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -344,6 +344,11 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); + while (true) { + webSocketDebug("espconn_state -> %d \n", espconn.state); + if (espconn.state != 0) { delay(1); } else { break; } + } + int result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); webSocketDebug("sendMessageResult -> %d \n", result); From 4b398309cd36917e36f26ca95d8b91937e19d328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 11:04:23 +0100 Subject: [PATCH 16/21] quick fix --- src/easyWebSocket.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 4fa790f..20aa598 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -345,8 +345,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); while (true) { - webSocketDebug("espconn_state -> %d \n", espconn.state); - if (espconn.state != 0) { delay(1); } else { break; } + webSocketDebug("espconn_state -> %d \n", webSocketConn.state); + if (webSocketConn.state != ESPCONN_WAIT) { delay(1); } else { break; } } int result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); From bfd3b6a7b2009a79969bb995f6efb861fd2585fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 11:10:03 +0100 Subject: [PATCH 17/21] quick fix --- src/easyWebSocket.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 20aa598..a0aa9b9 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -344,9 +344,10 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); - while (true) { + int i = 0; + while (true && i < 5) { webSocketDebug("espconn_state -> %d \n", webSocketConn.state); - if (webSocketConn.state != ESPCONN_WAIT) { delay(1); } else { break; } + if (webSocketConn.state != ESPCONN_WAIT) { delay(1); i++; } else { break; } } int result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); From bd68ede92b7674df7cbfaf2173dc11a2ef784d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 12:10:01 +0100 Subject: [PATCH 18/21] update send protection loop --- src/easyWebSocket.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index a0aa9b9..1ac3fe9 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -305,7 +305,7 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, uint32_t payloadLength, uint8_t options) { - webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload,payloadLength); + webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload, payloadLength); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; @@ -343,16 +343,22 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); - int i = 0; while (true && i < 5) { - webSocketDebug("espconn_state -> %d \n", webSocketConn.state); - if (webSocketConn.state != ESPCONN_WAIT) { delay(1); i++; } else { break; } - } - int result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + sint8 result = ESPCONN_OK; + result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + + if (result != ESPCONN_OK) { + webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p\ \n", result, connection); + delay(1); + webSocketDebug("sendWsMessage: millis: %d\ \n", millis()); + i++; + } else { + break; + } - webSocketDebug("sendMessageResult -> %d \n", result); + } ////////////// From 804f1555a7fb90c944cce83d83bdbfafe8bb533d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 12:11:11 +0100 Subject: [PATCH 19/21] quick fix --- src/easyWebSocket.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 1ac3fe9..c0dd7c0 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -350,9 +350,9 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); if (result != ESPCONN_OK) { - webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p\ \n", result, connection); + webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p \n", result, connection); delay(1); - webSocketDebug("sendWsMessage: millis: %d\ \n", millis()); + webSocketDebug("sendWsMessage: millis: %d \n", millis()); i++; } else { break; From fe5bc533275af7722cf594712a1990639d5b4ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aure=CC=81lien=20Essig?= Date: Mon, 28 Nov 2016 21:17:01 +0100 Subject: [PATCH 20/21] latest non working version of multi connection multi frames version --- src/easyWebSocket.cpp | 167 ++++++++++++++++++++++-------------------- src/easyWebSocket.h | 19 +++-- 2 files changed, 101 insertions(+), 85 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index c0dd7c0..71fddb3 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -14,9 +14,15 @@ extern "C" { espconn webSocketConn; esp_tcp webSocketTcp; +// Global values +// static bool readytosend = true; +// static char txbuffer2[MAX_TXBUFFER]; +// static uint16 txbufferlen = 0; + static void (*wsOnConnectionCallback)(void); static void (*wsOnMessageCallback)( char *payloadData ); +static char txbuffer[WS_MAXCONN][MAX_TXBUFFER]; static WSConnection wsConnections[WS_MAXCONN]; //*********************************************************************** @@ -30,10 +36,11 @@ void ICACHE_FLASH_ATTR webSocketInit( void ) { espconn_set_opt( &webSocketConn, ESPCONN_NODELAY ); // remove nagle for low latency sint8 ret = espconn_accept(&webSocketConn); - if ( ret == 0 ) + if ( ret == 0 ) { webSocketDebug("webSocket server established on port %d\n", WEB_SOCKET_PORT ); - else + } else { webSocketDebug("webSocket server on port %d FAILED ret=%d\n", WEB_SOCKET_PORT, ret); + } return; } @@ -54,8 +61,8 @@ void ICACHE_FLASH_ATTR webSocketConnectCb(void *arg) { webSocketDebug("\n\nmeshWebSocket received connection !!!\n"); - // set time out for this connection in seconds - espconn_regist_time( connection, 120, 1); + // set time out for this connection in seconds + espconn_regist_time(connection, 120, 1); //find an empty slot uint8_t slotId = 0; @@ -65,7 +72,6 @@ void ICACHE_FLASH_ATTR webSocketConnectCb(void *arg) { webSocketDebug("websocketConnectCb slotId=%d\n", slotId); - if (slotId >= WS_MAXCONN) { //no more free slots, close the connection webSocketDebug("No more free slots for WebSockets!\n"); @@ -73,22 +79,23 @@ void ICACHE_FLASH_ATTR webSocketConnectCb(void *arg) { return; } - // webSocketDebug("websocketConnectCb2\n"); + webSocketDebug("websocketConnectCb2\n"); - WSConnection wsConnection; - wsConnection.status = STATUS_UNINITIALISED; - wsConnection.connection = connection; - wsConnection.onMessage = wsOnMessageCallback; - wsConnections[slotId] = wsConnection; + wsConnections[slotId].status = STATUS_UNINITIALISED; + wsConnections[slotId].connection = connection; + wsConnections[slotId].onMessage = wsOnMessageCallback; + wsConnections[slotId].readytosend = true; + wsConnections[slotId].txbufferlen = 0; + wsConnections[slotId].txbuffer = txbuffer[slotId]; - // webSocketDebug("websocketConnectCb3\n"); + webSocketDebug("websocketConnectCb3\n"); espconn_regist_recvcb(connection, webSocketRecvCb); espconn_regist_sentcb(connection, webSocketSentCb); espconn_regist_reconcb(connection, webSocketReconCb); espconn_regist_disconcb(connection, webSocketDisconCb); - // webSocketDebug("leaving websocketConnectCb\n"); + webSocketDebug("leaving websocketConnectCb\n"); } //*********************************************************************** @@ -166,7 +173,7 @@ void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len return; } -// webSocketDebug("frame.payloadData-->%s<--\n", frame.payloadData); + // webSocketDebug("frame.payloadData-->%s<--\n", frame.payloadData); if (frame.opcode == OPCODE_PING) { // webSocketDebug("frame.opcode=OPCODE_PING\n"); @@ -230,21 +237,21 @@ static void ICACHE_FLASH_ATTR parseWsFrame(char *data, WSFrame *frame) { //*********************************************************************** WSConnection *ICACHE_FLASH_ATTR getWsConnection(struct espconn *connection) { -// webSocketDebug("In getWsConnecition\n"); + // webSocketDebug("In getWsConnecition\n"); for (int slotId = 0; slotId < WS_MAXCONN; slotId++) { -// webSocketDebug("slotId=%d, ws.conn*=%x, espconn*=%x<-- ", slotId, wsConnections[slotId].connection, connection); + // webSocketDebug("slotId=%d, ws.conn*=%x, espconn*=%x<-- ", slotId, wsConnections[slotId].connection, connection); // webSocketDebug("ws.connIP=%d.%d.%d.%d espconnIP=%d.%d.%d.%d --- ", IP2STR( wsConnections[slotId].connection->proto.tcp->remote_ip), IP2STR( connection->proto.tcp->remote_ip) ); -// webSocketDebug("ws.connIP=%x espconnIP=%x\n", *(uint32_t*)wsConnections[slotId].connection->proto.tcp->remote_ip, *(uint32_t*)connection->proto.tcp->remote_ip ) ; + // webSocketDebug("ws.connIP=%x espconnIP=%x\n", *(uint32_t*)wsConnections[slotId].connection->proto.tcp->remote_ip, *(uint32_t*)connection->proto.tcp->remote_ip ) ; // if (wsConnections[slotId].connection == connection) { if (*(uint32_t*)wsConnections[slotId].connection->proto.tcp->remote_ip == *(uint32_t*)connection->proto.tcp->remote_ip ) { - // webSocketDebug("Leaving getWsConnecition slotID=%d\n", slotId); + // webSocketDebug("Leaving getWsConnecition slotID=%d\n", slotId); return wsConnections + slotId; } } -// webSocketDebug("Leaving getWsConnecition w/ NULL\n"); + // webSocketDebug("Leaving getWsConnecition w/ NULL\n"); return NULL; } @@ -282,6 +289,7 @@ void ICACHE_FLASH_ATTR broadcastWsMessage(const char *payload, uint32_t payloadL for (int slotId = 0; slotId < WS_MAXCONN; slotId++) { WSConnection connection = wsConnections[slotId]; if (connection.connection != NULL && connection.status == STATUS_OPEN) { + webSocketDebug("broadcastWsMessage enter with conn: %p and add: %p\n", connection, &connection); sendWsMessage(&connection, payload, payloadLength, options); } } @@ -305,7 +313,8 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, uint32_t payloadLength, uint8_t options) { - webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d\n", payload, payloadLength); + webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d \n", payload, payloadLength); + webSocketDebug("sendWsMessage-->conn: %p \n", connection); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; @@ -342,80 +351,80 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, os_memcpy(message + 1, &payloadLengthField, payloadLengthFieldLength); os_memcpy(message + 1 + payloadLengthFieldLength, payload, strlen(payload)); - // webSocketDebug("message -> %s size -> %d \n", message, payloadLength + 1 + payloadLengthFieldLength); - int i = 0; - while (true && i < 5) { + // Fill up buffer with the data instead of sending it directly + webSocketDebug("message->%s\n", message); + espbuffsent(connection, (const char *)&message, payloadLength + 1 + payloadLengthFieldLength); - sint8 result = ESPCONN_OK; - result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + // sint8 result = ESPCONN_OK; + // ready_send_data = false; + // result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + // if (result != ESPCONN_OK) { + // webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p \n", result, connection); + // webSocketDebug("sendWsMessage: millis: %d \n", millis()); + // } + // break; + // } else { + // webSocketDebug("waiting for ready_send_data: %d - millis: %d\n", ready_send_data, millis()); + // static uint32_t tick = 0; + // if ( millis() - tick < 1000) { return; } + // /* Do stuff here every 1 second */ + // i++; + // tick = millis(); + // } + // } - if (result != ESPCONN_OK) { - webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p \n", result, connection); - delay(1); - webSocketDebug("sendWsMessage: millis: %d \n", millis()); - i++; - } else { - break; +} + +//*********************************************************************** +static sint8 ICACHE_FLASH_ATTR sendtxbuffer(WSConnection *connection) { + webSocketDebug("sendtxbuffer: conn: %p and readytosend %d\n", connection, connection->readytosend); + sint8 result = ESPCONN_OK; + if (connection->txbufferlen != 0) { + connection->readytosend = false; + result = espconn_sent(connection->connection, (uint8_t*)connection->txbuffer, connection->txbufferlen); + connection->txbufferlen = 0; + if (result != ESPCONN_OK) { + webSocketDebug("sendtxbuffer: espconn_sent error %d on conn %p\n", result, connection); } + } + return result; +} - } +sint8 ICACHE_FLASH_ATTR espbuffsent(WSConnection *connection, const char *data, uint16 len) { + webSocketDebug("espbuffsent: enter with conn %p\n", connection); - ////////////// - - // uint8_t mask[4]; - // uint32_t size = payloadLength; - // - // uint64_t maximumPossibleMessageSize = 14 + size; //14 bytes is the biggest frame header size - // char message[maximumPossibleMessageSize]; - // - // // Opcode; final fragment - // message[0] = FLAG_FIN | options; - // - // // NOTE: no support for > 16-bit sized messages - // int i = 0; - // if (size > 125) { - // message[1] = WS_SIZE16 | WS_MASK; - // message[2] = (uint8_t) (size >> 8); - // message[3] = (uint8_t) (size & 0xFF); - // i = 3; - // } else { - // message[1] = (uint8_t) size | WS_MASK; - // i = 1; - // } - // - // // mask[0] = random(0, 256); - // // mask[1] = random(0, 256); - // // mask[2] = random(0, 256); - // // mask[3] = random(0, 256); - // // - // // message[i+1] = mask[0]; - // // message[i+2] = mask[1]; - // // message[i+3] = mask[2]; - // // message[i+4] = mask[3]; - // - // int payloadSize = 0; - // for (int j=0; jconnection, (uint8_t *)&message, payloadSize); + if (connection->txbufferlen + len > MAX_TXBUFFER) { + webSocketDebug("espbuffsent: txbuffer full on conn %p\n", connection); + return -128; + } + os_memcpy(connection->txbuffer + connection->txbufferlen, data, len); + connection->txbufferlen += len; + webSocketDebug("espbuffsent: txbufferlen update success with readytosend %d\n", connection->readytosend); + if (connection->readytosend) { + return sendtxbuffer(connection); + } + return ESPCONN_OK; } //*********************************************************************** void ICACHE_FLASH_ATTR webSocketSentCb(void *arg) { - //data sent successfully - //webSocketDebug("webSocket sent cb \r\n"); - struct espconn *requestconn = (espconn *)arg; - // espconn_disconnect( requestconn ); + + espconn *esp_connection = (espconn*)arg; + WSConnection *wsConnection = getWsConnection(esp_connection); + + webSocketDebug("Sent callback on conn %p\n", wsConnection); + + if(wsConnection == NULL) return; + //wsConnection->readytosend = true; + wsConnection->readytosend = true; + sendtxbuffer(wsConnection); } /***********************************************************************/ void ICACHE_FLASH_ATTR webSocketDisconCb(void *arg) { espconn *esp_connection = (espconn*)arg; - - WSConnection *wsConn = getWsConnection( esp_connection); + WSConnection *wsConn = getWsConnection(esp_connection); if ( wsConn != NULL ) { wsConn->status = STATUS_CLOSED; webSocketDebug("Leaving webSocket_server_discon_cb found\n"); diff --git a/src/easyWebSocket.h b/src/easyWebSocket.h index 67f1ae2..6c9fde1 100644 --- a/src/easyWebSocket.h +++ b/src/easyWebSocket.h @@ -1,7 +1,7 @@ // adapted from https://github.com/dangrie158/ESP-8266-WebSocket -#ifndef _MESH_WEB_SOCKET_H_ -#define _MESH_WEB_SOCKET_H_ +#ifndef _EASY_WEB_SOCKET_H_ +#define _EASY_WEB_SOCKET_H_ #define WEB_SOCKET_PORT 2222 @@ -13,6 +13,7 @@ //we normally dont need that many connection, however a single //connection only allocates a WSConnection struct and is therefore really small #define WS_MAXCONN 4 +#define MAX_TXBUFFER 1024 #define CONN_TIMEOUT 60*60*12 /* from IEEE RFC6455 sec 5.2 @@ -82,11 +83,14 @@ struct WSFrame { struct WSConnection { uint8_t status; - struct espconn* connection; WSOnMessage onMessage; + struct espconn* connection; + char *txbuffer; //the buffer for the data to send + uint16 txbufferlen; //the length of data in txbuffer + bool readytosend; //true, if txbuffer can send by espconn_sent }; -void inline webSocketDebug( const char* format ... ) { +void inline webSocketDebug( const char* format ... ) { char str[200]; va_list args; @@ -118,12 +122,15 @@ static void ICACHE_FLASH_ATTR unmaskWsPayload(char *maskedPayload, void closeWsConnection(WSConnection* connection); void webSocketSetReceiveCallback( void (*onMessage)(char *paylodData) ); -void webSocketSetConnectionCallback( void (*onConnection)(void) ); +void webSocketSetConnectionCallback( void (*onConnection)(void) ); void webSocketConnectCb(void *arg); void webSocketRecvCb(void *arg, char *pusrdata, unsigned short length); void webSocketSentCb(void *arg); +void webSocketSentWriteFinishCb(void *arg); void webSocketDisconCb(void *arg); void webSocketReconCb(void *arg, sint8 err); -#endif //_MESH_WEB_SOCKET_H_ +sint8 ICACHE_FLASH_ATTR espbuffsent(WSConnection *connection, const char *data, uint16 len); + +#endif //_EASY_WEB_SOCKET_H_ From 0c2feba1514c611be52cf0b9171353901fbec49d Mon Sep 17 00:00:00 2001 From: lbovay Date: Tue, 29 Nov 2016 11:20:08 +0100 Subject: [PATCH 21/21] fix --- src/easyWebSocket.cpp | 91 ++++++++++++++++++++++--------------------- src/easyWebSocket.h | 8 ++-- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/easyWebSocket.cpp b/src/easyWebSocket.cpp index 71fddb3..56e7257 100644 --- a/src/easyWebSocket.cpp +++ b/src/easyWebSocket.cpp @@ -106,8 +106,8 @@ void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len //webSocketDebug("In webSocketRecvCb\n"); // webSocketDebug("webSocket recv--->%s<----\n", data); - WSConnection *wsConnection = getWsConnection(esp_connection); - if (wsConnection == NULL) { + int slotId = getWsConnection(esp_connection); + if (wsConnections[slotId].connection == NULL) { webSocketDebug("webSocket Heh?\n"); return; } @@ -148,7 +148,7 @@ void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len //send the response espconn_sent(esp_connection, (uint8_t *)responseMessage, strlen(responseMessage)); - wsConnection->status = STATUS_OPEN; + wsConnections[slotId].status = STATUS_OPEN; //call the connection callback if (wsOnConnectionCallback != NULL) { @@ -169,7 +169,7 @@ void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len //we are the server, and need to shut down the connection //if we receive an unmasked packet // webSocketDebug("frame.isMasked=false closing connection\n"); - closeWsConnection(wsConnection); + closeWsConnection(slotId); return; } @@ -177,19 +177,19 @@ void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len if (frame.opcode == OPCODE_PING) { // webSocketDebug("frame.opcode=OPCODE_PING\n"); - sendWsMessage(wsConnection, frame.payloadData, frame.payloadLength, FLAG_FIN | OPCODE_PONG); + sendWsMessage(slotId, frame.payloadData, frame.payloadLength, FLAG_FIN | OPCODE_PONG); return; } if (frame.opcode == OPCODE_CLOSE) { //gracefully shut down the connection // webSocketDebug("frame.opcode=OPCODE_CLOSE, closeing connection\n"); - closeWsConnection(wsConnection); + closeWsConnection(slotId); return; } - if (wsConnection->onMessage != NULL) { - wsConnection->onMessage(frame.payloadData); + if (wsConnections[slotId].onMessage != NULL) { + wsConnections[slotId].onMessage(frame.payloadData); } } // webSocketDebug("Leaving webSocketRecvCb\n"); @@ -236,18 +236,19 @@ static void ICACHE_FLASH_ATTR parseWsFrame(char *data, WSFrame *frame) { } //*********************************************************************** -WSConnection *ICACHE_FLASH_ATTR getWsConnection(struct espconn *connection) { +int ICACHE_FLASH_ATTR getWsConnection(struct espconn *connection) { // webSocketDebug("In getWsConnecition\n"); for (int slotId = 0; slotId < WS_MAXCONN; slotId++) { // webSocketDebug("slotId=%d, ws.conn*=%x, espconn*=%x<-- ", slotId, wsConnections[slotId].connection, connection); - // webSocketDebug("ws.connIP=%d.%d.%d.%d espconnIP=%d.%d.%d.%d --- ", IP2STR( wsConnections[slotId].connection->proto.tcp->remote_ip), IP2STR( connection->proto.tcp->remote_ip) ); - // webSocketDebug("ws.connIP=%x espconnIP=%x\n", *(uint32_t*)wsConnections[slotId].connection->proto.tcp->remote_ip, *(uint32_t*)connection->proto.tcp->remote_ip ) ; + // webSocketDebug("ws.connIP=%d.%d.%d.%d espconnIP=%d.%d.%d.%d --- ", IP2STR( wsConnections[slotId].wsConnections[slotId].proto.tcp->remote_ip), IP2STR( wsConnections[slotId].proto.tcp->remote_ip) ); + // webSocketDebug("ws.connIP=%x espconnIP=%x\n", *(uint32_t*)wsConnections[slotId].wsConnections[slotId].proto.tcp->remote_ip, *(uint32_t*)wsConnections[slotId].proto.tcp->remote_ip ) ; // if (wsConnections[slotId].connection == connection) { if (*(uint32_t*)wsConnections[slotId].connection->proto.tcp->remote_ip == *(uint32_t*)connection->proto.tcp->remote_ip ) { // webSocketDebug("Leaving getWsConnecition slotID=%d\n", slotId); - return wsConnections + slotId; + // return wsConnections + slotId; + return slotId; } } @@ -274,12 +275,12 @@ static int ICACHE_FLASH_ATTR createWsAcceptKey(const char *key, char *buffer, in } //*********************************************************************** -void ICACHE_FLASH_ATTR closeWsConnection(WSConnection * connection) { +void ICACHE_FLASH_ATTR closeWsConnection(int slotId) { // webSocketDebug("In closeWsConnection\n"); char closeMessage[CLOSE_MESSAGE_LENGTH] = CLOSE_MESSAGE; - espconn_sent(connection->connection, (uint8_t *)closeMessage, sizeof(closeMessage)); - connection->status = STATUS_CLOSED; + espconn_sent(wsConnections[slotId].connection, (uint8_t *)closeMessage, sizeof(closeMessage)); + wsConnections[slotId].status = STATUS_CLOSED; return; } @@ -290,7 +291,7 @@ void ICACHE_FLASH_ATTR broadcastWsMessage(const char *payload, uint32_t payloadL WSConnection connection = wsConnections[slotId]; if (connection.connection != NULL && connection.status == STATUS_OPEN) { webSocketDebug("broadcastWsMessage enter with conn: %p and add: %p\n", connection, &connection); - sendWsMessage(&connection, payload, payloadLength, options); + sendWsMessage(slotId, payload, payloadLength, options); } } } @@ -308,13 +309,13 @@ uint16_t ICACHE_FLASH_ATTR countWsConnections( void ) { } //*********************************************************************** -void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, +void ICACHE_FLASH_ATTR sendWsMessage(int slotId, const char *payload, uint32_t payloadLength, uint8_t options) { webSocketDebug("sendWsMessage-->%s<-- payloadLength=%d \n", payload, payloadLength); - webSocketDebug("sendWsMessage-->conn: %p \n", connection); + // webSocketDebug("sendWsMessage-->conn: %p \n", connection); uint8_t payloadLengthField[9]; uint8_t payloadLengthFieldLength = 0; @@ -353,11 +354,11 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, // Fill up buffer with the data instead of sending it directly webSocketDebug("message->%s\n", message); - espbuffsent(connection, (const char *)&message, payloadLength + 1 + payloadLengthFieldLength); + espbuffsent(slotId, (const char *)&message, payloadLength + 1 + payloadLengthFieldLength); // sint8 result = ESPCONN_OK; // ready_send_data = false; - // result = espconn_sent(connection->connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); + // result = espconn_sent(wsConnections[slotId].connection, (uint8_t *)&message, payloadLength + 1 + payloadLengthFieldLength); // if (result != ESPCONN_OK) { // webSocketDebug("sendWsMessage: espconn_sent error %d on conn %p \n", result, connection); // webSocketDebug("sendWsMessage: millis: %d \n", millis()); @@ -376,33 +377,33 @@ void ICACHE_FLASH_ATTR sendWsMessage(WSConnection *connection, } //*********************************************************************** -static sint8 ICACHE_FLASH_ATTR sendtxbuffer(WSConnection *connection) { - webSocketDebug("sendtxbuffer: conn: %p and readytosend %d\n", connection, connection->readytosend); +static sint8 ICACHE_FLASH_ATTR sendtxbuffer(int slotId) { + // webSocketDebug("sendtxbuffer: conn: %p and readytosend %d\n", connection, wsConnections[slotId].readytosend); sint8 result = ESPCONN_OK; - if (connection->txbufferlen != 0) { - connection->readytosend = false; - result = espconn_sent(connection->connection, (uint8_t*)connection->txbuffer, connection->txbufferlen); - connection->txbufferlen = 0; + if (wsConnections[slotId].txbufferlen != 0) { + wsConnections[slotId].readytosend = false; + result = espconn_sent((espconn*)wsConnections[slotId].connection, (uint8_t*)wsConnections[slotId].txbuffer, wsConnections[slotId].txbufferlen); + wsConnections[slotId].txbufferlen = 0; if (result != ESPCONN_OK) { - webSocketDebug("sendtxbuffer: espconn_sent error %d on conn %p\n", result, connection); + // webSocketDebug("sendtxbuffer: espconn_sent error %d on conn %p\n", result, connection); } } return result; } -sint8 ICACHE_FLASH_ATTR espbuffsent(WSConnection *connection, const char *data, uint16 len) { - webSocketDebug("espbuffsent: enter with conn %p\n", connection); +sint8 ICACHE_FLASH_ATTR espbuffsent(int slotId, const char *data, uint16 len) { + // webSocketDebug("espbuffsent: enter with conn %p\n", connection); - if (connection->txbufferlen + len > MAX_TXBUFFER) { - webSocketDebug("espbuffsent: txbuffer full on conn %p\n", connection); + if (wsConnections[slotId].txbufferlen + len > MAX_TXBUFFER) { + // webSocketDebug("espbuffsent: txbuffer full on conn %p\n", connection); return -128; } - os_memcpy(connection->txbuffer + connection->txbufferlen, data, len); - connection->txbufferlen += len; - webSocketDebug("espbuffsent: txbufferlen update success with readytosend %d\n", connection->readytosend); - if (connection->readytosend) { - return sendtxbuffer(connection); + os_memcpy(wsConnections[slotId].txbuffer + wsConnections[slotId].txbufferlen, data, len); + wsConnections[slotId].txbufferlen += len; + webSocketDebug("espbuffsent: txbufferlen update success with readytosend %d\n", wsConnections[slotId].readytosend); + if (wsConnections[slotId].readytosend) { + return sendtxbuffer(slotId); } return ESPCONN_OK; } @@ -411,22 +412,22 @@ sint8 ICACHE_FLASH_ATTR espbuffsent(WSConnection *connection, const char *data, void ICACHE_FLASH_ATTR webSocketSentCb(void *arg) { espconn *esp_connection = (espconn*)arg; - WSConnection *wsConnection = getWsConnection(esp_connection); + int slotId = getWsConnection(esp_connection); - webSocketDebug("Sent callback on conn %p\n", wsConnection); + // webSocketDebug("Sent callback on conn %p\n", wsConnection); - if(wsConnection == NULL) return; - //wsConnection->readytosend = true; - wsConnection->readytosend = true; - sendtxbuffer(wsConnection); + if(wsConnections[slotId].connection == NULL) return; + //wswsConnections[slotId].readytosend = true; + wsConnections[slotId].readytosend = true; + sendtxbuffer(slotId); } /***********************************************************************/ void ICACHE_FLASH_ATTR webSocketDisconCb(void *arg) { espconn *esp_connection = (espconn*)arg; - WSConnection *wsConn = getWsConnection(esp_connection); - if ( wsConn != NULL ) { - wsConn->status = STATUS_CLOSED; + int slotId = getWsConnection(esp_connection); + if ( wsConnections[slotId].connection != NULL ) { + wsConnections[slotId].status = STATUS_CLOSED; webSocketDebug("Leaving webSocket_server_discon_cb found\n"); return; } diff --git a/src/easyWebSocket.h b/src/easyWebSocket.h index 6c9fde1..832128a 100644 --- a/src/easyWebSocket.h +++ b/src/easyWebSocket.h @@ -105,7 +105,7 @@ void inline webSocketDebug( const char* format ... ) { void ICACHE_FLASH_ATTR webSocketInit( void ); -void ICACHE_FLASH_ATTR sendWsMessage(WSConnection* connection, +void ICACHE_FLASH_ATTR sendWsMessage(int slotId, const char* payload, uint32_t payloadLength, uint8_t options); @@ -113,13 +113,13 @@ void ICACHE_FLASH_ATTR broadcastWsMessage(const char* payload, uint32_t payloadLength, uint8_t options); uint16_t ICACHE_FLASH_ATTR countWsConnections( void ); -WSConnection *ICACHE_FLASH_ATTR getWsConnection(struct espconn *connection); +int ICACHE_FLASH_ATTR getWsConnection(struct espconn *connection); static int ICACHE_FLASH_ATTR createWsAcceptKey(const char *key, char *buffer, int bufferSize); static void ICACHE_FLASH_ATTR parseWsFrame(char *data, WSFrame *frame); static void ICACHE_FLASH_ATTR unmaskWsPayload(char *maskedPayload, uint32_t payloadLength, uint32_t maskingKey); -void closeWsConnection(WSConnection* connection); +void closeWsConnection(int slotId); void webSocketSetReceiveCallback( void (*onMessage)(char *paylodData) ); void webSocketSetConnectionCallback( void (*onConnection)(void) ); @@ -131,6 +131,6 @@ void webSocketSentWriteFinishCb(void *arg); void webSocketDisconCb(void *arg); void webSocketReconCb(void *arg, sint8 err); -sint8 ICACHE_FLASH_ATTR espbuffsent(WSConnection *connection, const char *data, uint16 len); +sint8 ICACHE_FLASH_ATTR espbuffsent(int slotId, const char *data, uint16 len); #endif //_EASY_WEB_SOCKET_H_