clean-and-add-pedantic-remove-long-long.patch
authorAndy Green <andy@warmcat.com>
Sun, 19 Dec 2010 20:50:01 +0000 (20:50 +0000)
committerAndy Green <andy@warmcat.com>
Sun, 19 Dec 2010 20:50:01 +0000 (20:50 +0000)
Signed-off-by: Andy Green <andy@warmcat.com>
lib/Makefile.am
lib/Makefile.in
lib/handshake.c
lib/libwebsockets.c
lib/libwebsockets.h
lib/private-libwebsockets.h
test-server/test-server.c

index b30712e..41d73c0 100644 (file)
@@ -6,7 +6,7 @@ dist_libwebsockets_la_SOURCES=libwebsockets.c \
                                md5.c \
                                libwebsockets.h \
                                private-libwebsockets.h
-libwebsockets_la_CFLAGS=-Wall -Werror -rdynamic -fPIC -c
+libwebsockets_la_CFLAGS=-Wall -Werror -pedantic -rdynamic -fPIC -c
 libwebsockets_la_LDFLAGS=-version-info 0:2
 
 all-local:
index f7ce9f8..4397b7f 100644 (file)
@@ -218,7 +218,7 @@ dist_libwebsockets_la_SOURCES = libwebsockets.c \
                                libwebsockets.h \
                                private-libwebsockets.h
 
-libwebsockets_la_CFLAGS = -Wall -Werror -rdynamic -fPIC -c
+libwebsockets_la_CFLAGS = -Wall -Werror -pedantic -rdynamic -fPIC -c
 libwebsockets_la_LDFLAGS = -version-info 0:2
 all: all-am
 
index 35059d0..74f3954 100644 (file)
 #include "private-libwebsockets.h"
 
 
-static int interpret_key(const char *key, unsigned long *result)
+static int
+interpret_key(const char *key, unsigned long *result)
 {
        char digits[20];
        int digit_pos = 0;
        const char *p = key;
        unsigned int spaces = 0;
-       unsigned long long acc;
+       unsigned long acc = 0;
+       int rem = 0;
 
        while (*p) {
                if (isdigit(*p)) {
@@ -51,12 +53,19 @@ static int interpret_key(const char *key, unsigned long *result)
        if (!spaces)
                return -3;
 
-       /*
-        * long long is absolutely needed since "digits" can be a multiple
-        * of a 32-bit range number
-        */
-       acc = atoll(digits);
-       *result = acc / spaces;
+       p = &digits[0];
+       while (*p) {
+               rem = (rem * 10) + ((*p++) - '0');
+               acc = (acc * 10) + (rem / spaces);
+               rem -= (rem / spaces) * spaces;
+       }
+
+       if (rem) {
+               fprintf(stderr, "nonzero handshake remainder\n");
+               return -1;
+       }
+
+       *result = acc;
 
        return 0;
 }
index bfc5615..5e21ce6 100644 (file)
@@ -94,7 +94,7 @@ libwebsocket_close_and_free_session(struct libwebsocket *wsi)
                if (wsi->utf8_token[n].token)
                        free(wsi->utf8_token[n].token);
 
-//     fprintf(stderr, "closing fd=%d\n", wsi->sock);
+/*     fprintf(stderr, "closing fd=%d\n", wsi->sock); */
 
 #ifdef LWS_OPENSSL_SUPPORT
        if (use_ssl) {
@@ -184,9 +184,9 @@ libwebsocket_poll_connections(struct libwebsocket_context *this)
                                        continue;
 
                                this->wsi[n]->protocol-> callback(this->wsi[n],
-                                                LWS_CALLBACK_BROADCAST, 
-                                                this->wsi[n]->user_space,
-                                                buf + LWS_SEND_BUFFER_PRE_PADDING, len);
+                                       LWS_CALLBACK_BROADCAST, 
+                                       this->wsi[n]->user_space,
+                                       buf + LWS_SEND_BUFFER_PRE_PADDING, len);
                        }
 
                        continue;
@@ -204,7 +204,7 @@ libwebsocket_poll_connections(struct libwebsocket_context *this)
                        continue;
                }
                if (!n) {
-//                     fprintf(stderr, "POLLIN with 0 len waiting\n");
+/*                     fprintf(stderr, "POLLIN with 0 len waiting\n"); */
                                libwebsocket_close_and_free_session(
                                                             this->wsi[client]);
                        goto nuke_this;
@@ -319,8 +319,10 @@ int libwebsocket_create_server(int port,
                OpenSSL_add_all_algorithms();
                SSL_load_error_strings();
 
-                       // Firefox insists on SSLv23 not SSLv3
-                       // Konq disables SSLv2 by default now, SSLv23 works
+               /*
+                * Firefox insists on SSLv23 not SSLv3
+                * Konq disables SSLv2 by default now, SSLv23 works
+                */
 
                method = (SSL_METHOD *)SSLv23_server_method();
                if (!method) {
@@ -485,7 +487,7 @@ int libwebsocket_create_server(int port,
                 */
 
                /* give server fork a chance to start up */
-               sleep(1);
+               usleep(500000);
 
                for (client = 1; client < this->count_protocols + 1; client++) {
                        fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -764,6 +766,16 @@ libwebsockets_broadcast(const struct libwebsocket_protocols * protocol,
                return 0;
        }
 
+       /*
+        * We're being called from a different process context than the server
+        * loop.  Instead of broadcasting directly, we send our
+        * payload on a socket to do the IPC; the server process will serialize
+        * the broadcast action in its main poll() loop.
+        *
+        * There's one broadcast socket listening for each protocol supported
+        * set up when the websocket server initializes
+        */
+
        n = send(protocol->broadcast_socket_user_fd, buf, len, 0);
 
        return n;
index b1ae7db..792f2c6 100644 (file)
@@ -28,7 +28,7 @@ enum libwebsocket_callback_reasons {
        LWS_CALLBACK_CLOSED,
        LWS_CALLBACK_RECEIVE,
        LWS_CALLBACK_HTTP,
-       LWS_CALLBACK_BROADCAST,
+       LWS_CALLBACK_BROADCAST
 };
 
 enum libwebsocket_write_protocol {
index 9c69a4c..777ae30 100644 (file)
 
 
 #ifdef DEBUG
-#define debug(format, args...)  \
-      fprintf(stderr, format , ## args)
+#define debug(  \
+      fprintf(stderr, 
 #else
-#define debug(format, args...)
+static void inline debug(const char *format, ...) { }
 #endif
 
 #ifdef LWS_OPENSSL_SUPPORT
index 4e63cc4..4688c2c 100644 (file)
@@ -44,8 +44,6 @@
 
 
 #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
-static int port = 7681;
-static int use_ssl;
 
 /* this protocol server (always the first one) just knows how to do HTTP */
 
@@ -80,6 +78,14 @@ static int callback_http(struct libwebsocket *wsi,
 
 /* dumb_increment protocol */
 
+/*
+ * one of these is auto-created for each connection and a pointer to the
+ * appropriate instance is passed to the callback in the user parameter
+ *
+ * for this example protocol we use it to individualize the count for each
+ * connection.
+ */
+
 struct per_session_data__dumb_increment {
        int number;
 };
@@ -134,40 +140,15 @@ callback_dumb_increment(struct libwebsocket *wsi,
 
 /* lws-mirror_protocol */
 
-#define MAX_MESSAGE_QUEUE 64
-
-struct per_session_data__lws_mirror {
-       struct libwebsocket *wsi;
-       int ringbuffer_tail;
-};
-
-struct a_message {
-       void *payload;
-       size_t len;
-};
-
-static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
-static int ringbuffer_head;
-
-
 static int
 callback_lws_mirror(struct libwebsocket *wsi,
                        enum libwebsocket_callback_reasons reason,
                                               void *user, void *in, size_t len)
 {
        int n;
-       char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
-                                                 LWS_SEND_BUFFER_POST_PADDING];
-       unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
-       struct per_session_data__lws_mirror *pss = user;
 
        switch (reason) {
 
-       case LWS_CALLBACK_ESTABLISHED:
-               pss->wsi = wsi;
-               pss->ringbuffer_tail = ringbuffer_head;
-               break;
-
        case LWS_CALLBACK_BROADCAST:
                n = libwebsocket_write(wsi, in, len, LWS_WRITE_TEXT);
                break;
@@ -199,24 +180,22 @@ callback_lws_mirror(struct libwebsocket *wsi,
 /* list of supported protocols and callbacks */
 
 static struct libwebsocket_protocols protocols[] = {
-       {
+       /* first protocol must always be HTTP handler */
+       [0] = {
                .name = "http-only",
                .callback = callback_http,
-               .per_session_data_size = 0,
        },
-       {
+       [1] = {
                .name = "dumb-increment-protocol",
                .callback = callback_dumb_increment,
                .per_session_data_size =
                                sizeof(struct per_session_data__dumb_increment),
        },
-       {
+       [2] = {
                .name = "lws-mirror-protocol",
                .callback = callback_lws_mirror,
-               .per_session_data_size =
-                               sizeof(struct per_session_data__lws_mirror),
        },
-       {  /* end of list */
+       [3] = {  /* end of list */
                .callback = NULL
        }
 };
@@ -237,13 +216,15 @@ int main(int argc, char **argv)
                        LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
        unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 +
                                                  LWS_SEND_BUFFER_POST_PADDING];
+       int port = 7681;
+       int use_ssl = 0;
 
        fprintf(stderr, "libwebsockets test server\n"
                        "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
                                                    "licensed under LGPL2.1\n");
 
        while (n >= 0) {
-               n = getopt_long(argc, argv, "hp:", options, NULL);
+               n = getopt_long(argc, argv, "hsp:", options, NULL);
                if (n < 0)
                        continue;
                switch (n) {
@@ -278,11 +259,11 @@ int main(int argc, char **argv)
 
        while (1) {
                
-               sleep(1);
+               usleep(50000);
 
                /*
                 * This broadcasts to all dumb-increment-protocol connections
-                * once per second.
+                * at 20Hz.
                 * 
                 * We're just sending a character 'x', in these examples the
                 * callbacks send their own per-connection content.
@@ -293,7 +274,7 @@ int main(int argc, char **argv)
                 * We take care of pre-and-post padding allocation.
                 */
 
-               /* [1] == dumb-increment-protocol */
+               /* protocols[1] == dumb-increment-protocol */
                libwebsockets_broadcast(&protocols[1],
                                        &buf[LWS_SEND_BUFFER_PRE_PADDING], 1);
        }