fastcgi++
A C++ FastCGI/Web API
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Fastcgipp::Transceiver Class Reference

Handles low level communication with "the other side". More...

#include <fastcgi++/transceiver.hpp>

Classes

struct  Record
 Simple FastCGI record to queue up for transmission. More...
 

Public Member Functions

void handler ()
 General transceiver handler. More...
 
void stop ()
 Call from any thread to stop the handler() thread. More...
 
void terminate ()
 Call from any thread to terminate the handler() thread. More...
 
void start ()
 Call from any thread to start the handler() thread. More...
 
void join ()
 Block until a stop() or terminate() is called and completed. More...
 
void send (const Socket &socket, Block &&data, bool kill)
 Queue up a block of data for transmission. More...
 
 Transceiver (const std::function< void(Protocol::RequestId, Message &&)> sendMessage)
 Constructor. More...
 
 ~Transceiver ()
 
bool listen ()
 Listen to the default Fastcgi socket. More...
 
bool listen (const char *name, uint32_t permissions=0xffffffffUL, const char *owner=nullptr, const char *group=nullptr)
 Listen to a named socket. More...
 
bool listen (const char *interface, const char *service)
 Listen to a TCP port. More...
 

Private Member Functions

bool transmit ()
 Transmit all buffered data possible. More...
 
void receive (Socket &socket)
 Receive data on the specified socket. More...
 
void cleanupSocket (const Socket &socket)
 Cleanup a dead socket. More...
 

Private Attributes

std::map< Socket, Blockm_receiveBuffers
 Container associating sockets with their receive buffers. More...
 
std::deque< std::unique_ptr< Record > > m_sendBuffer
 Buffer for transmitting data More...
 
std::mutex m_sendBufferMutex
 Thread safe the send buffer. More...
 
const std::function< void(Protocol::RequestId, Message &&)> m_sendMessage
 Function to call to pass messages to requests. More...
 
SocketGroup m_sockets
 Listen for connections with this. More...
 
std::atomic_bool m_terminate
 True when handler() should be terminating. More...
 
std::atomic_bool m_stop
 True when handler() should be stopping. More...
 
std::thread m_thread
 Thread our handler is running in. More...
 

Detailed Description

Handles low level communication with "the other side".

This class handles the sending/receiving/buffering of data through the OS level sockets and also the creation/destruction of the sockets themselves.

Date
May 4, 2017
Author
Eddie Carle <eddie.nosp@m.@isa.nosp@m.tec.c.nosp@m.a>

Definition at line 58 of file transceiver.hpp.

Constructor & Destructor Documentation

Fastcgipp::Transceiver::Transceiver ( const std::function< void(Protocol::RequestId, Message &&)>  sendMessage)

Constructor.

Construct a transceiver object based on an initial file descriptor to listen on and a function to pass messages on to.

Parameters
[in]sendMessageFunction to call to pass messages to requests

Definition at line 120 of file transceiver.cpp.

References DIAG_LOG.

Fastcgipp::Transceiver::~Transceiver ( )

Definition at line 224 of file transceiver.cpp.

References DIAG_LOG, m_receiveBuffers, and terminate().

Member Function Documentation

void Fastcgipp::Transceiver::cleanupSocket ( const Socket socket)
private

Cleanup a dead socket.

Definition at line 193 of file transceiver.cpp.

References Fastcgipp::Protocol::badFcgiId, Fastcgipp::Socket::close(), m_receiveBuffers, and m_sendMessage.

Referenced by receive().

void Fastcgipp::Transceiver::handler ( )

General transceiver handler.

This function is called by Manager::handler() to both transmit data passed to it from requests and relay received data back to them as a Message. The function will return true if there is nothing at all for it to do.

Definition at line 75 of file transceiver.cpp.

References m_sockets, m_stop, m_terminate, Fastcgipp::SocketGroup::poll(), receive(), Fastcgipp::SocketGroup::size(), and transmit().

Referenced by start().

void Fastcgipp::Transceiver::join ( )

Block until a stop() or terminate() is called and completed.

Definition at line 112 of file transceiver.cpp.

References m_thread.

Referenced by Fastcgipp::Manager_base::join().

bool Fastcgipp::Transceiver::listen ( )
inline

Listen to the default Fastcgi socket.

Calling this simply adds the default socket used on FastCGI applications that are initialized from HTTP servers.

Returns
True on success. False on failure.

Definition at line 128 of file transceiver.hpp.

References Fastcgipp::SocketGroup::listen(), and m_sockets.

Referenced by Fastcgipp::Manager_base::listen().

bool Fastcgipp::Transceiver::listen ( const char *  name,
uint32_t  permissions = 0xffffffffUL,
const char *  owner = nullptr,
const char *  group = nullptr 
)
inline

Listen to a named socket.

Listen on a named socket. In the Unix world this would be a path. In the Windows world I have no idea what this would be.

Parameters
[in]nameName of socket (path in Unix world).
[in]permissionsPermissions of socket. If you do not wish to set the permissions, leave it as it's default value of 0xffffffffUL.
[in]ownerOwner (username) of socket. Leave as nullptr if you do not wish to set it.
[in]groupGroup (group name) of socket. Leave as nullptr if you do not wish to set it.
Returns
True on success. False on failure.

Definition at line 148 of file transceiver.hpp.

References Fastcgipp::SocketGroup::listen(), and m_sockets.

bool Fastcgipp::Transceiver::listen ( const char *  interface,
const char *  service 
)
inline

Listen to a TCP port.

Listen on a specific interface and TCP port.

Parameters
[in]interfaceInterface to listen on. This could be an IP address or a hostname. If you don't want to specify the interface, pass nullptr.
[in]servicePort or service to listen on. This could be a service name, or a string representation of a port number.
Returns
True on success. False on failure.

Definition at line 169 of file transceiver.hpp.

References Fastcgipp::SocketGroup::listen(), and m_sockets.

void Fastcgipp::Transceiver::receive ( Socket socket)
inlineprivate
void Fastcgipp::Transceiver::send ( const Socket socket,
Block &&  data,
bool  kill 
)

Queue up a block of data for transmission.

Parameters
[in]socketSocket to write the data out
[in]dataBlock of data to send out
[in]killTrue if the socket should be closed once everything is sent.

Definition at line 205 of file transceiver.cpp.

References m_sendBuffer, m_sendBufferMutex, m_sockets, and Fastcgipp::SocketGroup::wake().

Referenced by Fastcgipp::Manager_base::localHandler(), and Fastcgipp::Manager< RequestT >::makeRequest().

void Fastcgipp::Transceiver::start ( )

Call from any thread to start the handler() thread.

If the thread is already running this will do nothing.

Definition at line 100 of file transceiver.cpp.

References Fastcgipp::SocketGroup::accept(), handler(), m_sockets, m_stop, m_terminate, and m_thread.

Referenced by Fastcgipp::Manager_base::start().

void Fastcgipp::Transceiver::stop ( )

Call from any thread to stop the handler() thread.

Calling this thread will signal the handler() thread to cleanly stop itself. This means it keeps going until all connections are closed. No new connections are accepted.

See also
join()

Definition at line 88 of file transceiver.cpp.

References Fastcgipp::SocketGroup::accept(), m_sockets, and m_stop.

Referenced by Fastcgipp::Manager_base::stop().

void Fastcgipp::Transceiver::terminate ( )

Call from any thread to terminate the handler() thread.

Calling this thread will signal the handler() thread to immediately terminate itself. This means it doesn't wait until all connections are closed.

See also
join()

Definition at line 94 of file transceiver.cpp.

References m_sockets, m_terminate, and Fastcgipp::SocketGroup::wake().

Referenced by Fastcgipp::Manager_base::terminate(), and ~Transceiver().

bool Fastcgipp::Transceiver::transmit ( )
inlineprivate

Member Data Documentation

std::map<Socket, Block> Fastcgipp::Transceiver::m_receiveBuffers
private

Container associating sockets with their receive buffers.

Definition at line 178 of file transceiver.hpp.

Referenced by cleanupSocket(), receive(), transmit(), and ~Transceiver().

std::deque<std::unique_ptr<Record> > Fastcgipp::Transceiver::m_sendBuffer
private

Buffer for transmitting data

Definition at line 200 of file transceiver.hpp.

Referenced by send(), and transmit().

std::mutex Fastcgipp::Transceiver::m_sendBufferMutex
private

Thread safe the send buffer.

Definition at line 203 of file transceiver.hpp.

Referenced by send(), and transmit().

const std::function<void(Protocol::RequestId, Message&&)> Fastcgipp::Transceiver::m_sendMessage
private

Function to call to pass messages to requests.

Definition at line 206 of file transceiver.hpp.

Referenced by cleanupSocket(), and receive().

SocketGroup Fastcgipp::Transceiver::m_sockets
private

Listen for connections with this.

Definition at line 209 of file transceiver.hpp.

Referenced by handler(), listen(), send(), start(), stop(), and terminate().

std::atomic_bool Fastcgipp::Transceiver::m_stop
private

True when handler() should be stopping.

Definition at line 224 of file transceiver.hpp.

Referenced by handler(), start(), and stop().

std::atomic_bool Fastcgipp::Transceiver::m_terminate
private

True when handler() should be terminating.

Definition at line 221 of file transceiver.hpp.

Referenced by handler(), start(), and terminate().

std::thread Fastcgipp::Transceiver::m_thread
private

Thread our handler is running in.

Definition at line 227 of file transceiver.hpp.

Referenced by join(), and start().


The documentation for this class was generated from the following files: