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 <cstddef>
12
13namespace Pt {
14
15namespace Http {
16
17class WebSocketConnection;
18
70class PT_HTTP_API WebSocketMessage : private NonCopyable
71{
72 friend class WebSocketConnection;
73
74 public:
77 enum Type
78 {
82 };
83
86 Type type() const
87 { return _type; }
88
94
97 std::iostream& body()
98 { return _body; }
99
102 std::size_t available() const;
103
106 std::size_t pending() const;
107
110 void discard();
111
114 void clear();
115
116 private:
117 class PayloadBuffer;
118
119 WebSocketMessage();
120
121 ~WebSocketMessage();
122
123 void setTypeFromEngine(Type type);
124
125 void append(const char* data, std::size_t n);
126
127 void prepareRead();
128
129 const char* sendData() const;
130
131 std::size_t sendSize() const;
132
133 void consume(std::size_t n);
134
135 private:
136 Type _type;
137 PayloadBuffer* _buffer;
138 std::iostream _body;
139};
140
141} // namespace Http
142
143} // namespace Pt
144
145#endif
Type
Data type of a WebSocket message.
Definition WebSocketMessage.h:78
@ Text
UTF-8 text.
Definition WebSocketMessage.h:80
@ Unknown
No data type yet.
Definition WebSocketMessage.h:79
@ Binary
Binary payload.
Definition WebSocketMessage.h:81
void discard()
Drops the buffered body and leaves the type.
void setType(Type type)
Sets the data type of an outgoing message.
std::iostream & body()
Returns the payload stream.
Definition WebSocketMessage.h:97
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:86
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
HTTP clients and servers.
Core module.
Definition Allocator.h:33