| 1 | // Websocket.cxx -- a base class for websockets |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started April 2014. |
| 4 | // |
| 5 | // Copyright (C) 2014 Torsten Dreyer |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifndef WEBSOCKET_HXX_ |
| 22 | #define WEBSOCKET_HXX_ |
| 23 | |
| 24 | #include "HTTPRequest.hxx" |
| 25 | #include <string> |
| 26 | |
| 27 | namespace flightgear { |
| 28 | namespace http { |
| 29 | |
| 30 | class WebsocketWriter { |
| 31 | public: |
| 32 | virtual ~WebsocketWriter() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | virtual int writeToWebsocket(int opcode, const char * data, size_t len) = 0; |
| 37 | |
| 38 | // ref: http://tools.ietf.org/html/rfc6455#section-5.2 |
| 39 | int writeContinuation(const char * data, size_t len); // { return writeToWebsocket( 0, data, len ); } |
| 40 | int writeText(const char * data, size_t len) |
| 41 | { |
| 42 | return writeToWebsocket(1, data, len); |
| 43 | } |
| 44 | inline int writeText(const std::string & text) |
| 45 | { |
| 46 | return writeText(text.c_str(), text.length()); |
| 47 | } |
| 48 | inline int writeBinary(const char * data, size_t len) |
| 49 | { |
| 50 | return writeToWebsocket(2, data, len); |
| 51 | } |
| 52 | inline int writeConnectionClose(const char * data, size_t len) |
| 53 | { |
| 54 | return writeToWebsocket(8, data, len); |
| 55 | } |
| 56 | inline int writePing(const char * data, size_t len) |
| 57 | { |
| 58 | return writeToWebsocket(9, data, len); |
| 59 | } |
| 60 | inline int writePong(const char * data, size_t len) |
| 61 | { |
| 62 | return writeToWebsocket(0xa, data, len); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class Websocket { |
| 67 | public: |
| 68 | virtual ~Websocket() |
| 69 | { |
| 70 | } |
| 71 | virtual void close() = 0; |
| 72 | virtual void handleRequest(const HTTPRequest & request, WebsocketWriter & writer) = 0; |
| 73 | virtual void poll(WebsocketWriter & writer) = 0; |
| 74 | |
| 75 | }; |
| 76 | |
| 77 | } // namespace http |
| 78 | } // namespace flightgear |
| 79 | |
| 80 | #endif /* WEBSOCKET_HXX_ */ |