diff --git a/README.md b/README.md index 81e7da0..ab36243 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,74 @@ easywsclient ============ -A header-only WebSocket client for C++ with no mandatory dependencies \ No newline at end of file +A header-only WebSocket client for C++. Depends only on the standard libraries. +Can make optional use of C++11 features (i.e. std::function and lambda). + +Usage +===== + +The interface looks somewhat like this: + + // Factory method to create a WebSocket: + static pointer from_url(std::string url); + + // Function to perform actual network send()/recv() I/O: + void poll(); + + // Receive a message, and pass it to callable(). Really, this just looks at + // a buffer (filled up by poll()) and decodes any messages in the buffer. + // Callable must have signature: void(const std::string & message). + // Should work with C functions, C++ functors, and C++11 std::function and + // lambda: + template + void dispatch(Callable & callable); + + // Sends a TEXT type message (gets put into a buffer for poll() to send + // later): + void send(std::string message); + +Put altogether, this will look something like this: + + using easywsclient::WebSocket; + WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); + assert(ws); + while (true) { + ws->poll(); + ws->send("hello"); + ws->dispatch(handle_message); + // ...do more stuff... + } + +Example +======= + + # Launch the server + node example-server.js + + # Build and launch the client + g++ example-client.cpp -o example-client + ./example-client + + # Optional: build and launch a C++11 client + g++ -std=gnu++0x example-client-cpp11.cpp -o example-client-cpp11 + ./example-client-cpp11 + + # Expect the output from example-client: + Connected to: ws://localhost:8126/foo + >>> galaxy + >>> world + +Threading +========= + +This library is not thread safe. The user must take care to use locks if +accessing an instance of `WebSocket` from multiple threads. + +Future Work +=========== + +(contributions appreciated!) + +* Parameterize the `pointer` type (especially for `shared_ptr`). +* Support optional integration on top of an async (event-driven) library, + especially Asio. diff --git a/easywsclient.hpp b/easywsclient.hpp new file mode 100644 index 0000000..74a7f63 --- /dev/null +++ b/easywsclient.hpp @@ -0,0 +1,288 @@ +#ifndef EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD +#define EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD + +// This code comes from, +// https://github.com/dhbaird/easywsclient + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +// Experimental WebSocket and JSON implementation for CalVR +// Written by David Baird +namespace easywsclient { + +struct WebSocket +{ + typedef WebSocket * pointer; + #if 0 + http://tools.ietf.org/html/rfc6455#section-5.2 Base Framing Protocol + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-------+-+-------------+-------------------------------+ + |F|R|R|R| opcode|M| Payload len | Extended payload length | + |I|S|S|S| (4) |A| (7) | (16/64) | + |N|V|V|V| |S| | (if payload len==126/127) | + | |1|2|3| |K| | | + +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + + | Extended payload length continued, if payload len == 127 | + + - - - - - - - - - - - - - - - +-------------------------------+ + | |Masking-key, if MASK set to 1 | + +-------------------------------+-------------------------------+ + | Masking-key (continued) | Payload Data | + +-------------------------------- - - - - - - - - - - - - - - - + + : Payload Data continued ... : + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + | Payload Data continued ... | + +---------------------------------------------------------------+ + #endif + struct wsheader_type { + int header_size; + bool fin; + bool mask; + enum opcode_type { + CONTINUATION = 0x0, + TEXT_FRAME = 0x1, + BINARY_FRAME = 0x2, + CLOSE = 8, + PING = 9, + PONG = 0xa, + } opcode; + int N0; + uint64_t N; + uint8_t masking_key[4]; + }; + + std::vector rxbuf; + std::vector txbuf; + + int sockfd; + bool closed; + + static int hostname_connect(std::string hostname, int port) { + struct addrinfo hints; + struct addrinfo *result; + struct addrinfo *p; + int ret; + int sockfd = -1; + char sport[16]; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + snprintf(sport, 16, "%d", port); + if ((ret = getaddrinfo(hostname.c_str(), sport, &hints, &result)) != 0) + { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); + return 1; + } + for(p = result; p != NULL; p = p->ai_next) + { + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sockfd == -1) { continue; } + if (connect(sockfd, p->ai_addr, p->ai_addrlen) != -1) { + break; + } + close(sockfd); + sockfd = -1; + } + freeaddrinfo(result); + return sockfd; + } + + static pointer from_url(std::string url) { + char host[128]; + int port; + char path[128]; + if (false) { } + else if (sscanf(url.c_str(), "ws://%[^:]:%d/%s", host, &port, path) == 3) { + } + else if (sscanf(url.c_str(), "ws://%[^/]/%s", host, path) == 2) { + port = 80; + } + else { + fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str()); + return NULL; + } + int sockfd = hostname_connect(host, port); + if (sockfd == -1) { + fprintf(stderr, "Unable to connect to %s:%d\n", host, port); + return NULL; + } + { + // XXX: this should be done non-blocking, + char line[256]; + int status; + int i; + snprintf(line, 256, "GET /%s HTTP/1.1\r\n", path); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "Host: %s:%d\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "Upgrade: websocket\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "Connection: Upgrade\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "Sec-WebSocket-Version: 13\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 256, "\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + for (i = 0; i < 2 || i < 255 && line[i-2] != '\r' && line[i-1] != '\n'; ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } + line[i] = 0; + if (i == 255) { fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n", url.c_str()); return NULL; } + if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) { fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(), line); return NULL; } + // TODO: verify response headers, + while (true) { + for (i = 0; i < 2 || i < 255 && line[i-2] != '\r' && line[i-1] != '\n'; ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } + if (line[0] == '\r' && line[1] == '\n') { break; } + } + } + int flag = 1; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm + fcntl(sockfd, F_SETFL, O_NONBLOCK); + fprintf(stderr, "Connected to: %s\n", url.c_str()); + return pointer(new WebSocket(sockfd)); + } + + WebSocket(int sockfd) : sockfd(sockfd), closed(false) { + } + + void poll() { + if (closed) { return; } + while (true) { + // FD_ISSET(0, &rfds) will be true + int N = rxbuf.size(); + ssize_t ret; + rxbuf.resize(N + 1500); + ret = recv(sockfd, &rxbuf[0] + N, 1500, 0); + if (false) { } + else if (ret < 0) { + rxbuf.resize(N); + break; + } + else if (ret == 0) { + rxbuf.resize(N); + closed = true; + close(sockfd); + break; + } + else { + rxbuf.resize(N + ret); + } + } + while (txbuf.size()) { + int ret; + ret = ::send(sockfd, &txbuf[0], txbuf.size(), 0); + if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); } + else { break; } + } + } + + // Callable must have signature: void(const std::string & message). + // Should work with C functions, C++ functors, and C++11 std::function and + // lambda: + template + void dispatch(Callable callable) { + // TODO: consider acquiring a lock on rxbuf... + while (true) { + wsheader_type ws; + if (rxbuf.size() < 2) { return; /* Need at least 2 */ } + const uint8_t * data = (uint8_t *) &rxbuf[0]; // peek, but don't consume + ws.fin = (data[0] & 0x80) == 0x80; + ws.opcode = (wsheader_type::opcode_type) (data[0] & 0x0f); + ws.mask = (data[1] & 0x80) == 0x80; + ws.N0 = (data[1] & 0x7f); + ws.header_size = 2 + (ws.N0 == 126? 2 : 0) + (ws.N0 == 127? 6 : 0) + (ws.mask? 4 : 0); + if (rxbuf.size() < ws.header_size) { return; /* Need: ws.header_size - rxbuf.size() */ } + int i; + if (ws.N0 < 126) { + ws.N = ws.N0; + i = 2; + } + else if (ws.N0 == 126) { + ws.N = 0; + ws.N |= ((uint64_t) data[2]) << 8; + ws.N |= ((uint64_t) data[3]) << 0; + i = 4; + } + else if (ws.N0 == 127) { + ws.N = 0; + ws.N |= ((uint64_t) data[2]) << 56; + ws.N |= ((uint64_t) data[3]) << 48; + ws.N |= ((uint64_t) data[4]) << 40; + ws.N |= ((uint64_t) data[5]) << 32; + ws.N |= ((uint64_t) data[6]) << 24; + ws.N |= ((uint64_t) data[7]) << 16; + ws.N |= ((uint64_t) data[8]) << 8; + ws.N |= ((uint64_t) data[9]) << 0; + i = 10; + } + if (ws.mask) { + ws.masking_key[0] = ((uint8_t) data[i+0]) << 0; + ws.masking_key[1] = ((uint8_t) data[i+1]) << 0; + ws.masking_key[2] = ((uint8_t) data[i+2]) << 0; + ws.masking_key[3] = ((uint8_t) data[i+3]) << 0; + } + else { + ws.masking_key[0] = 0; + ws.masking_key[1] = 0; + ws.masking_key[2] = 0; + ws.masking_key[3] = 0; + } + if (rxbuf.size() < ws.header_size+ws.N) { return; /* Need: ws.header_size+ws.N - rxbuf.size() */ } + + // We got a whole message, now do something with it: + if (false) { } + else if (ws.opcode == wsheader_type::TEXT_FRAME && ws.fin) { + if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } } + std::string data(rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+ws.N); + callable((const std::string) data); + } + else if (ws.opcode == wsheader_type::PING) { } + else if (ws.opcode == wsheader_type::PONG) { } + else if (ws.opcode == wsheader_type::CLOSE) { closed = true; close(sockfd); } + else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); closed = true; close(sockfd); } + + rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+ws.N); + } + } + + void send(std::string message) { + // TODO: consider acquiring a lock on txbuf... + std::vector header; + header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0); + header[0] = 0x80 | wsheader_type::TEXT_FRAME; + if (false) { } + else if (message.size() < 126) { + header[1] = message.size(); + } + else if (message.size() < 65536) { + header[1] = 126; + header[2] = (message.size() >> 8) & 0xff; + header[3] = (message.size() >> 0) & 0xff; + } + else { // TODO: run coverage testing here + header[1] = 127; + header[2] = (message.size() >> 56) & 0xff; + header[3] = (message.size() >> 48) & 0xff; + header[4] = (message.size() >> 40) & 0xff; + header[5] = (message.size() >> 32) & 0xff; + header[6] = (message.size() >> 24) & 0xff; + header[7] = (message.size() >> 16) & 0xff; + header[8] = (message.size() >> 8) & 0xff; + header[9] = (message.size() >> 0) & 0xff; + } + txbuf.insert(txbuf.end(), header.begin(), header.end()); + txbuf.insert(txbuf.end(), message.begin(), message.end()); + } + +}; + +} // namespace easywsclient + +#endif /* EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD */ diff --git a/example-client-cpp11.cpp b/example-client-cpp11.cpp new file mode 100644 index 0000000..e3f1749 --- /dev/null +++ b/example-client-cpp11.cpp @@ -0,0 +1,25 @@ +// Compile with: +// g++ -std=gnu++0x example-client-cpp11.cpp -o example-client-cpp11 +#include "easywsclient.hpp" +#include +#include +#include + +int main() +{ + using easywsclient::WebSocket; + WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); + assert(ws); + ws->send("goodbye"); + ws->send("hello"); + while (true) { + std::string message; + ws->poll(); + ws->dispatch([&message](const std::string & message_) { + printf(">>> %s\n", message_.c_str()); + message = message_; + }); + if (message == "world") { break; } + } + return 0; +} diff --git a/example-client.cpp b/example-client.cpp new file mode 100644 index 0000000..797cc21 --- /dev/null +++ b/example-client.cpp @@ -0,0 +1,28 @@ +#include "easywsclient.hpp" +#include +#include +#include + +// N.B. A real application should abuse a global variable like this... +// (gets messy if threads are involved) +std::string message; + +void handle_message(const std::string & message) +{ + printf(">>> %s\n", message.c_str()); + ::message = message; +} + +int main() +{ + using easywsclient::WebSocket; + WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); + assert(ws); + ws->send("goodbye"); + ws->send("hello"); + while (message != "world") { + ws->poll(); + ws->dispatch(handle_message); + } + return 0; +} diff --git a/example-server.js b/example-server.js new file mode 100644 index 0000000..4eb17f9 --- /dev/null +++ b/example-server.js @@ -0,0 +1,36 @@ +/* + Prerequisites: + + 1. Install node.js and npm + 2. npm install ws + + See also, + + http://einaros.github.com/ws/ + + To run, + + node example-server.js +*/ + +"use strict"; // http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ +var WebSocketServer = require('ws').Server; +var http = require('http'); + +var server = http.createServer(); +var wss = new WebSocketServer({server: server, path: '/foo'}); +wss.on('connection', function(ws) { + console.log('/foo connected'); + ws.on('message', function(data, flags) { + if (flags.binary) { return; } + console.log('/foo >>> ' + data); + if (data == 'goodbye') { ws.send('galaxy'); } + if (data == 'hello') { ws.send('world'); } + }); + ws.on('close', function() { + }); + ws.on('error', function(e) { + }); +}); +server.listen(8126); +console.log('Listening on port 8126...');