Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcp::DataSocket Class Referenceabstract

Represents a buffered socket that can send and receive data using optional SSL encryption. More...

#include <tcpsocket.h>

Collaboration diagram for tcp::DataSocket:

Public Member Functions

 DataSocket (EPoll &epoll, const int domain=AF_INET, const int socket=0, const bool blocking=false, const int events=(EPOLLIN|EPOLLRDHUP))
 
size_t available ()
 Returns the number of bytes available in the inputBuffer.
 
size_t read (void *buffer, size_t size)
 Reads up to size bytes from inputBuffer into buffer. More...
 
size_t write (const void *buffer, size_t size)
 Writes the contents of buffer to the outputBuffer. More...
 
- Public Member Functions inherited from tcp::Socket
 Socket (EPoll &epoll, const int domain=AF_INET, const int socket=0, const bool blocking=false, const int events=(EPOLLIN|EPOLLRDHUP))
 Construct a blocking or non-blocking socket handle that responds to certain epoll events. More...
 
 ~Socket ()
 Closes and destroys the socket. More...
 
int socket () const
 Return the linux socket handle.
 
int domain () const
 Return the socket domain (AF_INET or AF_INET6)
 

Protected Member Functions

void readToInputBuffer ()
 Reads all available data from the socket into inputBuffer.
 
void sendOutputBuffer ()
 Writes all available data from the outputBuffer to the socket. More...
 
void canSend (bool value)
 Sets the epoll event flags. More...
 
void handleEvents (uint32_t events) override
 Called by the EPoll class when the listening socket recieves an epoll event. More...
 
void disconnect () override
 Shuts down any SSL connection gracefully. More...
 
void disconnected () override
 Called when a connection is disconnected. More...
 
virtual void dataAvailable ()=0
 Called whenever new data is appended to the inputBuffer. More...
 
virtual SSLcreateSSL (SSLContext *context)
 Factory method for returning an SSL object. More...
 
- Protected Member Functions inherited from tcp::Socket
bool setEvents (int events)
 Changes which epoll events the socket listens for. More...
 
EPollepoll ()
 Returns a reference to the epoll instance used by this socket.
 

Protected Attributes

SSLssl_ {nullptr}
 Exposes the underlying SSL record used for openSSL calls to descendant classes.
 
- Protected Attributes inherited from tcp::Socket
recursive_mutex mtx
 The mutex used to provide exclusive access to the socket.
 
SocketState state_
 Descendant classes can manipulate the socket state directly.
 

Friends

class SSL
 

Detailed Description

Represents a buffered socket that can send and receive data using optional SSL encryption.

This class provides properties and methods common to both the Client and Session classes

Definition at line 159 of file tcpsocket.h.

Member Function Documentation

◆ canSend()

void tcp::DataSocket::canSend ( bool  value)
protected

Sets the epoll event flags.

The flags will include EPOLLOUT if value==true

Definition at line 247 of file tcpsocket.cpp.

248 {
249  int events = EPOLLIN | EPOLLRDHUP;
250  if (value)
251  events |= EPOLLOUT;
252  setEvents(events);
253 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ createSSL()

SSL * tcp::DataSocket::createSSL ( SSLContext context)
protectedvirtual

Factory method for returning an SSL object.

Override to replace the SSL class used.

Definition at line 348 of file tcpsocket.cpp.

349 {
350  if (context) {
351  return new SSL(*this,*context);
352  } else {
353  return nullptr;
354  }
355 }

◆ dataAvailable()

virtual void tcp::DataSocket::dataAvailable ( )
protectedpure virtual

Called whenever new data is appended to the inputBuffer.

Clients should override the dataAvailable method to do something in response to received data

◆ disconnect()

void tcp::DataSocket::disconnect ( )
overrideprotectedvirtual

Shuts down any SSL connection gracefully.

Socket::disconnect() is called to shutdown the underlying socket

Reimplemented from tcp::Socket.

Definition at line 189 of file tcpsocket.cpp.

190 {
191  mtx.lock();
192  if (ssl_ && (state_ == SocketState::CONNECTED)) {
193  ssl_->shutdown();
194  delete ssl_;
195  ssl_ = nullptr;
196  printSSLErrors();
197  }
198  mtx.unlock();
200 }

◆ disconnected()

void tcp::DataSocket::disconnected ( )
overrideprotectedvirtual

Called when a connection is disconnected.

Frees the SSL object that is associated with the connection

Reimplemented from tcp::Socket.

Definition at line 202 of file tcpsocket.cpp.

203 {
204  mtx.lock(); // Do I need to use lock here?
205  if (ssl_) {
206  delete ssl_;
207  ssl_ = nullptr;
208  printSSLErrors();
209  }
210  mtx.unlock();
212 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleEvents()

void tcp::DataSocket::handleEvents ( uint32_t  events)
overrideprotectedvirtual

Called by the EPoll class when the listening socket recieves an epoll event.

Calls either disconnected(), readToInputBuffer() + dataAvailable() or sendOutputBuffer() depending on the events that have been set.

Implements tcp::Socket.

Definition at line 255 of file tcpsocket.cpp.

256 {
257  if (state_ == SocketState::CONNECTED) {
258  if (events & EPOLLRDHUP) {
259  disconnected();
260  } else {
261  if (events & EPOLLIN) {
262  mtx.lock();
264  dataAvailable();
265  if (outputBuffer.size() > 0U) {
267  canSend(outputBuffer.size() > 0U);
268  } else {
269  canSend(false);
270  }
271  mtx.unlock();
272  }
273  if (events & EPOLLOUT) {
274  mtx.lock();
276  canSend(outputBuffer.size() > 0U);
277  mtx.unlock();
278  }
279  }
280  }
281 }

◆ read()

size_t tcp::DataSocket::read ( void *  buffer,
size_t  size 
)

Reads up to size bytes from inputBuffer into buffer.

Returns
The number of bytes actually read

Definition at line 313 of file tcpsocket.cpp.

314 {
315  size_t result = 0;
316  if (size) {
317  mtx.lock();
318  result = max<size_t>(size,inputBuffer.size());
319  if (result > 0) {
320  for (size_t i=0;i<result;++i) {
321  ((uint8_t*)buffer)[i] = inputBuffer.at(0);
322  inputBuffer.pop_front();
323  }
324  }
325  mtx.unlock();
326  }
327  return result;
328 }

◆ sendOutputBuffer()

void tcp::DataSocket::sendOutputBuffer ( )
protected

Writes all available data from the outputBuffer to the socket.

Any data that could not be written will be retained in the outputBuffer and sent with the next call to sendOutputBuffer()

Definition at line 226 of file tcpsocket.cpp.

227 {
228  mtx.lock();
229  size_t size = outputBuffer.size();
230  if (size == 0) return;
231  uint8_t *buffer = (uint8_t*)malloc(size);
232  for (size_t i=0;i<size;i++) {
233  buffer[i] = outputBuffer.at(0);
234  outputBuffer.pop_front();
235  }
236  size_t res = write_(buffer,size);
237  canSend(res!=size);
238  if (res != size) {
239  for (size_t i=res;i<size;i++) {
240  outputBuffer.push_front(buffer[i]);
241  }
242  }
243  free(buffer);
244  mtx.unlock();
245 }
Here is the call graph for this function:

◆ write()

size_t tcp::DataSocket::write ( const void *  buffer,
size_t  size 
)

Writes the contents of buffer to the outputBuffer.

The content of the outputBuffer will be sent automatically at the next EPoll event

Definition at line 330 of file tcpsocket.cpp.

331 {
332  size_t result = 0U;
333  if (size) {
334  mtx.lock();
335  try {
336  for (size_t i=0;i<size;++i) {
337  outputBuffer.push_back(((uint8_t*)buffer)[i]);
338  ++result;
339  }
340  canSend(true);
341  } catch (const std::bad_alloc&) {
342  mtx.unlock();
343  }
344  }
345  return result;
346 }
Here is the call graph for this function:

The documentation for this class was generated from the following files:
tcp::printSSLErrors
void printSSLErrors()
This method logs openSSL errors to cerr.
Definition: tcpssl.cpp:56
tcp::Socket::disconnected
virtual void disconnected()
Called when a connection is disconnected due to a network error.
Definition: tcpsocket.cpp:173
tcp::Socket::mtx
recursive_mutex mtx
The mutex used to provide exclusive access to the socket.
Definition: tcpsocket.h:141
tcp::DataSocket::readToInputBuffer
void readToInputBuffer()
Reads all available data from the socket into inputBuffer.
Definition: tcpsocket.cpp:214
tcp::Socket::state_
SocketState state_
Descendant classes can manipulate the socket state directly.
Definition: tcpsocket.h:147
tcp::DataSocket::sendOutputBuffer
void sendOutputBuffer()
Writes all available data from the outputBuffer to the socket.
Definition: tcpsocket.cpp:226
tcp::DataSocket::disconnected
void disconnected() override
Called when a connection is disconnected.
Definition: tcpsocket.cpp:202
tcp::SSL::shutdown
void shutdown()
Closes the SSL connection gracefully.
Definition: tcpssl.cpp:641
tcp::DataSocket::ssl_
SSL * ssl_
Exposes the underlying SSL record used for openSSL calls to descendant classes.
Definition: tcpsocket.h:213
tcp::Socket::disconnect
virtual void disconnect()
Shuts down the socket gracefully.
Definition: tcpsocket.cpp:164
tcp::DataSocket::canSend
void canSend(bool value)
Sets the epoll event flags.
Definition: tcpsocket.cpp:247
tcp::Socket::setEvents
bool setEvents(int events)
Changes which epoll events the socket listens for.
Definition: tcpsocket.cpp:148
tcp::DataSocket::dataAvailable
virtual void dataAvailable()=0
Called whenever new data is appended to the inputBuffer.