|
1 /** |
|
2 * @file websocket_server.h |
|
3 * @brief Websocket server functions. |
|
4 * @author Blake Felt - blake.w.felt@gmail.com` |
|
5 */ |
|
6 |
|
7 #ifdef __cplusplus |
|
8 extern "C" { |
|
9 #endif |
|
10 |
|
11 #ifndef WEBSOCKET_SERVER_H |
|
12 #define WEBSOCKET_SERVER_H |
|
13 |
|
14 #include "websocket.h" |
|
15 |
|
16 #define WEBSOCKET_SERVER_MAX_CLIENTS CONFIG_WEBSOCKET_SERVER_MAX_CLIENTS |
|
17 #define WEBSOCKET_SERVER_QUEUE_SIZE CONFIG_WEBSOCKET_SERVER_QUEUE_SIZE |
|
18 #define WEBSOCKET_SERVER_QUEUE_TIMEOUT CONFIG_WEBSOCKET_SERVER_QUEUE_TIMEOUT |
|
19 #define WEBSOCKET_SERVER_TASK_STACK_DEPTH CONFIG_WEBSOCKET_SERVER_TASK_STACK_DEPTH |
|
20 #define WEBSOCKET_SERVER_TASK_PRIORITY CONFIG_WEBSOCKET_SERVER_TASK_PRIORITY |
|
21 #define WEBSOCKET_SERVER_PINNED CONFIG_WEBSOCKET_SERVER_PINNED |
|
22 #if WEBSOCKET_SERVER_PINNED |
|
23 #define WEBSOCKET_SERVER_PINNED_CORE CONFIG_WEBSOCKET_SERVER_PINNED_CORE |
|
24 #endif |
|
25 |
|
26 /** |
|
27 * @brief Starts the server. |
|
28 * @return 1 if started, 0 if the server was already started. |
|
29 */ |
|
30 int ws_server_start(); |
|
31 |
|
32 /** |
|
33 * @brief Stops the server. |
|
34 * @return 1 if stopped, 0 if the server was not running. |
|
35 */ |
|
36 int ws_server_stop(); |
|
37 |
|
38 /** |
|
39 * @brief Adds a client, returns the client's number in the server. |
|
40 * @param conn The network connection. |
|
41 * @param msg The message received from the client. |
|
42 * @param len The length of the message. |
|
43 * @param url The url of the connection. |
|
44 * @param callback The callback function. |
|
45 * @return Negative if an error, else the connection number. |
|
46 */ |
|
47 int ws_server_add_client(struct netconn* conn, |
|
48 char* msg, |
|
49 uint16_t len, |
|
50 char* url, |
|
51 void (*callback)(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)); |
|
52 |
|
53 /** |
|
54 * @brief Adds a client, returns the client's number in the server. |
|
55 * @param conn The network connection. |
|
56 * @param msg The message received from the client. |
|
57 * @param len The length of the message. |
|
58 * @param url The url of the connection. |
|
59 * @param protocol The protocol requested by the client. |
|
60 * @param callback The callback function. |
|
61 * @return Negative if an error, else the connection number. |
|
62 */ |
|
63 int ws_server_add_client_protocol(struct netconn* conn, |
|
64 char* msg, |
|
65 uint16_t len, |
|
66 char* url, |
|
67 char* protocol, |
|
68 void (*callback)(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)); |
|
69 |
|
70 /** |
|
71 * @brief Returns the number of connected clients to url |
|
72 * @param url The url to check. |
|
73 * @return The number of connected clients. |
|
74 */ |
|
75 int ws_server_len_url(char* url); |
|
76 |
|
77 /** |
|
78 * @brief Returns the total number of connected clients. |
|
79 * @return The number of connected clients. |
|
80 */ |
|
81 int ws_server_len_all(); |
|
82 |
|
83 /** |
|
84 * @brief Removes the client with the set number |
|
85 * @param num The client number. |
|
86 * @return 1 if success, else 0. |
|
87 */ |
|
88 int ws_server_remove_client(int num); |
|
89 |
|
90 /** |
|
91 * @brief Removes all clients connected to the specified url. |
|
92 * @param url The connection url. |
|
93 * @return The number of clients removed. |
|
94 */ |
|
95 int ws_server_remove_clients(char* url); |
|
96 |
|
97 /** |
|
98 * @brief Removes all clients from the server. |
|
99 * @return The number of clients removed. |
|
100 */ |
|
101 int ws_server_remove_all(); |
|
102 |
|
103 /** |
|
104 * @brief Send text to client with the set number. |
|
105 * @param num The client connection number. |
|
106 * @param msg The message to send. |
|
107 * @param The length of the message to send. |
|
108 * @retrun 1 if message send. |
|
109 */ |
|
110 int ws_server_send_text_client(int num,char* msg,uint64_t len); |
|
111 int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len); |
|
112 |
|
113 /** |
|
114 * @brief Sends text to all clients with the set url. |
|
115 * @param url The clients url. |
|
116 * @param msg The message to send. |
|
117 * @param The length of the message to send. |
|
118 * @return The number of clients the message was send to. |
|
119 */ |
|
120 int ws_server_send_text_clients(char* url,char* msg,uint64_t len); |
|
121 int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len); |
|
122 |
|
123 /** |
|
124 * @brief Sends text to all clients. |
|
125 * @param msg The message to send. |
|
126 * @param The length of the message to send. |
|
127 * @return The number of clients the message was send to. |
|
128 */ |
|
129 int ws_server_send_text_all(char* msg,uint64_t len); |
|
130 int ws_server_send_text_all_from_callback(char* msg,uint64_t len); |
|
131 |
|
132 /** |
|
133 * @brief Send binary message to client with the set number. |
|
134 * @param num The client connection number. |
|
135 * @param msg The message to send. |
|
136 * @param The length of the message to send. |
|
137 * @retrun 1 if message send. |
|
138 */ |
|
139 int ws_server_send_bin_client(int num,char* msg,uint64_t len); |
|
140 int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len); |
|
141 |
|
142 /** |
|
143 * @brief Sends binary message to all clients with the set url. |
|
144 * @param url The clients url. |
|
145 * @param msg The message to send. |
|
146 * @param The length of the message to send. |
|
147 * @return The number of clients the message was send to. |
|
148 */ |
|
149 int ws_server_send_bin_clients(char* url,char* msg,uint64_t len); |
|
150 |
|
151 /** |
|
152 * @brief Sends binary message to all clients. |
|
153 * @param msg The message to send. |
|
154 * @param The length of the message to send. |
|
155 * @return The number of clients the message was send to. |
|
156 */ |
|
157 int ws_server_send_bin_all(char* msg,uint64_t len); |
|
158 |
|
159 |
|
160 int ws_server_ping(int num); // sends a ping to client num |
|
161 |
|
162 #endif |
|
163 |
|
164 #ifdef __cplusplus |
|
165 } |
|
166 #endif |