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

Encapsulates a socket handle that is capable of recieving epoll events. More...

#include <tcpsocket.h>

Public Member Functions

 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)
 
virtual void disconnect ()
 Shuts down the socket gracefully.
 

Protected Member Functions

bool setEvents (int events)
 Changes which epoll events the socket listens for. More...
 
virtual void handleEvents (uint32_t events)=0
 Called when the socket recieves an epoll event. More...
 
virtual void disconnected ()
 Called when a connection is disconnected due to a network error. More...
 
EPollepoll ()
 Returns a reference to the epoll instance used by this socket.
 

Protected Attributes

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 EPoll
 

Detailed Description

Encapsulates a socket handle that is capable of recieving epoll events.

Definition at line 96 of file tcpsocket.h.

Constructor & Destructor Documentation

◆ Socket()

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.

Parameters
domainEither AF_INET or AF_INET6
socketThe socket handle to encapsulate. If 0 is provided, a socket handle will be automatically created.
blockingIf true, a blocking socket will be created. If false, a non-blocking socket will be created.
eventsA bit flag of the epoll events to register interest include
Remarks
A client or server listener will typically call the constructor with socket=0 to start with a new socket.
A server session will create a Socket by providing the socket handle returned from an accept command.

Definition at line 97 of file tcpsocket.cpp.

97  : epoll_(epoll), events_(events), domain_(domain), socket_(socket)
98 {
99  if ((domain != AF_INET) && (domain != AF_INET6)) {
100  error("Socket","Only IPv4 and IPv6 are supported.");
101  return;
102  }
103  if (socket < 0) {
104  error("Socket","Socket parameter is < 0");
105  return;
106  }
107  mtx.lock();
108  if (socket == 0) {
109  socket_ = ::socket(domain,SOCK_STREAM,0);
110  if (socket_ == -1) {
111  error("socket", strerror(errno));
112  }
113  }
114  int flags = fcntl(socket_,F_GETFL,0);
115  if (flags == -1) {
116  error("fcntl (get)",strerror(errno));
117  } else {
118  if (!blocking) {
119  flags |= O_NONBLOCK;
120  } else {
121  flags = flags & ~O_NONBLOCK;
122  }
123  if (fcntl(socket_,F_SETFL,flags) == -1) {
124  error("fcntl (set)",strerror(errno));
125  }
126  }
127 
128  if (!epoll_.add(*this,events)) {
129  error("Unable to add socket to epoll");
130  }
131 
132  mtx.unlock();
133 }
Here is the call graph for this function:

◆ ~Socket()

tcp::Socket::~Socket ( )

Closes and destroys the socket.

Remarks
An active socket should first be shut down using the disconnect() command

Definition at line 135 of file tcpsocket.cpp.

136 {
137  if (socket_ > 0) {
138  mtx.lock();
139  epoll_.remove(*this);
140  if (::close(socket_) == -1) {
141  error("close",strerror(errno));
142  }
143  socket_ = 0;
144  mtx.unlock();
145  }
146 }
Here is the call graph for this function:

Member Function Documentation

◆ disconnected()

void tcp::Socket::disconnected ( )
protectedvirtual

Called when a connection is disconnected due to a network error.

Sets the socket state to DISCONNECTED and frees its resources. Override disconnected to perform additional cleanup of a dropped socket connection.

Reimplemented in tcp::DataSocket, and tcp::Session.

Definition at line 173 of file tcpsocket.cpp.

174 {
175  mtx.lock();
176  if (state_ != SocketState::DISCONNECTED) {
177  ::close(socket_);
178  socket_ = 0;
179  state_ = SocketState::DISCONNECTED;
180  log("Disconnected");
181  } else {
182  warning("Already disconnected");
183  }
184  mtx.unlock();
185 }
Here is the caller graph for this function:

◆ handleEvents()

virtual void tcp::Socket::handleEvents ( uint32_t  events)
protectedpure virtual

Called when the socket recieves an epoll event.

Descendant classes override this abstract method to respond to epoll events

Parameters
eventsA bitmask of event flags. See the epoll documentation

Implemented in tcp::DataSocket, tcp::Client, and tcp::Server.

◆ setEvents()

bool tcp::Socket::setEvents ( int  events)
protected

Changes which epoll events the socket listens for.

Descendant classes may want to override this

Parameters
eventsA bitmask of event flags. See the epoll documentation

Definition at line 148 of file tcpsocket.cpp.

149 {
150  mtx.lock();
151  bool result = false;
152  if (events != events_) {
153  if (epoll_.update(*this,events)) {
154  events_ = events;
155  result = true;
156  }
157  } else {
158  result = true;
159  }
160  mtx.unlock();
161  return result;
162 }
Here is the caller graph for this function:

The documentation for this class was generated from the following files:
tcp::Socket::epoll
EPoll & epoll()
Returns a reference to the epoll instance used by this socket.
Definition: tcpsocket.h:144
tcp::error
void error(string msg)
Send an error message to the log stream.
Definition: tcpsocket.cpp:18
tcp::Socket::mtx
recursive_mutex mtx
The mutex used to provide exclusive access to the socket.
Definition: tcpsocket.h:141
tcp::warning
void warning(string msg)
Send a warning message to the log stream.
Definition: tcpsocket.cpp:20
tcp::Socket::state_
SocketState state_
Descendant classes can manipulate the socket state directly.
Definition: tcpsocket.h:147
tcp::Socket::socket
int socket() const
Return the linux socket handle.
Definition: tcpsocket.h:115
tcp::Socket::domain
int domain() const
Return the socket domain (AF_INET or AF_INET6)
Definition: tcpsocket.h:118
tcp::log
void log(string msg)
Send an log message to the log stream.
Definition: tcpsocket.cpp:22