Skip to content

Commit

Permalink
Added simple support for WebSockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
deplinenoise committed Aug 5, 2012
1 parent bd943e0 commit be87aea
Show file tree
Hide file tree
Showing 4 changed files with 883 additions and 87 deletions.
109 changes: 106 additions & 3 deletions demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#ifdef _WIN32
#include <winsock2.h>
Expand All @@ -14,6 +15,14 @@

static int quit = 0;

enum
{
MAX_WSCONN = 8
};

static int ws_connection_count;
static struct WebbyConnection *ws_connections[MAX_WSCONN];

static void test_log(const char* text)
{
printf("[debug] %s\n", text);
Expand Down Expand Up @@ -48,9 +57,83 @@ static int test_dispatch(struct WebbyConnection *connection)
return 1;
}

static int test_ws_connect(struct WebbyConnection *connection)
{
/* Allow websocket upgrades on /wstest */
if (0 == strcmp(connection->request.uri, "/wstest") && ws_connection_count < MAX_WSCONN)
return 0;
else
return 1;
}

static void test_ws_connected(struct WebbyConnection *connection)
{
printf("WebSocket connected\n");
ws_connections[ws_connection_count++] = connection;
}

static void test_ws_closed(struct WebbyConnection *connection)
{
int i;
printf("WebSocket closed\n");

for (i = 0; i < ws_connection_count; i++)
{
if (ws_connections[i] == connection)
{
int remain = ws_connection_count - i;
memmove(ws_connections + i, ws_connections + i + 1, remain * sizeof(struct WebbyConnection *));
--ws_connection_count;
break;
}
}
}

static int test_ws_frame(struct WebbyConnection *connection, const struct WebbyWsFrame *frame)
{
int i = 0;

printf("WebSocket frame incoming\n");
printf(" Frame OpCode: %d\n", frame->opcode);
printf(" Final frame?: %s\n", (frame->flags & WEBBY_WSF_FIN) ? "yes" : "no");
printf(" Masked? : %s\n", (frame->flags & WEBBY_WSF_MASKED) ? "yes" : "no");
printf(" Data Length : %d\n", (int) frame->payload_length);

while (i < frame->payload_length)
{
unsigned char buffer[16];
int remain = frame->payload_length - i;
size_t read_size = remain > sizeof buffer ? sizeof buffer : (size_t) remain;
size_t k;

printf("%08x ", (int) i);

if (0 != WebbyRead(connection, buffer, read_size))
break;

for (k = 0; k < read_size; ++k)
printf("%02x ", buffer[k]);

for (k = read_size; k < 16; ++k)
printf(" ");

printf(" | ");

for (k = 0; k < read_size; ++k)
printf("%c", isprint(buffer[k]) ? buffer[k] : '?');

printf("\n");

i += read_size;
}

return 0;
}

int main(int argc, char *argv[])
{
int i;
int frame_counter = 0;
void *memory;
int memory_size;
struct WebbyServer *server;
Expand All @@ -68,20 +151,25 @@ int main(int argc, char *argv[])
}
#endif

memset(&config, 0, sizeof config);
config.bind_address = "127.0.0.1";
config.listening_port = 8081;
config.flags = 0;
config.flags = WEBBY_SERVER_WEBSOCKETS;
config.connection_max = 4;
config.request_buffer_size = 2048;
config.io_buffer_size = 8192;
config.dispatch = &test_dispatch;
config.log = &test_log;
config.ws_connect = &test_ws_connect;
config.ws_connected = &test_ws_connected;
config.ws_closed = &test_ws_closed;
config.ws_frame = &test_ws_frame;

for (i = 1; i < argc; )
{
if (0 == strcmp(argv[i], "-p"))
{
config.listening_port = atoi(argv[i + 1]);
config.listening_port = (unsigned short) atoi(argv[i + 1]);
i += 2;
}
else if (0 == strcmp(argv[i], "-b"))
Expand All @@ -91,7 +179,7 @@ int main(int argc, char *argv[])
}
else if (0 == strcmp(argv[i], "-d"))
{
config.flags = WEBBY_SERVER_LOG_DEBUG;
config.flags |= WEBBY_SERVER_LOG_DEBUG;
i += 1;
}
else
Expand All @@ -108,14 +196,29 @@ int main(int argc, char *argv[])
return 1;
}

frame_counter = 0;

while (!quit)
{
WebbyServerUpdate(server);

/* Push some test data over websockets */
if (0 == (frame_counter & 0x7f))
{
for (i = 0; i < ws_connection_count; ++i)
{
WebbyBeginSocketFrame(ws_connections[i], WEBBY_WS_OP_TEXT_FRAME);
WebbyPrintf(ws_connections[i], "Hello world over websockets!\n");
WebbyEndSocketFrame(ws_connections[i]);
}
}
#if defined(__APPLE__)
usleep(30 * 1000);
#elif defined(_WIN32)
Sleep(30);
#endif

++frame_counter;
}

WebbyServerShutdown(server);
Expand Down
10 changes: 5 additions & 5 deletions tundra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Build {
CCOPTS = {
{ '-Wall', '-Werror' },
{ '-g'; Config = "*-*-debug" },
{ '-O2'; Config = "*-*-release" },
},
},
},
Expand All @@ -30,12 +31,11 @@ Build {
},
Units = function()
require "tundra.syntax.glob"
Program {
Name = "a.out",
Sources = { Glob { Dir = ".", Extensions = CFiles } },
local demo = Program {
Name = "webbydemo",
Sources = { "demo.c", "webby.c" },
Libs = { { "ws2_32.lib"; Config = "win64-msvc" } },
}

Default "a.out"
Default(demo)
end,
}
Loading

0 comments on commit be87aea

Please sign in to comment.