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 Http {
18
19class Stream;
21class WebSocketChannel;
22
156class PT_HTTP_API WebSocketSession : public Connectable
157{
158 friend class WebSocketService;
159 friend class WebSocketServlet;
160
161 public:
175 WebSocketSession(WebSocketService& service,
176 Stream& stream,
177 const Reply& reply);
178
182
185 WebSocketService& service()
186 { return *_service; }
187
190 const WebSocketService& service() const
191 { return *_service; }
192
197 const std::string& protocol() const
198 { return _protocol; }
199
203
207
216 void beginSend(bool finished = true);
217
226
238
248
256 void beginPing(const char* payload = 0, std::size_t n = 0);
257
263 void endPing();
264
273 void beginShutdown(unsigned code = 1000,
274 const std::string& reason = std::string());
275
282
289 void close();
290
293 unsigned closeCode() const;
294
297 const std::string& closeReason() const;
298
299 protected:
302 virtual void onInput() = 0;
303
306 virtual void onOutput() = 0;
307
312 virtual void onPingReady();
313
319 virtual void onShutdownReady();
320
327 virtual void onClosed();
328
335 virtual void onPong(const std::string& payload);
336
337 private:
338 void onInputReady();
339
340 void onOutputReady();
341
342 void onChannelPingReady();
343
344 void onChannelShutdownReady();
345
346 void onChannelClosed();
347
348 private:
349 WebSocketService* _service;
350 WebSocketChannel* _channel;
351 std::string _protocol;
352};
353
354} // namespace Http
355
356} // namespace Pt
357
358#endif
Connectable()
Default constructor.
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
HTTP reply message.
Definition Reply.h:64
One HTTP stream on a connection.
Definition Stream.h:92
Payload of one text or binary WebSocket message.
Definition WebSocketMessage.h:110
Mapped service and factory for WebSocket sessions.
Definition WebSocketService.h:132
WebSocketSession(WebSocketService &service, Stream &stream, const Reply &reply)
Binds stream for service.
virtual void onInput()=0
Called when data bytes were received.
virtual void onShutdownReady()
Reports that the shutdown operation completed.
unsigned closeCode() const
Returns the close status code of the handshake that ended the stream.
virtual void onPong(const std::string &payload)
Reports a received pong.
virtual void onOutput()=0
Called when data bytes were sent.
void endPing()
Completes the ping started by beginPing().
~WebSocketSession()
Closes the stream.
const std::string & closeReason() const
Returns the close reason of the handshake that ended the stream.
const std::string & protocol() const
Returns the protocol name selected on the 101.
Definition WebSocketSession.h:197
void beginSend(bool finished=true)
Begins sending outgoing().
const WebSocketService & service() const
Returns the service that created this session.
Definition WebSocketSession.h:190
void close()
Releases the stream and writes no frame.
WebSocketService & service()
Returns the service that created this session.
Definition WebSocketSession.h:185
void beginPing(const char *payload=0, std::size_t n=0)
Begins writing a ping with an optional payload of at most 125 bytes.
virtual void onClosed()
Reports that the engine has ended the protocol.
virtual void onPingReady()
Reports that the ping operation completed.
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
MessageProgress endReceive()
Completes the receive started by beginReceive().
void endShutdown()
Completes the shutdown started by beginShutdown().
MessageProgress endSend()
Completes the send started by beginSend().
WebSocketMessage & incoming()
Returns the incoming message.
void beginShutdown(unsigned code=1000, const std::string &reason=std::string())
Begins a close handshake.
HTTP clients and servers.
Core module.
Definition Allocator.h:33