diff options
author | silvioprog <silvioprog@gmail.com> | 2020-04-10 14:05:57 -0300 |
---|---|---|
committer | silvioprog <silvioprog@gmail.com> | 2020-04-10 14:05:57 -0300 |
commit | f4be5e015488659dc68dadbaca5a479e7b12d09c (patch) | |
tree | 880cabdc97edd75fc2b434d06f47460420d0ef0e | |
parent | 08134cf85ade4eb39b6a384af1df58db7af9266b (diff) | |
download | libmicrohttpd-f4be5e015488659dc68dadbaca5a479e7b12d09c.tar.gz libmicrohttpd-f4be5e015488659dc68dadbaca5a479e7b12d09c.zip |
Avoid item duplication inside client list in the websocket example.
-rw-r--r-- | src/examples/websocket_threaded_example.c | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/examples/websocket_threaded_example.c b/src/examples/websocket_threaded_example.c index ac4bb839..44cb3493 100644 --- a/src/examples/websocket_threaded_example.c +++ b/src/examples/websocket_threaded_example.c | |||
@@ -735,20 +735,20 @@ run_usock (void *cls) | |||
735 | if (type == WS_OPCODE_CON_CLOSE_FRAME) | 735 | if (type == WS_OPCODE_CON_CLOSE_FRAME) |
736 | { | 736 | { |
737 | free (msg); | 737 | free (msg); |
738 | pthread_mutex_lock (&MUTEX); | ||
739 | for (i = 0; i < MAX_CLIENTS; i++) | ||
740 | { | ||
741 | if (CLIENT_SOCKS[i] == ws->sock) | ||
742 | { | ||
743 | CLIENT_SOCKS[i] = -1; | ||
744 | break; | ||
745 | } | ||
746 | } | ||
747 | pthread_mutex_unlock (&MUTEX); | ||
748 | break; | 738 | break; |
749 | } | 739 | } |
750 | } | 740 | } |
751 | } | 741 | } |
742 | pthread_mutex_lock (&MUTEX); | ||
743 | for (i = 0; i < MAX_CLIENTS; i++) | ||
744 | { | ||
745 | if (CLIENT_SOCKS[i] == ws->sock) | ||
746 | { | ||
747 | CLIENT_SOCKS[i] = -1; | ||
748 | break; | ||
749 | } | ||
750 | } | ||
751 | pthread_mutex_unlock (&MUTEX); | ||
752 | free (ws); | 752 | free (ws); |
753 | MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); | 753 | MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); |
754 | return NULL; | 754 | return NULL; |
@@ -771,14 +771,13 @@ uh_cb (void *cls, struct MHD_Connection *con, void *con_cls, | |||
771 | (void) extra_in; /* Unused. Silent compiler warning. */ | 771 | (void) extra_in; /* Unused. Silent compiler warning. */ |
772 | (void) extra_in_size; /* Unused. Silent compiler warning. */ | 772 | (void) extra_in_size; /* Unused. Silent compiler warning. */ |
773 | 773 | ||
774 | sock_overflow = MHD_YES; | ||
775 | ws = malloc (sizeof (struct WsData)); | 774 | ws = malloc (sizeof (struct WsData)); |
776 | if (NULL == ws) | 775 | if (NULL == ws) |
777 | abort (); | 776 | abort (); |
778 | memset (ws, 0, sizeof (struct WsData)); | 777 | memset (ws, 0, sizeof (struct WsData)); |
779 | ws->sock = sock; | 778 | ws->sock = sock; |
780 | ws->urh = urh; | 779 | ws->urh = urh; |
781 | 780 | sock_overflow = MHD_YES; | |
782 | pthread_mutex_lock (&MUTEX); | 781 | pthread_mutex_lock (&MUTEX); |
783 | for (i = 0; i < MAX_CLIENTS; i++) | 782 | for (i = 0; i < MAX_CLIENTS; i++) |
784 | { | 783 | { |
@@ -789,13 +788,13 @@ uh_cb (void *cls, struct MHD_Connection *con, void *con_cls, | |||
789 | break; | 788 | break; |
790 | } | 789 | } |
791 | } | 790 | } |
791 | pthread_mutex_unlock (&MUTEX); | ||
792 | if (sock_overflow) | 792 | if (sock_overflow) |
793 | { | 793 | { |
794 | free (ws); | 794 | free (ws); |
795 | MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); | 795 | MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); |
796 | return; | 796 | return; |
797 | } | 797 | } |
798 | pthread_mutex_unlock (&MUTEX); | ||
799 | if (0 != pthread_create (&pt, NULL, &run_usock, ws)) | 798 | if (0 != pthread_create (&pt, NULL, &run_usock, ws)) |
800 | abort (); | 799 | abort (); |
801 | /* Note that by detaching like this we make it impossible to ensure | 800 | /* Note that by detaching like this we make it impossible to ensure |