#include <Pt/Http/StreamSession.h>

Binds one session object to one HTTP stream. More...

Inherits NonCopyable.

Public Member Functions

bool isOpen () const
 Returns true when a stream is bound.

Protected Member Functions

 StreamSession ()
 Creates a session with no stream.
 StreamSession (Stream &stream)
 Creates a session bound to stream.
 ~StreamSession ()
 Unbinds and closes the stream.
void open (Stream &stream)
 Binds stream.
void close ()
 Unbinds and closes the stream.
Stream * stream () const
 Returns the bound stream, or null.
virtual void onCloseStream (Stream &stream)
 Called when the stream ends.

Detailed Description

StreamSession is the external peer of one Stream. The server or the client owns the connection, and the connection owns the stream. A session does not own either. It stores a pointer to the stream, and the stream stores a pointer back. One stream has one session. A second open() throws std::logic_error.

Binding is how an upgrade is accepted. On the server, Service::onUpgrade() receives the stream the server already owns. A derived session constructs itself with that stream, or calls open() from its own accept method. After onUpgrade() returns, a stream with no session is declined and the server closes it. On the client, Client::upgrade() returns the stream the client already owns, and the derived session opens that stream after the 101 reply.

This base does not transfer bytes. The derived type uses the stream it holds: Stream::buffer(), beginInput(), beginOutput(), and the ready signals of that stream. A framed protocol formats into the stream buffer and connects its own slots to the stream. Application code does not derive this base for WebSocket. The client uses WebSocket, and the server uses WebSocketSession.

open() and close() are protected because the derived type owns the handshake and the shutdown frame. A WebSocket close writes the close frame and then calls close(). A public close on this base would skip that frame.

The destructor unbinds and closes the stream. Destroying the stream unbinds this session and calls onClose(). The session object stays, and stream() is null. The two sides call each other through the peer pointer. isOpen() is false after either side ends.

Constructor & Destructor Documentation

◆ StreamSession()

StreamSession ( Stream & stream)
explicitprotected
Exceptions
%std::logic_errorif stream already has a session.

Member Function Documentation

◆ open()

void open ( Stream & stream)
protected
Exceptions
%std::logic_errorif this session is already open, or if stream already has a session.

◆ close()

void close ( )
protected

Does nothing when no stream is bound. The stream clears its session pointer before it ends, so a close that runs from this destructor does not re-enter the stream.

◆ onCloseStream()

virtual void onCloseStream ( Stream & stream)
protectedvirtual

The stream pointer is already null.