Service Class Referenceabstract

#include <Pt/Http/Service.h>

Factory for request responders. More...

Inherits NonCopyable.

Inherited by BasicService< R, Alloc >, WebSocketService, HttpService, HttpService, and .

Public Member Functions

 Service ()
 Default Constructor.
virtual ~Service ()
 Destructor.
Responder * getResponder (const Request &)
 Creates a responder to handle request received by a server.
void releaseResponder (Responder *)
 Destroys a responder created by a server.

Protected Member Functions

virtual Responder * onGetResponder (const Request &)=0
 Creates a responder to handle request received by a server.
virtual void onReleaseResponder (Responder *)=0
 Destroys a responder created by a server.
virtual void onUpgrade (Stream &stream)
 Called when a 101 reply has upgraded a connection.

Detailed Description

Service is the responder factory in the server model. The server does not construct responders itself: when a servlet maps a request, the service's getResponder() calls onGetResponder(), and when the reply has been sent or the exchange has failed, releaseResponder() calls onReleaseResponder(). The service must remain alive while any responder it created is still in use.

BasicService is that factory for a single responder type, using an allocator that defaults to new and delete. Use a custom Service when the responder type depends on the request headers, or when responders are pooled.

A responder upgrades the connection by finishing the reply with status 101. The server keeps the TCP connection and calls onUpgrade() on the server thread with the Stream it owns. The responder has already been released. Binding a StreamSession to that stream accepts the upgrade. The empty base implementation leaves the stream unbound, and the server closes it. Stream::protocol() is the value of the request's Upgrade header. The stream buffer is Stream::buffer(). beginInput() and beginOutput() transfer that buffer.

The example is the usual factory: a BasicService for a responder type. The equivalent hand-written service implements onGetResponder() and onReleaseResponder() in the same way.

Basic HTTP service implementation.
Definition Service.h:133

Member Function Documentation

◆ onUpgrade()

virtual void onUpgrade ( Stream & stream)
protectedvirtual

Runs on the server thread with the Stream the server owns. The responder has already been released. Bind a StreamSession to stream to accept it. The empty implementation leaves the stream unbound, and the server closes it.

Reimplemented in WebSocketService.