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

Class for representing an OS level I/O socket. More...

#include <fastcgi++/sockets.hpp>

Classes

struct  Data
 Data structure to hold the shared socket data. More...
 

Public Member Functions

ssize_t read (char *buffer, size_t size) const
 Try and read a chunk of data out of the socket. More...
 
ssize_t write (const char *buffer, size_t size) const
 Try and write a chunk of data into the socket. More...
 
bool operator< (const Socket &x) const
 We need this to allow the socket objects to be in sorted containers. More...
 
bool operator== (const Socket &x) const
 We need this to allow the socket objects to be in sorted containers. More...
 
 Socket (const Socket &x)
 Copy constructor. More...
 
Socketoperator= (const Socket &x)
 Assignment. More...
 
 Socket (Socket &&x)
 Move constructor. More...
 
 ~Socket ()
 Calls close() on the socket if we are destructing the original. More...
 
bool valid () const
 Returns true if this socket is still open and capable of read/write. More...
 
void close () const
 Call this to close the socket. More...
 
 Socket ()
 Creates an invalid socket with no original. More...
 

Private Member Functions

 Socket (const socket_t &socket, SocketGroup &group, bool valid=true)
 Sole non-copy/move constructor. More...
 

Private Attributes

std::shared_ptr< Datam_data
 Shared pointer to hold the socket data. More...
 
bool m_original
 This is only true for a non-copy constructed object. More...
 

Friends

class SocketGroup
 Our respective SocketGroup needs private access. More...
 

Detailed Description

Class for representing an OS level I/O socket.

It works together with the SocketGroup class to establish all the interfacing between the OS and the Transceiver class to communicate with the outside world. The objects of this class represent individual connections to the FastCGI server. They are consolidated and managed within the SocketGroup class.

No non-const member functions are thread safe. This means you can only use valid() and the comparison operators across multiple threads.

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

Definition at line 83 of file sockets.hpp.

Constructor & Destructor Documentation

◆ Socket() [1/4]

Fastcgipp::Socket::Socket ( const socket_t socket,
SocketGroup group,
bool  valid = true 
)
private

Sole non-copy/move constructor.

This constructor is only accessible to the SocketGroup class to create new "original" sockets as they are accepted. Only sockets created with this constructor will have m_original set to true.

Parameters
[in,out]socketThe OS level socket identifier to associate with this.
[in,out]groupThe SocketGroup object that created and is consolidating this socket and it's peers.
[in]validSet to false if this is a dead socket. Sometimes it may be needed to create a socket that is invalid off the start.

Definition at line 53 of file sockets.cpp.

References close(), ERROR_LOG, and Fastcgipp::SocketGroup::pollAdd().

◆ Socket() [2/4]

Fastcgipp::Socket::Socket ( const Socket x)
inline

Copy constructor.

Any sockets built using the copy constructor will not be marked original.

Definition at line 219 of file sockets.hpp.

◆ Socket() [3/4]

Fastcgipp::Socket::Socket ( Socket &&  x)
inline

Move constructor.

This constructor serves the purposes of moving "original" sockets into containers. The source socket has it's originality stripped and moved to the destination.

Definition at line 241 of file sockets.hpp.

◆ ~Socket()

Fastcgipp::Socket::~Socket ( )

Calls close() on the socket if we are destructing the original.

Definition at line 138 of file sockets.cpp.

◆ Socket() [4/4]

Fastcgipp::Socket::Socket ( )

Creates an invalid socket with no original.

Definition at line 646 of file sockets.cpp.

Member Function Documentation

◆ close()

void Fastcgipp::Socket::close ( ) const

Call this to close the socket.

If the socket is valid, this will do the following:

  • Close/hangup the OS level socket.
  • Remove the socket from the associated SocketGroup polling set.
  • Fully disassociate the socket with the SocketGroup.
  • Mark the socket as invalid.

If the socket is already invalid, calling this does nothing.

Definition at line 122 of file sockets.cpp.

Referenced by Fastcgipp::Transceiver::cleanupSocket(), Socket(), and Fastcgipp::Transceiver::transmit().

◆ operator<()

bool Fastcgipp::Socket::operator< ( const Socket x) const
inline

We need this to allow the socket objects to be in sorted containers.

Definition at line 203 of file sockets.hpp.

References m_data.

◆ operator=()

Socket& Fastcgipp::Socket::operator= ( const Socket x)
inline

Assignment.

Any sockets assigned with this will not be marked original.

Definition at line 228 of file sockets.hpp.

References m_data, and m_original.

◆ operator==()

bool Fastcgipp::Socket::operator== ( const Socket x) const
inline

We need this to allow the socket objects to be in sorted containers.

Definition at line 209 of file sockets.hpp.

References m_data.

◆ read()

ssize_t Fastcgipp::Socket::read ( char *  buffer,
size_t  size 
) const

Try and read a chunk of data out of the socket.

This function will attempt to read the requested amount of data out of the socket into the buffer. The return value indicates both how many bytes were read and socket validity.

If an error occurs during the read operation the socket is closed/destroyed, marked invalid and -1 is returned. If the other end has hung up, you can continue reading until the internal buffer is empty. At that point -1 is returned and the socket is closed/destroyed and marked invalid.

Parameters
[out]bufferPointer to memory location to which data should be read into.
[in]sizeMaximum amount of data to read into the buffer. Obviously this should be less than or equal to the actual amount of memory allocated in the buffer.
Returns
Actual number of bytes read into the buffer. If -1 is returned the socket is no longer valid and all data has been received.

Definition at line 68 of file sockets.cpp.

References WARNING_LOG.

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

◆ valid()

bool Fastcgipp::Socket::valid ( ) const
inline

Returns true if this socket is still open and capable of read/write.

Definition at line 252 of file sockets.hpp.

References m_data.

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

◆ write()

ssize_t Fastcgipp::Socket::write ( const char *  buffer,
size_t  size 
) const

Try and write a chunk of data into the socket.

This function will attempt to write the requested amount of data into the socket from the buffer. The return value indicates either how many bytes were actually written or socket validity.

If an error occurs during the write operation the socket is closed/destroyed, marked invalid and -1 is returned. If the other end has hung up, -1 will be returned but this does not mean the socket has been closed/destroyed yet. There may yet be data waiting to be read(). The socket will not be automatically shut down until said data is read.

Parameters
[out]bufferPointer to memory location to which data should be written from.
[in]sizeMaximum amount of data to write from the buffer.
Returns
Actual number of bytes written from the buffer. A -1 means you can't actually write data to the socket anymore.

Definition at line 99 of file sockets.cpp.

References WARNING_LOG.

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

Friends And Related Function Documentation

◆ SocketGroup

friend class SocketGroup
friend

Our respective SocketGroup needs private access.

Definition at line 87 of file sockets.hpp.

Member Data Documentation

◆ m_data

std::shared_ptr<Data> Fastcgipp::Socket::m_data
private

Shared pointer to hold the socket data.

Definition at line 133 of file sockets.hpp.

Referenced by operator<(), operator=(), operator==(), and valid().

◆ m_original

bool Fastcgipp::Socket::m_original
private

This is only true for a non-copy constructed object.

Definition at line 136 of file sockets.hpp.

Referenced by operator=().


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