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

Class for representing an OS level socket that listens for connections. More...

#include <fastcgi++/sockets.hpp>

Public Member Functions

 SocketGroup ()
 
 ~SocketGroup ()
 
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...
 
Socket connect (const char *name)
 Connect to a named socket. More...
 
Socket connect (const char *host, const char *service)
 Connect to a host on a TCP port/serivce. More...
 
Socket poll (bool block)
 Poll socket set for new incoming connections and data. More...
 
void wake ()
 Wake up from a nap inside poll() More...
 
size_t size () const
 How many active sockets (not counting listeners) are in the group. More...
 
void accept (bool status)
 Should we accept new connections? More...
 

Private Member Functions

void createSocket (const socket_t listener)
 Accept a new connection and create it's socket. More...
 
bool pollAdd (const socket_t socket)
 Add a socket identifier to the poll list. More...
 
bool pollDel (const socket_t socket)
 Remove a socket identifier to the poll list. More...
 

Private Attributes

std::set< socket_tm_listeners
 These are the sockets we listen for connections on. More...
 
poll_t m_poll
 Our poll object. More...
 
socket_t m_wakeSockets [2]
 A pair of sockets for wakeup purposes. More...
 
bool m_waking
 Set to true while there is a pending wake. More...
 
std::atomic_bool m_accept
 Set to true if we should be accepting new connections. More...
 
std::atomic_bool m_refreshListeners
 Set to true if we should refresh the listeners in the poll. More...
 
std::mutex m_wakingMutex
 We need this mutex to thread safe the wake() function. More...
 
std::map< socket_t, Socketm_sockets
 All the sockets. More...
 
std::deque< std::string > m_filenames
 Filenames to cleanup when we're done. More...
 

Friends

class Socket
 Our sockets need access to our private data. More...
 

Detailed Description

Class for representing an OS level socket that listens for connections.

It works together with the Socket class to establish all the interfacing between the OS and the Transceiver class to communicate with the outside world. The object of this class represents the socket that listens for incoming connections to the FastCGI server. This class will create and manage an array of Socket objects as new connections are initiated.

The only part of this class that is safe to call from multiple threads is the wake() function.

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

Definition at line 287 of file sockets.hpp.

Constructor & Destructor Documentation

◆ SocketGroup()

Fastcgipp::SocketGroup::SocketGroup ( )

Definition at line 149 of file sockets.cpp.

References DIAG_LOG, m_wakeSockets, and pollAdd().

◆ ~SocketGroup()

Fastcgipp::SocketGroup::~SocketGroup ( )

Definition at line 171 of file sockets.cpp.

References DIAG_LOG.

Member Function Documentation

◆ accept()

void Fastcgipp::SocketGroup::accept ( bool  status)

Should we accept new connections?

Parameters
[in]statusSet to false if you want to start refusing new connections. True otherwise (default).

Definition at line 696 of file sockets.cpp.

◆ connect() [1/2]

Fastcgipp::Socket Fastcgipp::SocketGroup::connect ( const char *  name)

Connect to a named socket.

Connect to 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).
Returns
Socket associated with the new connection. If the connection failed, you'll get an invalid socket (it will evaluate to false).

Definition at line 363 of file sockets.cpp.

References ERROR_LOG.

◆ connect() [2/2]

Fastcgipp::Socket Fastcgipp::SocketGroup::connect ( const char *  host,
const char *  service 
)

Connect to a host on a TCP port/serivce.

Parameters
[in]hostHost to connect to. This could be an IP address address or a hostname.
[in]servicePort or service to connect to. This could be a service name, or a string representation of a port number.
Returns
Socket associated with the new connection. If the connection failed, you'll get an invalid socket (it will evaluate to false).

Definition at line 397 of file sockets.cpp.

References ERROR_LOG.

◆ createSocket()

void Fastcgipp::SocketGroup::createSocket ( const socket_t  listener)
inlineprivate

Accept a new connection and create it's socket.

Definition at line 609 of file sockets.cpp.

References ERROR_LOG, and FAIL_LOG.

◆ listen() [1/3]

bool Fastcgipp::SocketGroup::listen ( )

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 201 of file sockets.cpp.

References ERROR_LOG.

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

◆ listen() [2/3]

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

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 226 of file sockets.cpp.

References ERROR_LOG.

◆ listen() [3/3]

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

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 306 of file sockets.cpp.

References ERROR_LOG.

◆ poll()

Fastcgipp::Socket Fastcgipp::SocketGroup::poll ( bool  block)

Poll socket set for new incoming connections and data.

Calling this will initiate a poll for both new connections and new incoming data within the set of sockets that this object has created. It will also keep an eye out for dead sockets and tear them down accordingly.

Its behaviour is as follows:

  • If a new connection arrives, it will establish the new connection.
  • If a socket is dead, it destroys and marks the socket as invalid. It will not return anything regarding the dead socket.
  • If new data has arrived in a currently active connection it will return the socket.
  • If the call has been set to non-blocking and no new data awaits, a generic invalid socket is returned.

This function can be either blocking or non-blocking depending on the boolean value passed to it. If the call is blocking it can be awoken in a thread safe manner with a call to wake().

Parameters
[in]blockSet true to make the call sleep and wait for new data to arrive.
Returns
The socket for which there is new data waiting. Make sure to Socket::valid() on it to ensure validity.

Definition at line 460 of file sockets.cpp.

References ERROR_LOG, FAIL_LOG, and WARNING_LOG.

◆ pollAdd()

bool Fastcgipp::SocketGroup::pollAdd ( const socket_t  socket)
private

Add a socket identifier to the poll list.

Definition at line 651 of file sockets.cpp.

Referenced by Fastcgipp::Socket::Socket(), and SocketGroup().

◆ pollDel()

bool Fastcgipp::SocketGroup::pollDel ( const socket_t  socket)
private

Remove a socket identifier to the poll list.

Definition at line 676 of file sockets.cpp.

◆ size()

size_t Fastcgipp::SocketGroup::size ( ) const
inline

How many active sockets (not counting listeners) are in the group.

Definition at line 406 of file sockets.hpp.

References m_sockets.

◆ wake()

void Fastcgipp::SocketGroup::wake ( )

Wake up from a nap inside poll()

Calling this simply wakes up the execution thread that poll() is blocking in. If there is no nap taking place, calling this does nothing.

This function is thread safe and can be called from anywhere as often as is desired.

Definition at line 596 of file sockets.cpp.

References FAIL_LOG.

Friends And Related Function Documentation

◆ Socket

friend class Socket
friend

Our sockets need access to our private data.

Definition at line 420 of file sockets.hpp.

Member Data Documentation

◆ m_accept

std::atomic_bool Fastcgipp::SocketGroup::m_accept
private

Set to true if we should be accepting new connections.

Definition at line 435 of file sockets.hpp.

◆ m_filenames

std::deque<std::string> Fastcgipp::SocketGroup::m_filenames
private

Filenames to cleanup when we're done.

Definition at line 456 of file sockets.hpp.

◆ m_listeners

std::set<socket_t> Fastcgipp::SocketGroup::m_listeners
private

These are the sockets we listen for connections on.

Definition at line 423 of file sockets.hpp.

◆ m_poll

poll_t Fastcgipp::SocketGroup::m_poll
private

Our poll object.

Definition at line 426 of file sockets.hpp.

◆ m_refreshListeners

std::atomic_bool Fastcgipp::SocketGroup::m_refreshListeners
private

Set to true if we should refresh the listeners in the poll.

Definition at line 438 of file sockets.hpp.

◆ m_sockets

std::map<socket_t, Socket> Fastcgipp::SocketGroup::m_sockets
private

All the sockets.

Definition at line 444 of file sockets.hpp.

Referenced by size().

◆ m_wakeSockets

socket_t Fastcgipp::SocketGroup::m_wakeSockets[2]
private

A pair of sockets for wakeup purposes.

Definition at line 429 of file sockets.hpp.

Referenced by SocketGroup().

◆ m_waking

bool Fastcgipp::SocketGroup::m_waking
private

Set to true while there is a pending wake.

Definition at line 432 of file sockets.hpp.

◆ m_wakingMutex

std::mutex Fastcgipp::SocketGroup::m_wakingMutex
private

We need this mutex to thread safe the wake() function.

Definition at line 441 of file sockets.hpp.


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