WebSocketMessage.h
1/* Copyright (C) 2026 by Marc Boris Duerner
2 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
3*/
4
5#ifndef PT_HTTP_WEBSOCKETMESSAGE_H
6#define PT_HTTP_WEBSOCKETMESSAGE_H
7
8#include <Pt/Http/Api.h>
9#include <Pt/NonCopyable.h>
10#include <iostream>
11#include <streambuf>
12#include <vector>
13#include <cstddef>
14
15namespace Pt {
16
17namespace Http {
18
19class WebSocketChannel;
20
21
25class WebSocketMessageBuffer : public std::streambuf
26{
27 public:
29 WebSocketMessageBuffer();
30
32 ~WebSocketMessageBuffer();
33
35 void discard();
36
38 void discard(std::size_t n);
39
41 std::size_t size() const;
42
44 const char* data() const;
45
46 protected:
47 virtual int_type overflow(int_type ch);
48
49 virtual std::streamsize xsputn(const char* data, std::streamsize n);
50
51 virtual int_type underflow();
52
53 private:
54 std::vector<char> _payload;
55};
56
57
109class PT_HTTP_API WebSocketMessage : private NonCopyable
110{
111 friend class WebSocketChannel;
112
113 public:
117
121
124 enum Type
125 {
129 };
130
133 Type type() const
134 { return _type; }
135
141
144 std::iostream& body()
145 { return _body; }
146
149 std::size_t available() const;
150
153 std::size_t pending() const;
154
157 void discard();
158
164 void discard(std::size_t n);
165
168 void clear();
169
171 WebSocketMessageBuffer& buffer()
172 { return _buf; }
173
174 private:
175 void setTypeFromEngine(Type type);
176
178 void setBuffer(std::streambuf& sb);
179
180 private:
181 Type _type;
182 WebSocketMessageBuffer _buf;
183 std::iostream _body;
184};
185
186} // namespace Http
187
188} // namespace Pt
189
190#endif
Type
Data type of a WebSocket message.
Definition WebSocketMessage.h:125
@ Text
UTF-8 text.
Definition WebSocketMessage.h:127
@ Unknown
No data type yet.
Definition WebSocketMessage.h:126
@ Binary
Binary payload.
Definition WebSocketMessage.h:128
WebSocketMessage()
Creates an empty message.
void discard()
Drops the buffered body and leaves the type.
void setType(Type type)
Sets the data type of an outgoing message.
void discard(std::size_t n)
Drops up to n buffered body bytes and leaves the type.
std::iostream & body()
Returns the payload stream.
Definition WebSocketMessage.h:144
std::size_t pending() const
Returns how many payload bytes are waiting to be sent.
void clear()
Drops the buffered body and sets the type to Unknown.
std::size_t available() const
Returns how many payload bytes can be read.
Type type() const
Returns the data type of this message.
Definition WebSocketMessage.h:133
~WebSocketMessage()
Destroys the message.
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
HTTP clients and servers.
Core module.
Definition Allocator.h:33