1 // Websocket.cxx -- a base class for websockets
3 // Written by Torsten Dreyer, started April 2014.
5 // Copyright (C) 2014 Torsten Dreyer
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.
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.
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.
21 #ifndef WEBSOCKET_HXX_
22 #define WEBSOCKET_HXX_
24 #include "HTTPRequest.hxx"
27 namespace flightgear {
30 class WebsocketWriter {
32 virtual ~WebsocketWriter()
36 virtual int writeToWebsocket(int opcode, const char * data, size_t len) = 0;
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)
42 return writeToWebsocket(1, data, len);
44 inline int writeText(const std::string & text)
46 return writeText(text.c_str(), text.length());
48 inline int writeBinary(const char * data, size_t len)
50 return writeToWebsocket(2, data, len);
52 inline int writeConnectionClose(const char * data, size_t len)
54 return writeToWebsocket(8, data, len);
56 inline int writePing(const char * data, size_t len)
58 return writeToWebsocket(9, data, len);
60 inline int writePong(const char * data, size_t len)
62 return writeToWebsocket(0xa, data, len);
71 virtual void close() = 0;
72 virtual void handleRequest(const HTTPRequest & request, WebsocketWriter & writer) = 0;
73 virtual void poll(WebsocketWriter & writer) = 0;
78 } // namespace flightgear
80 #endif /* WEBSOCKET_HXX_ */