1//
2// WebSocketImpl.h
3//
4// Library: Net
5// Package: WebSocket
6// Module: WebSocketImpl
7//
8// Definition of the StreamSocketImpl class.
9//
10// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Net_WebSocketImpl_INCLUDED
18#define Net_WebSocketImpl_INCLUDED
19
20
21#include "Poco/Net/StreamSocketImpl.h"
22#include "Poco/Buffer.h"
23#include "Poco/Random.h"
24#include "Poco/Buffer.h"
25
26
27namespace Poco {
28namespace Net {
29
30
31class HTTPSession;
32
33
34class Net_API WebSocketImpl: public StreamSocketImpl
35 /// This class implements a WebSocket, according
36 /// to the WebSocket protocol described in RFC 6455.
37{
38public:
39 WebSocketImpl(StreamSocketImpl* pStreamSocketImpl, HTTPSession& session, bool mustMaskPayload);
40 /// Creates a WebSocketImpl.
41
42 // StreamSocketImpl
43 virtual int sendBytes(const void* buffer, int length, int flags);
44 /// Sends a WebSocket protocol frame.
45
46 virtual int receiveBytes(void* buffer, int length, int flags);
47 /// Receives a WebSocket protocol frame.
48
49 virtual int receiveBytes(Poco::Buffer<char>& buffer, int flags);
50 /// Receives a WebSocket protocol frame.
51
52 virtual SocketImpl* acceptConnection(SocketAddress& clientAddr);
53 virtual void connect(const SocketAddress& address);
54 virtual void connect(const SocketAddress& address, const Poco::Timespan& timeout);
55 virtual void connectNB(const SocketAddress& address);
56 virtual void bind(const SocketAddress& address, bool reuseAddress = false);
57 virtual void bind(const SocketAddress& address, bool reuseAddress, bool reusePort);
58 virtual void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false);
59 virtual void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only);
60 virtual void listen(int backlog = 64);
61 virtual void close();
62 virtual void shutdownReceive();
63 virtual void shutdownSend();
64 virtual void shutdown();
65 virtual int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
66 virtual int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
67 virtual void sendUrgent(unsigned char data);
68 virtual int available();
69 virtual bool secure() const;
70 virtual void setSendTimeout(const Poco::Timespan& timeout);
71 virtual Poco::Timespan getSendTimeout();
72 virtual void setReceiveTimeout(const Poco::Timespan& timeout);
73 virtual Poco::Timespan getReceiveTimeout();
74
75 // Internal
76 int frameFlags() const;
77 /// Returns the frame flags of the most recently received frame.
78
79 bool mustMaskPayload() const;
80 /// Returns true if the payload must be masked.
81
82protected:
83 enum
84 {
85 FRAME_FLAG_MASK = 0x80,
86 MAX_HEADER_LENGTH = 14
87 };
88
89 int receiveHeader(char mask[4], bool& useMask);
90 int receivePayload(char *buffer, int payloadLength, char mask[4], bool useMask);
91 int receiveNBytes(void* buffer, int bytes);
92 int receiveSomeBytes(char* buffer, int bytes);
93 virtual ~WebSocketImpl();
94
95private:
96 WebSocketImpl();
97
98 StreamSocketImpl* _pStreamSocketImpl;
99 Poco::Buffer<char> _buffer;
100 int _bufferOffset;
101 int _frameFlags;
102 bool _mustMaskPayload;
103 Poco::Random _rnd;
104};
105
106
107//
108// inlines
109//
110inline int WebSocketImpl::frameFlags() const
111{
112 return _frameFlags;
113}
114
115
116inline bool WebSocketImpl::mustMaskPayload() const
117{
118 return _mustMaskPayload;
119}
120
121
122} } // namespace Poco::Net
123
124
125#endif // Net_WebSocketImpl_INCLUDED
126