aboutsummaryrefslogtreecommitdiff
path: root/src/examples/websocket_chatserver_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/websocket_chatserver_example.c')
-rw-r--r--src/examples/websocket_chatserver_example.c121
1 files changed, 61 insertions, 60 deletions
diff --git a/src/examples/websocket_chatserver_example.c b/src/examples/websocket_chatserver_example.c
index 0893279b..eb5dad39 100644
--- a/src/examples/websocket_chatserver_example.c
+++ b/src/examples/websocket_chatserver_example.c
@@ -629,14 +629,14 @@ struct ConnectedUser
629 /* the UpgradeResponseHandle of libmicrohttpd (needed for closing the socket) */ 629 /* the UpgradeResponseHandle of libmicrohttpd (needed for closing the socket) */
630 struct MHD_UpgradeResponseHandle *urh; 630 struct MHD_UpgradeResponseHandle *urh;
631 /* the websocket encode/decode stream */ 631 /* the websocket encode/decode stream */
632 struct MHD_WebSocketStream*ws; 632 struct MHD_WebSocketStream *ws;
633 /* the possibly read data at the start (only used once) */ 633 /* the possibly read data at the start (only used once) */
634 char *extra_in; 634 char *extra_in;
635 size_t extra_in_size; 635 size_t extra_in_size;
636 /* the unique user id (counting from 1, ids will never be re-used) */ 636 /* the unique user id (counting from 1, ids will never be re-used) */
637 size_t user_id; 637 size_t user_id;
638 /* the current user name */ 638 /* the current user name */
639 char*user_name; 639 char *user_name;
640 size_t user_name_len; 640 size_t user_name_len;
641 /* the zero-based index of the next message; 641 /* the zero-based index of the next message;
642 may be decremented when old messages are deleted */ 642 may be decremented when old messages are deleted */
@@ -673,7 +673,7 @@ struct Message
673 /* The user id of the recipient. This is 0 if every connected user shall receive it */ 673 /* The user id of the recipient. This is 0 if every connected user shall receive it */
674 size_t to_user_id; 674 size_t to_user_id;
675 /* The data of the message. */ 675 /* The data of the message. */
676 char*data; 676 char *data;
677 size_t data_len; 677 size_t data_len;
678 /* Specifies whether the data is UTF-8 encoded text (0) or binary data (1) */ 678 /* Specifies whether the data is UTF-8 encoded text (0) or binary data (1) */
679 int is_binary; 679 int is_binary;
@@ -684,9 +684,9 @@ size_t unique_user_id = 0;
684 684
685/* the chat data (users and messages; may be accessed by all threads, but is protected by mutex) */ 685/* the chat data (users and messages; may be accessed by all threads, but is protected by mutex) */
686pthread_mutex_t chat_mutex; 686pthread_mutex_t chat_mutex;
687struct ConnectedUser**users = NULL; 687struct ConnectedUser **users = NULL;
688size_t user_count = 0; 688size_t user_count = 0;
689struct Message**messages = NULL; 689struct Message **messages = NULL;
690size_t message_count = 0; 690size_t message_count = 0;
691/* specifies whether all websockets must close (1) or not (0) */ 691/* specifies whether all websockets must close (1) or not (0) */
692volatile int disconnect_all = 0; 692volatile int disconnect_all = 0;
@@ -728,7 +728,7 @@ make_blocking (MHD_socket fd)
728 * @param len The length in bytes of the data in the buffer 728 * @param len The length in bytes of the data in the buffer
729 */ 729 */
730static void 730static void
731send_all (struct ConnectedUser*cu, 731send_all (struct ConnectedUser *cu,
732 const char *buf, 732 const char *buf,
733 size_t len) 733 size_t len)
734{ 734{
@@ -776,13 +776,13 @@ send_all (struct ConnectedUser*cu,
776static int 776static int
777chat_addmessage (size_t from_user_id, 777chat_addmessage (size_t from_user_id,
778 size_t to_user_id, 778 size_t to_user_id,
779 char*data, 779 char *data,
780 size_t data_len, 780 size_t data_len,
781 int is_binary, 781 int is_binary,
782 int needs_lock) 782 int needs_lock)
783{ 783{
784 /* allocate the buffer and fill it with data */ 784 /* allocate the buffer and fill it with data */
785 struct Message*message = (struct Message*) malloc (sizeof (struct Message)); 785 struct Message *message = (struct Message *) malloc (sizeof (struct Message));
786 if (NULL == message) 786 if (NULL == message)
787 return 1; 787 return 1;
788 788
@@ -809,10 +809,10 @@ chat_addmessage (size_t from_user_id,
809 809
810 /* add the new message to the global message list */ 810 /* add the new message to the global message list */
811 size_t message_count_ = message_count + 1; 811 size_t message_count_ = message_count + 1;
812 struct Message**messages_ = (struct Message**) realloc (messages, 812 struct Message **messages_ = (struct Message **) realloc (messages,
813 message_count_ 813 message_count_
814 * sizeof (struct 814 * sizeof (struct
815 Message*)); 815 Message *));
816 if (NULL == messages_) 816 if (NULL == messages_)
817 { 817 {
818 free (message); 818 free (message);
@@ -928,14 +928,14 @@ chat_clearmessages (int needs_lock)
928 * @return 0 on success, other values on error 928 * @return 0 on success, other values on error
929 */ 929 */
930static int 930static int
931chat_adduser (struct ConnectedUser*cu) 931chat_adduser (struct ConnectedUser *cu)
932{ 932{
933 /* initialize the notification message of the new user */ 933 /* initialize the notification message of the new user */
934 char user_index[32]; 934 char user_index[32];
935 snprintf (user_index, 32, "%d", (int) cu->user_id); 935 snprintf (user_index, 32, "%d", (int) cu->user_id);
936 size_t user_index_len = strlen (user_index); 936 size_t user_index_len = strlen (user_index);
937 size_t data_len = user_index_len + cu->user_name_len + 9; 937 size_t data_len = user_index_len + cu->user_name_len + 9;
938 char*data = (char*) malloc (data_len + 1); 938 char *data = (char *) malloc (data_len + 1);
939 if (NULL == data) 939 if (NULL == data)
940 return 1; 940 return 1;
941 strcpy (data, "useradd|"); 941 strcpy (data, "useradd|");
@@ -965,12 +965,12 @@ chat_adduser (struct ConnectedUser*cu)
965 965
966 /* add the new user to the list */ 966 /* add the new user to the list */
967 size_t user_count_ = user_count + 1; 967 size_t user_count_ = user_count + 1;
968 struct ConnectedUser**users_ = (struct ConnectedUser**) realloc (users, 968 struct ConnectedUser **users_ = (struct ConnectedUser **) realloc (users,
969 user_count_ 969 user_count_
970 * sizeof ( 970 * sizeof (
971 struct 971 struct
972 ConnectedUser 972 ConnectedUser
973 *)); 973 *));
974 if (NULL == users_) 974 if (NULL == users_)
975 { 975 {
976 /* realloc failed */ 976 /* realloc failed */
@@ -998,7 +998,7 @@ chat_adduser (struct ConnectedUser*cu)
998 * @return 0 on success, other values on error 998 * @return 0 on success, other values on error
999 */ 999 */
1000static int 1000static int
1001chat_removeuser (struct ConnectedUser*cu) 1001chat_removeuser (struct ConnectedUser *cu)
1002{ 1002{
1003 char user_index[32]; 1003 char user_index[32];
1004 1004
@@ -1006,7 +1006,7 @@ chat_removeuser (struct ConnectedUser*cu)
1006 snprintf (user_index, 32, "%d", (int) cu->user_id); 1006 snprintf (user_index, 32, "%d", (int) cu->user_id);
1007 size_t user_index_len = strlen (user_index); 1007 size_t user_index_len = strlen (user_index);
1008 size_t data_len = user_index_len + 9; 1008 size_t data_len = user_index_len + 9;
1009 char*data = (char*) malloc (data_len + 1); 1009 char *data = (char *) malloc (data_len + 1);
1010 if (NULL == data) 1010 if (NULL == data)
1011 return 1; 1011 return 1;
1012 strcpy (data, "userdel|"); 1012 strcpy (data, "userdel|");
@@ -1061,8 +1061,8 @@ chat_removeuser (struct ConnectedUser*cu)
1061 * @return 0 on success, other values on error. 2 means name already in use. 1061 * @return 0 on success, other values on error. 2 means name already in use.
1062 */ 1062 */
1063static int 1063static int
1064chat_renameuser (struct ConnectedUser*cu, 1064chat_renameuser (struct ConnectedUser *cu,
1065 char*new_name, 1065 char *new_name,
1066 size_t new_name_len) 1066 size_t new_name_len)
1067{ 1067{
1068 /* lock the mutex */ 1068 /* lock the mutex */
@@ -1090,7 +1090,7 @@ chat_renameuser (struct ConnectedUser*cu,
1090 snprintf (user_index, 32, "%d", (int) cu->user_id); 1090 snprintf (user_index, 32, "%d", (int) cu->user_id);
1091 size_t user_index_len = strlen (user_index); 1091 size_t user_index_len = strlen (user_index);
1092 size_t data_len = user_index_len + new_name_len + 10; 1092 size_t data_len = user_index_len + new_name_len + 10;
1093 char*data = (char*) malloc (data_len + 1); 1093 char *data = (char *) malloc (data_len + 1);
1094 if (NULL == data) 1094 if (NULL == data)
1095 return 1; 1095 return 1;
1096 strcpy (data, "username|"); 1096 strcpy (data, "username|");
@@ -1128,8 +1128,8 @@ chat_renameuser (struct ConnectedUser*cu,
1128* @return 0 on success, other values on error 1128* @return 0 on success, other values on error
1129*/ 1129*/
1130static int 1130static int
1131connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu, 1131connecteduser_parse_received_websocket_stream (struct ConnectedUser *cu,
1132 char*buf, 1132 char *buf,
1133 size_t buf_len) 1133 size_t buf_len)
1134{ 1134{
1135 size_t buf_offset = 0; 1135 size_t buf_offset = 0;
@@ -1270,7 +1270,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1270 snprintf (user_index, 32, "%d", (int) cu->user_id); 1270 snprintf (user_index, 32, "%d", (int) cu->user_id);
1271 size_t user_index_len = strlen (user_index); 1271 size_t user_index_len = strlen (user_index);
1272 size_t data_len = user_index_len + frame_len - j + 9; 1272 size_t data_len = user_index_len + frame_len - j + 9;
1273 char*data = (char*) malloc (data_len + 1); 1273 char *data = (char *) malloc (data_len + 1);
1274 if (NULL != data) 1274 if (NULL != data)
1275 { 1275 {
1276 strcpy (data, "regular|"); 1276 strcpy (data, "regular|");
@@ -1307,7 +1307,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1307 snprintf (user_index, 32, "%d", (int) cu->user_id); 1307 snprintf (user_index, 32, "%d", (int) cu->user_id);
1308 size_t user_index_len = strlen (user_index); 1308 size_t user_index_len = strlen (user_index);
1309 size_t data_len = user_index_len + frame_len - j + 9; 1309 size_t data_len = user_index_len + frame_len - j + 9;
1310 char*data = (char*) malloc (data_len + 1); 1310 char *data = (char *) malloc (data_len + 1);
1311 if (NULL != data) 1311 if (NULL != data)
1312 { 1312 {
1313 1313
@@ -1347,7 +1347,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1347 1); 1347 1);
1348 break; 1348 break;
1349 } 1349 }
1350 char*new_name = (char*) malloc (new_name_len + 1); 1350 char *new_name = (char *) malloc (new_name_len + 1);
1351 if (NULL == new_name) 1351 if (NULL == new_name)
1352 { 1352 {
1353 chat_addmessage (0, 1353 chat_addmessage (0,
@@ -1415,7 +1415,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1415 if (0 == pthread_mutex_lock (&chat_mutex)) 1415 if (0 == pthread_mutex_lock (&chat_mutex))
1416 { 1416 {
1417 /* check whether the to_user exists */ 1417 /* check whether the to_user exists */
1418 struct ConnectedUser*ping_user = NULL; 1418 struct ConnectedUser *ping_user = NULL;
1419 for (size_t k = 0; k < user_count; ++k) 1419 for (size_t k = 0; k < user_count; ++k)
1420 { 1420 {
1421 if (users[k]->user_id == to_user_id) 1421 if (users[k]->user_id == to_user_id)
@@ -1478,7 +1478,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1478 MHD_websocket_free (cu->ws, 1478 MHD_websocket_free (cu->ws,
1479 frame_data); 1479 frame_data);
1480 { 1480 {
1481 char*result = NULL; 1481 char *result = NULL;
1482 size_t result_len = 0; 1482 size_t result_len = 0;
1483 int er = MHD_websocket_encode_close (cu->ws, 1483 int er = MHD_websocket_encode_close (cu->ws,
1484 MHD_WEBSOCKET_CLOSEREASON_REGULAR, 1484 MHD_WEBSOCKET_CLOSEREASON_REGULAR,
@@ -1545,7 +1545,8 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1545 snprintf (result_text + 5, 235, "%d", (int) cu->user_id); 1545 snprintf (result_text + 5, 235, "%d", (int) cu->user_id);
1546 strcat (result_text, 1546 strcat (result_text,
1547 "|"); 1547 "|");
1548 snprintf (result_text + strlen (result_text), 240 - strlen (result_text), "%d", (int) ping); 1548 snprintf (result_text + strlen (result_text), 240 - strlen (
1549 result_text), "%d", (int) ping);
1549 chat_addmessage (0, 1550 chat_addmessage (0,
1550 0, 1551 0,
1551 result_text, 1552 result_text,
@@ -1586,7 +1587,7 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser*cu,
1586 * @return Always NULL 1587 * @return Always NULL
1587 */ 1588 */
1588static void * 1589static void *
1589connecteduser_send_messages (void*cls) 1590connecteduser_send_messages (void *cls)
1590{ 1591{
1591 struct ConnectedUser *cu = cls; 1592 struct ConnectedUser *cu = cls;
1592 1593
@@ -1602,7 +1603,7 @@ connecteduser_send_messages (void*cls)
1602 if (1 == disconnect_all) 1603 if (1 == disconnect_all)
1603 { 1604 {
1604 /* the application closes and want that we disconnect all users */ 1605 /* the application closes and want that we disconnect all users */
1605 struct MHD_UpgradeResponseHandle*urh = cu->urh; 1606 struct MHD_UpgradeResponseHandle *urh = cu->urh;
1606 if (NULL != urh) 1607 if (NULL != urh)
1607 { 1608 {
1608 /* Close the TCP/IP socket. */ 1609 /* Close the TCP/IP socket. */
@@ -1629,7 +1630,7 @@ connecteduser_send_messages (void*cls)
1629 "libmicrohttpdchatserverpingdata"); 1630 "libmicrohttpdchatserverpingdata");
1630 snprintf (cu->ping_message + 31, 97, "%d", (int) cu->ping_counter); 1631 snprintf (cu->ping_message + 31, 97, "%d", (int) cu->ping_counter);
1631 cu->ping_message_len = strlen (cu->ping_message); 1632 cu->ping_message_len = strlen (cu->ping_message);
1632 char*frame_data = NULL; 1633 char *frame_data = NULL;
1633 size_t frame_len = 0; 1634 size_t frame_len = 0;
1634 int er = MHD_websocket_encode_ping (cu->ws, 1635 int er = MHD_websocket_encode_ping (cu->ws,
1635 cu->ping_message, 1636 cu->ping_message,
@@ -1657,11 +1658,11 @@ connecteduser_send_messages (void*cls)
1657 else if (cu->next_message_index < message_count) 1658 else if (cu->next_message_index < message_count)
1658 { 1659 {
1659 /* a chat message or command is pending */ 1660 /* a chat message or command is pending */
1660 char*frame_data = NULL; 1661 char *frame_data = NULL;
1661 size_t frame_len = 0; 1662 size_t frame_len = 0;
1662 int er = 0; 1663 int er = 0;
1663 { 1664 {
1664 struct Message*msg = messages[cu->next_message_index]; 1665 struct Message *msg = messages[cu->next_message_index];
1665 if ((0 == msg->to_user_id) || 1666 if ((0 == msg->to_user_id) ||
1666 (cu->user_id == msg->to_user_id) || 1667 (cu->user_id == msg->to_user_id) ||
1667 (cu->user_id == msg->from_user_id) ) 1668 (cu->user_id == msg->from_user_id) )
@@ -1810,10 +1811,10 @@ connecteduser_receive_messages (void *cls)
1810 { 1811 {
1811 struct UserInit 1812 struct UserInit
1812 { 1813 {
1813 char*user_init; 1814 char *user_init;
1814 size_t user_init_len; 1815 size_t user_init_len;
1815 }; 1816 };
1816 struct UserInit*init_users = NULL; 1817 struct UserInit *init_users = NULL;
1817 size_t init_users_len = 0; 1818 size_t init_users_len = 0;
1818 1819
1819 /* first collect all users without sending (so the mutex isn't locked too long) */ 1820 /* first collect all users without sending (so the mutex isn't locked too long) */
@@ -1821,8 +1822,8 @@ connecteduser_receive_messages (void *cls)
1821 { 1822 {
1822 if (0 < user_count) 1823 if (0 < user_count)
1823 { 1824 {
1824 init_users = (struct UserInit*) malloc (user_count * sizeof (struct 1825 init_users = (struct UserInit *) malloc (user_count * sizeof (struct
1825 UserInit)); 1826 UserInit));
1826 if (NULL != init_users) 1827 if (NULL != init_users)
1827 { 1828 {
1828 init_users_len = user_count; 1829 init_users_len = user_count;
@@ -1833,7 +1834,7 @@ connecteduser_receive_messages (void *cls)
1833 size_t user_index_len = strlen (user_index); 1834 size_t user_index_len = strlen (user_index);
1834 struct UserInit iu; 1835 struct UserInit iu;
1835 iu.user_init_len = user_index_len + users[i]->user_name_len + 10; 1836 iu.user_init_len = user_index_len + users[i]->user_name_len + 10;
1836 iu.user_init = (char*) malloc (iu.user_init_len + 1); 1837 iu.user_init = (char *) malloc (iu.user_init_len + 1);
1837 if (NULL != iu.user_init) 1838 if (NULL != iu.user_init)
1838 { 1839 {
1839 strcpy (iu.user_init, "userinit|"); 1840 strcpy (iu.user_init, "userinit|");
@@ -1929,7 +1930,7 @@ connecteduser_receive_messages (void *cls)
1929 pthread_mutex_unlock (&chat_mutex); 1930 pthread_mutex_unlock (&chat_mutex);
1930 pthread_join (pt, NULL); 1931 pthread_join (pt, NULL);
1931 } 1932 }
1932 struct MHD_UpgradeResponseHandle*urh = cu->urh; 1933 struct MHD_UpgradeResponseHandle *urh = cu->urh;
1933 if (NULL != urh) 1934 if (NULL != urh)
1934 { 1935 {
1935 cu->urh = NULL; 1936 cu->urh = NULL;
@@ -1974,7 +1975,7 @@ connecteduser_receive_messages (void *cls)
1974 pthread_mutex_unlock (&chat_mutex); 1975 pthread_mutex_unlock (&chat_mutex);
1975 pthread_join (pt, NULL); 1976 pthread_join (pt, NULL);
1976 } 1977 }
1977 struct MHD_UpgradeResponseHandle*urh = cu->urh; 1978 struct MHD_UpgradeResponseHandle *urh = cu->urh;
1978 if (NULL != urh) 1979 if (NULL != urh)
1979 { 1980 {
1980 cu->urh = NULL; 1981 cu->urh = NULL;
@@ -2000,7 +2001,7 @@ connecteduser_receive_messages (void *cls)
2000 pthread_mutex_unlock (&chat_mutex); 2001 pthread_mutex_unlock (&chat_mutex);
2001 pthread_join (pt, NULL); 2002 pthread_join (pt, NULL);
2002 } 2003 }
2003 struct MHD_UpgradeResponseHandle*urh = cu->urh; 2004 struct MHD_UpgradeResponseHandle *urh = cu->urh;
2004 if (NULL != urh) 2005 if (NULL != urh)
2005 { 2006 {
2006 cu->urh = NULL; 2007 cu->urh = NULL;
@@ -2161,10 +2162,10 @@ access_handler (void *cls,
2161 if (0 == strcmp (url, "/")) 2162 if (0 == strcmp (url, "/"))
2162 { 2163 {
2163 /* Default page for visiting the server */ 2164 /* Default page for visiting the server */
2164 struct MHD_Response*response = MHD_create_response_from_buffer (strlen ( 2165 struct MHD_Response *response = MHD_create_response_from_buffer (strlen (
2165 PAGE), 2166 PAGE),
2166 PAGE, 2167 PAGE,
2167 MHD_RESPMEM_PERSISTENT); 2168 MHD_RESPMEM_PERSISTENT);
2168 ret = MHD_queue_response (connection, 2169 ret = MHD_queue_response (connection,
2169 MHD_HTTP_OK, 2170 MHD_HTTP_OK,
2170 response); 2171 response);
@@ -2188,7 +2189,7 @@ access_handler (void *cls,
2188 */ 2189 */
2189 2190
2190 char is_valid = 1; 2191 char is_valid = 1;
2191 const char* value = NULL; 2192 const char *value = NULL;
2192 char sec_websocket_accept[29]; 2193 char sec_websocket_accept[29];
2193 2194
2194 /* check whether an websocket upgrade is requested */ 2195 /* check whether an websocket upgrade is requested */
@@ -2257,10 +2258,10 @@ access_handler (void *cls,
2257 else 2258 else
2258 { 2259 {
2259 /* return error page */ 2260 /* return error page */
2260 struct MHD_Response*response = MHD_create_response_from_buffer (strlen ( 2261 struct MHD_Response *response = MHD_create_response_from_buffer (strlen (
2261 PAGE_INVALID_WEBSOCKET_REQUEST), 2262 PAGE_INVALID_WEBSOCKET_REQUEST),
2262 PAGE_INVALID_WEBSOCKET_REQUEST, 2263 PAGE_INVALID_WEBSOCKET_REQUEST,
2263 MHD_RESPMEM_PERSISTENT); 2264 MHD_RESPMEM_PERSISTENT);
2264 ret = MHD_queue_response (connection, 2265 ret = MHD_queue_response (connection,
2265 MHD_HTTP_BAD_REQUEST, 2266 MHD_HTTP_BAD_REQUEST,
2266 response); 2267 response);
@@ -2269,10 +2270,10 @@ access_handler (void *cls,
2269 } 2270 }
2270 else 2271 else
2271 { 2272 {
2272 struct MHD_Response*response = MHD_create_response_from_buffer (strlen ( 2273 struct MHD_Response *response = MHD_create_response_from_buffer (strlen (
2273 PAGE_NOT_FOUND), 2274 PAGE_NOT_FOUND),
2274 PAGE_NOT_FOUND, 2275 PAGE_NOT_FOUND,
2275 MHD_RESPMEM_PERSISTENT); 2276 MHD_RESPMEM_PERSISTENT);
2276 ret = MHD_queue_response (connection, 2277 ret = MHD_queue_response (connection,
2277 MHD_HTTP_NOT_FOUND, 2278 MHD_HTTP_NOT_FOUND,
2278 response); 2279 response);
@@ -2338,7 +2339,7 @@ main (int argc,
2338 { 2339 {
2339 for (size_t i = 0; i < user_count; ++i) 2340 for (size_t i = 0; i < user_count; ++i)
2340 { 2341 {
2341 struct MHD_UpgradeResponseHandle*urh = users[i]->urh; 2342 struct MHD_UpgradeResponseHandle *urh = users[i]->urh;
2342 if (NULL != urh) 2343 if (NULL != urh)
2343 { 2344 {
2344 users[i]->urh = NULL; 2345 users[i]->urh = NULL;