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:
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
#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)) {
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;
}
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) {
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;
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;
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) {
*/
/* 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);
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;
LWS_CALLBACK_CLOSED,
LWS_CALLBACK_RECEIVE,
LWS_CALLBACK_HTTP,
- LWS_CALLBACK_BROADCAST,
+ LWS_CALLBACK_BROADCAST
};
enum libwebsocket_write_protocol {
#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
#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 */
/* 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;
};
/* 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;
/* 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
}
};
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) {
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.
* 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);
}