WebSocket.h
1/*
2 * Copyright (C) 2015 by Laurentiu-Gheorghe Crisan
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28#ifndef PT_HTTP_WEBSOCKET_H
29#define PT_HTTP_WEBSOCKET_H
30
31#include <Pt/Http/Api.h>
32#include <Pt/Connectable.h>
33#include <Pt/Signal.h>
34#include <Pt/System/Timer.h>
35#include <Pt/System/IODevice.h>
36#include <Pt/System/MainLoop.h>
37#include <Pt/Net/TcpSocket.h>
38#include <Pt/Http/IOStream.h>
39
40
41#include <vector>
42
43namespace Pt {
44namespace Http {
45
46class WebSocketParser;
47class WebSocketStream;
48
71class PT_HTTP_API WebSocket : public Pt::System::IODevice, public Pt::Connectable
72{
73 public:
84
88
92
95 virtual ~WebSocket();
96
100
106 void beginConnect(const std::string& url, const std::string& origin = std::string(), bool keepAlive = true);
107
111 {
112 return _connected;
113 }
114
120
124 {
125 _sendFrameMode = m;
126 }
127
131 {
132 return _receiveframeMode;
133 }
134
138
142
143 private:
144 void parseUrl(const std::string& url, const std::string& origin);
145
146 private://Clinet
147 void onConnected();
148
149 void onInput();
150
151 void onOutput();
152
153 private:
154 static std::string createKey();
155
156 private:
157 Pt::uint32_t createMask();
158
159 protected://Pt::System::IODevice
160 virtual void onClose();
161
162 virtual void onSetTimeout(size_t ms);
163
164 virtual std::size_t onBeginRead(Pt::System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
165
166 virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
167
168 virtual std::size_t onEndRead(Pt::System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
169
170 virtual std::size_t onBeginWrite(Pt::System::EventLoop& loop, const char* buffer, std::size_t n);
171
172 virtual std::size_t onWrite(const char* buffer, std::size_t count);
173
174 virtual std::size_t onEndWrite(Pt::System::EventLoop& loop, const char* buffer, std::size_t n);
175
176 virtual void onCancel();
177
178 virtual bool onRun()
179 {
180 return true;
181 }
182
184
186
187 protected:
188 void handleInput(size_t size);
189
190 protected:
191 enum State
192 {
193 Unknown,
194 Connecting,
195 ReceiveHeader,
196 ReceivePayloadLenght,
197 ReceiveMask,
198 ReceivePayloadChunk,
199 Send,
200 };
201
202 private:
203 void handleReceiveHeader(const char* buffer);
204
205 void handleReceiveMask(const char* buffer);
206
207 void handleReceivePayloadLenght(const char* buffer);
208
209 private://Client
210 std::string _host;
211 std::string _path;
212 std::string _protocol;
213 std::string _connectHeader;
214 unsigned short _port;
215 WebSocketStream* _ioStream;
216 Pt::Signal<WebSocket&> _connected;
217 WebSocketParser* _httpParser;
218 size_t _timeout;
219 bool _keepAlive;
220
221 private:
222 Pt::System::MainLoop _dummyLoop;
224
225 private://Read/Write
226 size_t _payloadSize;
227 size_t _currentOffset;
228 char* _userReceiveBuffer;
229 size_t _userReceiveBufferLenght;
230 size_t _userSendBufferLenght;
231 std::vector<char> _buffer;
232 Pt::uint32_t _sendMask;
233 Frame _sendFrameMode;
234 Frame _receiveframeMode;
235 bool _hasMask;
236 Pt::uint32_t _mask;
237 std::vector<char> _currentBuffer;
238 size_t _payloadLenghtType;
239 bool _error;
240 State _state;
241};
242
243}}
244
245#endif
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Stream of an upgraded HTTP connection.
Definition IOStream.h:53
void setSendFrame(Frame m)
Sets the opcode for the next send.
Definition WebSocket.h:123
virtual void onDetach(Pt::System::EventLoop &loop)
Detached from loop.
void endConnect()
Completes the client handshake.
Frame
WebSocket frame opcode.
Definition WebSocket.h:77
@ Text
Text data frame.
Definition WebSocket.h:79
@ Unknow
Unset frame type.
Definition WebSocket.h:78
@ Pong
Pong frame.
Definition WebSocket.h:82
@ Ping
Ping frame.
Definition WebSocket.h:81
@ Binary
Binary data frame.
Definition WebSocket.h:80
WebSocket(Pt::Http::IOStream *stream)
Creates a WebSocket that accepts stream.
virtual void onAttach(Pt::System::EventLoop &loop)
Attached to loop.
virtual void onCancel()
Blocks until operation has cancelled.
void sendPingFrame()
Sends a ping frame.
Pt::Signal< WebSocket & > & connected()
Returns the signal emitted when the handshake finishes.
Definition WebSocket.h:110
Frame receiveFrame() const
Returns the opcode of the last received frame.
Definition WebSocket.h:130
virtual bool onRun()
Check if ready and run.
Definition WebSocket.h:178
void accept(Pt::Http::IOStream *stream)
Accepts an upgraded connection stream.
void beginConnect(const std::string &url, const std::string &origin=std::string(), bool keepAlive=true)
Begins a client handshake to url.
WebSocket()
Creates a closed WebSocket.
void sendPongFrame()
Sends a pong frame.
virtual ~WebSocket()
Destructor.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Endpoint for I/O operations.
Definition IODevice.h:96
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
Platform event loop.
Definition MainLoop.h:51
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
HTTP clients and servers.
Core module.
Definition Allocator.h:33