/* * High Speed modem to transfer data in a 2,7kHz SSB channel * ========================================================= * Author: DJ0ABR * * (c) DJ0ABR * www.dj0abr.de * * websocket server: based on the work by: Davidson Francis * * 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 2 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * websocketserver.c ... sends data to a web browser * * if a web browser is logged into this WebSocketServer then * we send the data to this browser. * Its the job of the browser to make anything with these data. * * This WebSocketServer is a multi threaded implementation and * opens a new thread for each client * * ! THIS implementation of a WebSocketServer DOES NOT require a Webserver (like Apache) * because it handles all Websocket tasks by itself ! * * usage: * ====== * * ws_init() ... call once after program start to initialize the websocket server * * ws_send(unsigned char *pdata, int len) ... * send pdata (max. lenght MESSAGE_LENGTH) to all connected clients. * this function is thread safe. It stores the data in a buffer. * This buffer is sent to the clients in the websocket-thread in the background. * * */ #include "../hsmodem.h" void init_ws_locks(); void ws_alive(); #ifdef _WIN32_ void wsproc(void* param); #else void* wsproc(void* param); #endif #define MAXIPLEN 16 WS_SOCK actsock[MAX_CLIENTS]; char clilist[MAX_CLIENTS][MAXIPLEN]; /*void test_websocket() { char* msg = "ABCD1234"; static int t = 0; if (++t > 100) { t = 0; printf("send ws: %s\n", msg); ws_send((unsigned char *)msg, strlen(msg)); } }*/ // initialise the WebSocketServer // run the WebSocketServer in a new thread void ws_init() { printf("starting websocket server\n"); init_ws_locks(); for(int i=0; i> 8; txdata[idx++] = actsock[i].msglen & 0xff; memcpy(txdata+idx,actsock[i].msg,actsock[i].msglen); idx += actsock[i].msglen; } *plength = idx; WS_UNLOCK(); return txdata; } // insert a socket into the socket-list void insert_socket(int fd, char *cli) { WS_LOCK; for(int i=0; i 0) { //printf("%d %d\n", i, actsock[i].alive); actsock[i].alive--; if (actsock[i].alive == 0) { // remove inactive client remove_socket(actsock[i].socket); } } } } // remove a socket from the socket-list int get_alive(int fd) { int a = 0; WS_LOCK; for (int i = 0; i < MAX_CLIENTS; i++) { if (actsock[i].socket == fd) { a = actsock[i].alive; WS_UNLOCK(); return a; } } WS_UNLOCK(); return 0; }