diff -r 000000000000 -r b74b0e4902c3 components/websocket/include/websocket_server.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/components/websocket/include/websocket_server.h Sat Oct 20 13:23:15 2018 +0200 @@ -0,0 +1,166 @@ +/** + * @file websocket_server.h + * @brief Websocket server functions. + * @author Blake Felt - blake.w.felt@gmail.com` + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WEBSOCKET_SERVER_H +#define WEBSOCKET_SERVER_H + +#include "websocket.h" + +#define WEBSOCKET_SERVER_MAX_CLIENTS CONFIG_WEBSOCKET_SERVER_MAX_CLIENTS +#define WEBSOCKET_SERVER_QUEUE_SIZE CONFIG_WEBSOCKET_SERVER_QUEUE_SIZE +#define WEBSOCKET_SERVER_QUEUE_TIMEOUT CONFIG_WEBSOCKET_SERVER_QUEUE_TIMEOUT +#define WEBSOCKET_SERVER_TASK_STACK_DEPTH CONFIG_WEBSOCKET_SERVER_TASK_STACK_DEPTH +#define WEBSOCKET_SERVER_TASK_PRIORITY CONFIG_WEBSOCKET_SERVER_TASK_PRIORITY +#define WEBSOCKET_SERVER_PINNED CONFIG_WEBSOCKET_SERVER_PINNED +#if WEBSOCKET_SERVER_PINNED +#define WEBSOCKET_SERVER_PINNED_CORE CONFIG_WEBSOCKET_SERVER_PINNED_CORE +#endif + +/** + * @brief Starts the server. + * @return 1 if started, 0 if the server was already started. + */ +int ws_server_start(); + +/** + * @brief Stops the server. + * @return 1 if stopped, 0 if the server was not running. + */ +int ws_server_stop(); + +/** + * @brief Adds a client, returns the client's number in the server. + * @param conn The network connection. + * @param msg The message received from the client. + * @param len The length of the message. + * @param url The url of the connection. + * @param callback The callback function. + * @return Negative if an error, else the connection number. + */ +int ws_server_add_client(struct netconn* conn, + char* msg, + uint16_t len, + char* url, + void (*callback)(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)); + +/** + * @brief Adds a client, returns the client's number in the server. + * @param conn The network connection. + * @param msg The message received from the client. + * @param len The length of the message. + * @param url The url of the connection. + * @param protocol The protocol requested by the client. + * @param callback The callback function. + * @return Negative if an error, else the connection number. + */ +int ws_server_add_client_protocol(struct netconn* conn, + char* msg, + uint16_t len, + char* url, + char* protocol, + void (*callback)(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)); + +/** + * @brief Returns the number of connected clients to url + * @param url The url to check. + * @return The number of connected clients. + */ +int ws_server_len_url(char* url); + +/** + * @brief Returns the total number of connected clients. + * @return The number of connected clients. + */ +int ws_server_len_all(); + +/** + * @brief Removes the client with the set number + * @param num The client number. + * @return 1 if success, else 0. + */ +int ws_server_remove_client(int num); + +/** + * @brief Removes all clients connected to the specified url. + * @param url The connection url. + * @return The number of clients removed. + */ +int ws_server_remove_clients(char* url); + +/** + * @brief Removes all clients from the server. + * @return The number of clients removed. + */ +int ws_server_remove_all(); + +/** + * @brief Send text to client with the set number. + * @param num The client connection number. + * @param msg The message to send. + * @param The length of the message to send. + * @retrun 1 if message send. + */ +int ws_server_send_text_client(int num,char* msg,uint64_t len); +int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len); + +/** + * @brief Sends text to all clients with the set url. + * @param url The clients url. + * @param msg The message to send. + * @param The length of the message to send. + * @return The number of clients the message was send to. + */ +int ws_server_send_text_clients(char* url,char* msg,uint64_t len); +int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len); + +/** + * @brief Sends text to all clients. + * @param msg The message to send. + * @param The length of the message to send. + * @return The number of clients the message was send to. + */ +int ws_server_send_text_all(char* msg,uint64_t len); +int ws_server_send_text_all_from_callback(char* msg,uint64_t len); + +/** + * @brief Send binary message to client with the set number. + * @param num The client connection number. + * @param msg The message to send. + * @param The length of the message to send. + * @retrun 1 if message send. + */ +int ws_server_send_bin_client(int num,char* msg,uint64_t len); +int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len); + +/** + * @brief Sends binary message to all clients with the set url. + * @param url The clients url. + * @param msg The message to send. + * @param The length of the message to send. + * @return The number of clients the message was send to. + */ +int ws_server_send_bin_clients(char* url,char* msg,uint64_t len); + +/** + * @brief Sends binary message to all clients. + * @param msg The message to send. + * @param The length of the message to send. + * @return The number of clients the message was send to. + */ +int ws_server_send_bin_all(char* msg,uint64_t len); + + +int ws_server_ping(int num); // sends a ping to client num + +#endif + +#ifdef __cplusplus +} +#endif