optimize extpoll fd delete
[profile/ivi/libwebsockets.git] / test-server / test-server-extpoll.c
index f2f68b4..5025f4d 100644 (file)
@@ -105,7 +105,8 @@ static int callback_http(struct libwebsocket_context * this,
                if (libwebsockets_serve_http_file(wsi,
                                  LOCAL_RESOURCE_PATH"/test.html", "text/html"))
                        fprintf(stderr, "Failed to send HTTP file\n");
-               break;
+               /* we are done with this connnection */
+               return 1;
 
        /*
         * callback for confirming to continue with client IP appear in
@@ -138,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:
@@ -253,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;
@@ -331,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);
                        }
 
@@ -425,6 +431,7 @@ static struct libwebsocket_protocols protocols[] = {
 
 static struct option options[] = {
        { "help",       no_argument,            NULL, 'h' },
+       { "debug",      required_argument,      NULL, 'd' },
        { "port",       required_argument,      NULL, 'p' },
        { "ssl",        no_argument,            NULL, 's' },
        { "killmask",   no_argument,            NULL, 'k' },
@@ -454,10 +461,13 @@ int main(int argc, char **argv)
                                                    "licensed under LGPL2.1\n");
 
        while (n >= 0) {
-               n = getopt_long(argc, argv, "i:khsp:", options, NULL);
+               n = getopt_long(argc, argv, "i:khsp:d:", options, NULL);
                if (n < 0)
                        continue;
                switch (n) {
+               case 'd':
+                       lws_set_log_level(atoi(optarg), NULL);
+                       break;
                case 's':
                        use_ssl = 1;
                        break;
@@ -474,7 +484,8 @@ int main(int argc, char **argv)
                        break;
                case 'h':
                        fprintf(stderr, "Usage: test-server "
-                                            "[--port=<p>] [--ssl]\n");
+                                       "[--port=<p>] [--ssl] "
+                                       "[-d <log bitfield>]\n");
                        exit(1);
                }
        }
@@ -484,7 +495,8 @@ int main(int argc, char **argv)
 
        context = libwebsocket_create_context(port, interface_ptr, protocols,
                                        libwebsocket_internal_extensions,
-                                       cert_path, key_path, -1, -1, opts, NULL);
+                                       cert_path, key_path, NULL, -1, -1,
+                                       opts, NULL);
        if (context == NULL) {
                fprintf(stderr, "libwebsocket init failed\n");
                return -1;