optimize extpoll fd delete
[profile/ivi/libwebsockets.git] / test-server / test-server-extpoll.c
index 3289ba7..5025f4d 100644 (file)
@@ -139,13 +139,18 @@ static int callback_http(struct libwebsocket_context * this,
                break;
 
        case LWS_CALLBACK_DEL_POLL_FD:
-               for (n = 0; n < count_pollfds; n++)
-                       if (pollfds[n].fd == (int)(long)user)
-                               while (n < count_pollfds) {
-                                       pollfds[n] = pollfds[n + 1];
-                                       n++;
-                               }
-               count_pollfds--;
+               for (n = 0; n < count_pollfds; n++) {
+                       if (pollfds[n].fd != (int)(long)user)
+                               continue;
+                       /*
+                        * swap the end guy into our vacant slot...
+                        * works ok if n is the end guy
+                        */
+                       pollfds[n] = pollfds[count_pollfds - 1];
+                       pollfds[count_pollfds - 1].fd = -1;
+                       count_pollfds--;
+                       break;
+               }
                break;
 
        case LWS_CALLBACK_SET_MODE_POLL_FD:
@@ -254,7 +259,7 @@ callback_dumb_increment(struct libwebsocket_context * this,
                n = sprintf((char *)p, "%d", pss->number++);
                n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
                if (n < 0) {
-                       fprintf(stderr, "ERROR writing to socket");
+                       fprintf(stderr, "ERROR %d writing to socket\n", n);
                        return 1;
                }
                break;
@@ -332,7 +337,7 @@ callback_lws_mirror(struct libwebsocket_context * this,
                                                                LWS_WRITE_TEXT);
 
                        if (n < 0) {
-                               fprintf(stderr, "ERROR writing to socket");
+                               fprintf(stderr, "ERROR %d writing to socket\n", n);
                                exit(1);
                        }