add-getopts.patch

Signed-off-by: Andy Green <andy@warmcat.com>
This commit is contained in:
Andy Green 2010-10-31 07:40:33 +00:00
parent 4ea6006451
commit ea71ed1c21
3 changed files with 81 additions and 19 deletions

View file

@ -128,7 +128,10 @@ const struct lws_tokens lws_tokens[WSI_TOKEN_COUNT] = {
};
int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, size_t))
int libwebsocket_create_server(int port,
int (*callback)(struct libwebsocket *,
enum libwebsocket_callback_reasons, void *, size_t),
int protocol)
{
int n;
int sockfd, newsockfd;
@ -149,8 +152,18 @@ int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *,
}
wsi->callback = callback;
// wsi->ietf_spec_revision = 0;
wsi->ietf_spec_revision = 76;
switch (protocol) {
case 0:
case 2:
case 76:
fprintf(stderr, "Using protocol v%d\n", protocol);
wsi->ietf_spec_revision = protocol;
break;
default:
fprintf(stderr, "protocol %d not supported (try 0 2 or 76)\n",
protocol);
return -1;
}
/* sit there listening for connects, accept and spawn session servers */
@ -165,7 +178,8 @@ int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *,
serv_addr.sin_port = htons(port);
n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (n < 0) {
fprintf(stderr, "ERROR on binding %d %d\n", n, errno);
fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", port, n,
errno);
return -1;
}
@ -182,7 +196,8 @@ int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *,
if (n)
return 0;
fprintf(stderr, "Listening on port %d\n", port);
listen(sockfd, 5);
while (1) {
@ -539,15 +554,14 @@ int libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len)
p += wsi->utf8_token[WSI_TOKEN_HOST].token_len;
strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token);
p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len;
strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: ");
p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) {
strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: ");
p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len;
} else {
strcpy(p, "none");
p += strlen("none");
}
strcpy(p, "\x0d\x0a\x0d\x0a");
p += strlen("\x0d\x0a\x0d\x0a");
@ -734,6 +748,11 @@ int libwebsocket_write(struct libwebsocket * wsi, unsigned char *buf,
}
break;
}
for (n = 0; n < (len + pre + post); n++)
fprintf(stderr, "%02X ", buf[n - pre]);
fprintf(stderr, "\n");
n = write(wsi->sock, buf - pre, len + pre + post);
if (n < 0) {

View file

@ -10,7 +10,8 @@ struct libwebsocket;
extern int libwebsocket_create_server(int port,
int (*callback)(struct libwebsocket *,
enum libwebsocket_callback_reasons, void *, size_t));
enum libwebsocket_callback_reasons, void *, size_t),
int protocol);
/*
* IMPORTANT NOTICE!

View file

@ -1,10 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "libwebsockets.h"
#define PORT 7681
/*
* libwebsocket Example server Copyright 2010 Andy Green <andy@warmcat.com>
*
* Shows how to use libwebsocket
*/
static int port = 7681;
static int ws_protocol = 76;
static int websocket_callback(struct libwebsocket * wsi,
enum libwebsocket_callback_reasons reason, void *in, size_t len)
@ -12,10 +21,12 @@ static int websocket_callback(struct libwebsocket * wsi,
int n;
char buf[LWS_SEND_BUFFER_PRE_PADDING + 256 + LWS_SEND_BUFFER_POST_PADDING];
static int bump;
static int slow;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
fprintf(stderr, "Websocket connection established\n");
slow = 500;
break;
case LWS_CALLBACK_CLOSED:
@ -23,8 +34,13 @@ static int websocket_callback(struct libwebsocket * wsi,
break;
case LWS_CALLBACK_SEND:
sleep(1);
n = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING], "%d\n", bump++);
slow--;
if (slow) {
usleep(10000);
break;
}
slow = 100;
n = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING], "%d", bump++);
n = libwebsocket_write(wsi, (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], n, 0);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
@ -39,16 +55,42 @@ static int websocket_callback(struct libwebsocket * wsi,
return 0;
}
static struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "protocol", required_argument, NULL, 'r' },
{ NULL, 0, 0, 0 }
};
int main(int argv, char **argc)
int main(int argc, char **argv)
{
if (libwebsocket_create_server(PORT, websocket_callback) < 0) {
int n = 0;
fprintf(stderr, "libwebsockets test server\nCopyright 2010 Andy Green <andy@warmcat.com> licensed under GPL2\n");
while (n >= 0) {
n = getopt_long(argc, argv, "hp:r:", options, NULL);
if (n < 0)
continue;
switch (n) {
case 'p':
port = atoi(optarg);
break;
case 'r':
ws_protocol = atoi(optarg);
break;
case 'h':
fprintf(stderr, "Usage: test-server [--port=<p>] [--protocol=<v>]\n");
exit(1);
}
}
if (libwebsocket_create_server(port, websocket_callback, ws_protocol) < 0) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
fprintf(stderr, "Listening on port %d\n", PORT);
while (1)
sleep(1);