/* esp32-websocket - a websocket component on esp-idf Copyright (C) 2019 Blake Felt - blake.w.felt@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "websocket_server.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "freertos/queue.h" #include static SemaphoreHandle_t xwebsocket_mutex; // to lock the client array static QueueHandle_t xwebsocket_queue; // to hold the clients that send messages static ws_client_t clients[WEBSOCKET_SERVER_MAX_CLIENTS]; // holds list of clients static TaskHandle_t xtask; // the task itself static void background_callback(struct netconn* conn, enum netconn_evt evt,u16_t len) { switch(evt) { case NETCONN_EVT_RCVPLUS: xQueueSendToBack(xwebsocket_queue,&conn,WEBSOCKET_SERVER_QUEUE_TIMEOUT); break; default: break; } } static void handle_read(uint8_t num) { ws_header_t header; char* msg; header.received = 0; msg = ws_read(&clients[num],&header); if(!header.received) { if(msg) free(msg); return NULL; } switch(clients[num].last_opcode) { case WEBSOCKET_OPCODE_CONT: break; case WEBSOCKET_OPCODE_BIN: clients[num].scallback(num,WEBSOCKET_BIN,msg,header.length); break; case WEBSOCKET_OPCODE_TEXT: clients[num].scallback(num,WEBSOCKET_TEXT,msg,header.length); break; case WEBSOCKET_OPCODE_PING: ws_send(&clients[num],WEBSOCKET_OPCODE_PONG,msg,header.length,0); clients[num].scallback(num,WEBSOCKET_PING,msg,header.length); break; case WEBSOCKET_OPCODE_PONG: if(clients[num].ping) { clients[num].scallback(num,WEBSOCKET_PONG,NULL,0); clients[num].ping = 0; } break; case WEBSOCKET_OPCODE_CLOSE: clients[num].scallback(num,WEBSOCKET_DISCONNECT_EXTERNAL,NULL,0); ws_disconnect_client(&clients[num], 0); break; default: break; } if(msg) free(msg); } static void ws_server_task(void* pvParameters) { struct netconn* conn; xwebsocket_mutex = xSemaphoreCreateMutex(); xwebsocket_queue = xQueueCreate(WEBSOCKET_SERVER_QUEUE_SIZE, sizeof(struct netconn*)); // initialize all clients for(int i=0;icallback = background_callback; netconn_write(conn,handshake,strlen(handshake),NETCONN_COPY); for(int i=0;i