Stream.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_STREAM_H
6#define PT_HTTP_STREAM_H
7
8#include <Pt/Http/Api.h>
9#include <Pt/Connectable.h>
10#include <Pt/NonCopyable.h>
11#include <Pt/Signal.h>
12#include <string>
13#include <cstddef>
14#include <iosfwd>
15
16namespace Pt {
17
18namespace System {
19class EventLoop;
20}
21
22namespace Http {
23
24class Connection;
25class Channel;
26
90class PT_HTTP_API Stream : public Connectable
91 , private NonCopyable
92{
93 friend class Connection;
94 friend class Channel;
95
96 public:
103
106 bool isValid() const
107 { return _connection != 0; }
108
111 Channel* channel() const
112 { return _channel; }
113
116 const std::string& protocol() const
117 { return _protocolName; }
118
125 void close();
126
129 std::streambuf* buffer();
130
134
139 std::size_t endInput();
140
144
147 std::size_t endOutput();
148
151 void cancel();
152
155 void setTimeout(std::size_t ms);
156
162
166 { return _inputReady; }
167
171 { return _outputReady; }
172
173 protected:
174 Stream(Connection& connection, const std::string& protocol);
175
176 void attachChannel(Channel& channel);
177
178 void detachChannel(Channel& channel);
179
180 private:
181 Connection* _connection;
182 Channel* _channel;
183 std::string _protocolName;
184 Signal<> _inputReady;
185 Signal<> _outputReady;
186};
187
188} // namespace Http
189
190} // namespace Pt
191
192#endif // PT_HTTP_STREAM_H
Connectable()
Default constructor.
Represents a connection between a Signal/Delegate and a slot.
Definition Connection.h:96
Binds one channel to one HTTP stream.
Definition Channel.h:57
One HTTP stream on a connection.
Definition Stream.h:92
Channel * channel() const
Returns the bound channel, or null.
Definition Stream.h:111
void cancel()
Cancels a pending transfer of this stream.
~Stream()
Calls close().
void setTimeout(std::size_t ms)
Sets the stream timeout in milliseconds.
const std::string & protocol() const
Returns the Upgrade header value.
Definition Stream.h:116
void close()
Ends this stream.
bool isValid() const
Returns true when the connection still owns this stream.
Definition Stream.h:106
Signal & inputReady()
Returns the signal emitted when input is ready.
Definition Stream.h:165
System::EventLoop * loop() const
Returns the event loop of the owning connection.
std::streambuf * buffer()
Returns the stream buffer of the connection.
void beginOutput()
Begins a write of the bytes buffered in buffer().
std::size_t endInput()
Completes the read started by beginInput().
std::size_t endOutput()
Completes the write started by beginOutput().
void beginInput()
Begins a read into the stream buffer.
Signal & outputReady()
Returns the signal emitted when output is ready.
Definition Stream.h:170
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Multicast Signal to call multiple slots.
Definition Signal.h:190
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