WebSocketSession.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_WEBSOCKETSESSION_H
6#define PT_HTTP_WEBSOCKETSESSION_H
7
8#include <Pt/Http/Api.h>
9#include <Pt/Http/WebSocketMessage.h>
10#include <Pt/Http/Message.h>
11#include <Pt/Connectable.h>
12#include <string>
13#include <cstddef>
14
15namespace Pt {
16
17namespace System {
18class EventLoop;
19}
20
21namespace Http {
22
23class Stream;
26class WebSocketConnection;
27
125class PT_HTTP_API WebSocketSession : public Connectable
126{
127 friend class WebSocketServlet;
128
129 public:
138 WebSocketSession(WebSocketServlet& servlet,
140 Stream& stream);
141
145
149 { return *_service; }
150
154 { return *_service; }
155
159 { return *_loop; }
160
163 const System::EventLoop& loop() const
164 { return *_loop; }
165
169
173
176 void beginSend();
177
183
187
193
196 void ping(const char* payload = 0, std::size_t n = 0);
197
200 void close(unsigned code = 1000,
201 const std::string& reason = std::string());
202
205 unsigned closeCode() const;
206
209 const std::string& closeReason() const;
210
211 protected:
214 virtual void onInput() = 0;
215
218 virtual void onOutput() = 0;
219
225 virtual void onClose() = 0;
226
227 private:
228 void shutdown();
229
230 void onInputReady();
231
232 void onOutputReady();
233
234 void onClosed();
235
236 private:
237 WebSocketService* _service;
238 WebSocketServlet* _servlet;
239 System::EventLoop* _loop;
240 WebSocketConnection* _connection;
241};
242
243} // namespace Http
244
245} // namespace Pt
246
247#endif
Connectable()
Default constructor.
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
One HTTP stream on a connection.
Definition Stream.h:90
Payload of one text or binary WebSocket message.
Definition WebSocketMessage.h:71
Factory and endpoint policy for WebSocket sessions.
Definition WebSocketService.h:107
Release scope of the sessions one WebSocket service creates.
Definition WebSocketServlet.h:66
void beginSend()
Begins sending outgoing().
virtual void onInput()=0
Called when data bytes were received.
unsigned closeCode() const
Returns the close status code of the handshake that ended the stream.
void ping(const char *payload=0, std::size_t n=0)
Enqueues a ping with an optional payload of at most 125 bytes.
virtual void onOutput()=0
Called when data bytes were sent.
System::EventLoop & loop()
Returns the loop that serializes this stream.
Definition WebSocketSession.h:158
~WebSocketSession()
Closes the stream.
const std::string & closeReason() const
Returns the close reason of the handshake that ended the stream.
void close(unsigned code=1000, const std::string &reason=std::string())
Enqueues a close frame and ends the stream after the close handshake.
const WebSocketService & service() const
Returns the service that created this session.
Definition WebSocketSession.h:153
WebSocketService & service()
Returns the service that created this session.
Definition WebSocketSession.h:148
WebSocketSession(WebSocketServlet &servlet, System::EventLoop &loop, Stream &stream)
Binds stream on loop.
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
const System::EventLoop & loop() const
Returns the loop that serializes this stream.
Definition WebSocketSession.h:163
MessageProgress endReceive()
Completes the receive started by beginReceive().
MessageProgress endSend()
Completes the send started by beginSend().
WebSocketMessage & incoming()
Returns the incoming message.
virtual void onClose()=0
Called when the stream has ended.
Event loop of a thread or process.
Definition EventLoop.h:83
HTTP clients and servers.
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33