core::Socket Class Reference

`#include <Socket.h>`

Inheritance diagram for core::Socket:
[legend]
Collaboration diagram for core::Socket:
[legend]

## Public Member Functions

| Socket (EPoll &ePoll, std::string text="")
| ~Socket ()
void | shutdown (std::string text="unknown")
void | setDescriptor (int descriptor)
| Set the descriptor for the socket. More...
int | getDescriptor ()
| Get the descriptor for the socket. More...
bool | eventReceived (struct epoll_event event)
| Parse epoll event and call specified callbacks. More...
int | write (std::string data)
void | write (char *buffer, int length)
void | output (std::stringstream &out)
virtual void | onRegister ()
| Called before the socket has registered with the epoll processing. More...
virtual void | onRegistered ()
| Called after the socket has been registered with epoll processing. More...
virtual void | onUnregister ()
virtual void | onUnregistered ()
| Called when the socket has finished unregistering for the epoll processing. More...
bool | needsToWrite ()

## Public Attributes

bool | reset = false
Public Attributes inherited from core::Object
std::string | name
std::string | tag

## Protected Member Functions

void | setBufferSize (int length)
int | getBufferSize ()
virtual void | onDataReceived (std::string data)
| Called when data is received from the socket. More...
virtual void | onDataReceived (char *buffer, int len)
virtual void | receiveData (char *buffer, int bufferLength)

## Protected Attributes

EPoll & | ePoll
bool | shutDown = false

## Detailed Description

Socket

The core component to managing a socket.

Hooks into the EPoll through the registration and unregistration process and provides a communication socket of the specified protocol type. This object provides for all receiving data threading through use of the EPoll object and also provides buffering for output data requests to the socket.

A program using a socket object can request to open a socket (network or device) and communicate through the streambuffer interface of the socket object.

The socket side of the Socket accepts EPOLLIN event and will maintain the data in a buffer for the stream readers to read. A onDataReceived event is then sent with the data received in the buffer that can be read through the stream. Only sockets that send events to epoll can be used with this object.

When writing to the stream the data is written into a buffer and a EPOLLOUT is scheduled. Upon receiving the EPOLLOUT event then the buffer is written to the socket output.

## Constructor & Destructor Documentation

## Socket()

core::Socket::Socket | ( | EPoll & | ePoll,
|  | std::string | text = `""`
| ) |  |

Constructor

Parameters

  ePoll | The EPoll socket descriptor.
  text | A title for this socket.

## ~Socket()

core::Socket::~Socket | ( |  | ) |

Destructor

## Member Function Documentation

## eventReceived()

bool core::Socket::eventReceived | ( | struct epoll_event | event | ) |

Parse epoll event and call specified callbacks.

The event received from epoll is sent through the eventReceived method which will parse the event and call the read and write callbacks on the socket.

This method is called by the BMAEPoll object and should not be called from any user extended classes unless an epoll event is being simulated.

## getBufferSize()

int core::Socket::getBufferSize | ( |  | ) |

protected

## getDescriptor()

int core::Socket::getDescriptor | ( |  | ) |

Get the descriptor for the socket.

## needsToWrite()

bool core::Socket::needsToWrite | ( |  | ) |

## onDataReceived() [1/2]

void core::Socket::onDataReceived | ( | char * | buffer,
|  | int | len
| ) |  |

protectedvirtual

Reimplemented in core::TCPSession, and core::INotify.

## onDataReceived() [2/2]

void core::Socket::onDataReceived | ( | std::string | data | ) |

protectedvirtual

Called when data is received from the socket.

The onConnected method is called when the socket is ready to communicate. Writing to the socket can begin on this call to initiate a contact with the remote device. The onDataReceived method is called when the socket has received an event from epoll and there is data ready to be read from the socket. The default handler will pull the data and put it into the streambuf for the socket. EPOLLIN

Parameters

  data | the data that has been received from the socket.

Reimplemented in core::TCPServer, and core::UDPServerSocket.

## onRegister()

void core::Socket::onRegister | ( |  | ) |

virtual

Called before the socket has registered with the epoll processing.

The onRegister method is called before the socket is registered with ePoll so objects extending the Socket definition can initialize the socket before receiving events. Evoked when the descriptor is set using setDescriptor for the socket.

Reimplemented in core::TLSSession.

## onRegistered()

void core::Socket::onRegistered | ( |  | ) |

virtual

Called after the socket has been registered with epoll processing.

Reimplemented in core::TCPSession, and core::TLSSession.

## onUnregister()

void core::Socket::onUnregister | ( |  | ) |

virtual

## onUnregistered()

void core::Socket::onUnregistered | ( |  | ) |

virtual

Called when the socket has finished unregistering for the epoll processing.

The onUnregistered method is called whenever the socket is unregistered with ePoll and socket communcation events will be stopped. The default method will close the socket and clean up the connection. If this is overridden by an extended object then the object should call this method to clean the socket up.

## output()

void core::Socket::output | ( | std::stringstream & | out | ) |

## receiveData()

void core::Socket::receiveData | ( | char * | buffer,
|  | int | bufferLength
| ) |  |

protectedvirtual

receiveData will read the data from the socket and place it in the socket buffer. TLS layer overrides this to be able to read from SSL.

Reimplemented in core::TLSSession.

## setBufferSize()

void core::Socket::setBufferSize | ( | int | length | ) |

protected

## setDescriptor()

void core::Socket::setDescriptor | ( | int | descriptor | ) |

Set the descriptor for the socket.

setDescriptor establishes the file descriptor for the socket and registers the socket on the EPoll controller. setDescriptor will invoke the onRegister() event.

## shutdown()

void core::Socket::shutdown | ( | std::string | text = `"unknown"` | ) |

Use the shutdown() method to terminate the socket connection and remove resources. This method is provided to ensure that all destructors are called for all inherited objects without a virtual destructor.

## write() [1/2]

void core::Socket::write | ( | char * | buffer,
|  | int | length
| ) |  |

## write() [2/2]

int core::Socket::write | ( | std::string | data | ) |

Write data to the socket.

## Member Data Documentation

## ePoll

EPoll& core::Socket::ePoll

protected

## reset

bool core::Socket::reset = false

## shutDown

bool core::Socket::shutDown = false

protected

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

- Socket.h
- Socket.cpp