libBsdSockets
C++ Wrapper classes to the BSD Socket API
BsdSockets::Socket Class Reference

BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSockets. More...

#include <Socket.h>

Inheritance diagram for BsdSockets::Socket:

Public Types

typedef std::shared_ptr< SocketPtr
 

Public Member Functions

virtual ~Socket ()
 
SelectResult select (int timeout_ms, bool checkRead, bool checkWrite, bool checkError)
 
short poll (int timeout_ms, short eventsToLookFor)
 
ssize_t send (const void *buffer, size_t length, int flags=0) const
 
ssize_t receive (void *buffer, size_t length, int flags=0) const
 
ssize_t blockingReceive (void *buffer, size_t length) const
 
void getSocketOption (int level, int optionName, void *optionValue, socklen_t *optionLength) const
 
void setSocketOption (int level, int optionName, const void *optionValue, socklen_t optionLength) const
 
int getLowLevelSocket () const
 
std::shared_ptr< AddressgetAddress () const
 

Static Public Member Functions

static Socket::Ptr create (std::shared_ptr< Address > theAddress, int existingLowLevelSocket)
 

Protected Member Functions

 Socket (std::shared_ptr< Address > theAddress)
 

Detailed Description

BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSockets.

It provides creation (on creation), closing (on destruct), send/receive, and socket option set/get.

Note
The socket is created during the constructor and closed in the destructor. The socket may not be closed prior.

Definition at line 66 of file Socket.h.

Member Typedef Documentation

◆ Ptr

typedef std::shared_ptr<Socket> BsdSockets::Socket::Ptr

Constructor & Destructor Documentation

◆ ~Socket()

BsdSockets::Socket::~Socket ( )
virtual

Closes socket. Virtual destructor to support derived classes.

Definition at line 29 of file Socket.cpp.

◆ Socket()

BsdSockets::Socket::Socket ( std::shared_ptr< Address theAddress)
protected

Create Socket to support an address, reading parameters from address.

Parameters
theAddressto create socket to work for

Member Function Documentation

◆ blockingReceive()

ssize_t BsdSockets::Socket::blockingReceive ( void *  buffer,
size_t  length 
) const

Read up to length bytes into buffer with flags from socket, blocking until ready or error..

Parameters
bufferto write
lengthbytes to read from socket and write to buffer
Returns
bytes read. 0 is a remote closed socket.

Definition at line 114 of file Socket.cpp.

◆ create()

Socket::Ptr BsdSockets::Socket::create ( std::shared_ptr< Address theAddress,
int  existingLowLevelSocket 
)
static

Create Socket from an existing low-level socket id.

Parameters
theAddressto create socket to work for
existingLowLevelSocketlow-level socket id of an existing socket
Returns
the socket

Definition at line 38 of file Socket.cpp.

◆ getAddress()

std::shared_ptr< Address > BsdSockets::Socket::getAddress ( ) const
Returns
the address of this socket

Definition at line 135 of file Socket.cpp.

◆ getLowLevelSocket()

int BsdSockets::Socket::getLowLevelSocket ( ) const
Returns
the low-level socket number

Definition at line 139 of file Socket.cpp.

◆ getSocketOption()

void BsdSockets::Socket::getSocketOption ( int  level,
int  optionName,
void *  optionValue,
socklen_t *  optionLength 
) const

Get socket option optionName at network level, saving to buffer optionValue and to length optionLength.

Parameters
levelnetwork level of option
optionNamename of the option at level
optionValueto save value to
optionLengthto save length of optionValue to

Definition at line 123 of file Socket.cpp.

◆ poll()

short BsdSockets::Socket::poll ( int  timeout_ms,
short  eventsToLookFor 
)

poll the Socket for eventsToLookFor timeout out after timeout_ms

Parameters
timeout_mstime in ms to try to find eventsToLookFor
eventsToLookForlow-level events from poll() to look for
Returns
low-level events from poll() to look for

Definition at line 83 of file Socket.cpp.

◆ receive()

ssize_t BsdSockets::Socket::receive ( void *  buffer,
size_t  length,
int  flags = 0 
) const

Read up to length bytes into buffer with flags from socket.

Parameters
bufferto write
lengthbytes to read from socket and write to buffer
flagsto use when receiving.
Returns
bytes read
See also
send() flags parameter for details of flags

Definition at line 105 of file Socket.cpp.

◆ select()

SelectResult BsdSockets::Socket::select ( int  timeout_ms,
bool  checkRead,
bool  checkWrite,
bool  checkError 
)

Check state of socket without timeout_ms

Parameters
timeout_mstimeout in milliseconds to stop trying to check
checkReadtrue if read should be checked
checkWritetrue if write should be checked
checkErrortrue if error should be checked
Returns
SelectResult containing results of select

Definition at line 61 of file Socket.cpp.

◆ send()

ssize_t BsdSockets::Socket::send ( const void *  buffer,
size_t  length,
int  flags = 0 
) const

Send length bytes from buffer with flags to socket.

Parameters
bufferto read
lengthbytes to read from buffer and write to socket
flagsto use when sending.
See also
send() flags parameter for details of flags
Returns
bytes sent

Definition at line 96 of file Socket.cpp.

◆ setSocketOption()

void BsdSockets::Socket::setSocketOption ( int  level,
int  optionName,
const void *  optionValue,
socklen_t  optionLength 
) const

Set socket option optionName at network level, reading from buffer optionValue and from length optionLength.

Parameters
levelnetwork level of option
optionNamename of the option at level
optionValueto read value from
optionLengthlength of optionValue

Definition at line 129 of file Socket.cpp.


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