join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
join::BasicSocket< Protocol > Class Template Reference

basic socket class. More...

#include <socket.hpp>

Inheritance diagram for join::BasicSocket< Protocol >:
Collaboration diagram for join::BasicSocket< Protocol >:

Public Types

enum  Mode { Blocking , NonBlocking }
 socket modes. More...
 
enum  Option {
  NoDelay , KeepAlive , KeepIdle , KeepIntvl ,
  KeepCount , SndBuffer , RcvBuffer , TimeStamp ,
  ReuseAddr , ReusePort , Broadcast , Ttl ,
  MulticastLoop , MulticastTtl , PathMtuDiscover , RcvError ,
  AuxData
}
 socket options. More...
 
enum  State {
  Connecting , Connected , Disconnecting , Disconnected ,
  Closed
}
 socket states. More...
 
using Ptr = std::unique_ptr <BasicSocket <Protocol>>
 
using Endpoint = typename Protocol::Endpoint
 

Public Member Functions

 BasicSocket ()
 default constructor.
 
 BasicSocket (Mode mode)
 create socket instance specifying the mode.
 
 BasicSocket (const BasicSocket &other)=delete
 copy constructor.
 
BasicSocketoperator= (const BasicSocket &other)=delete
 copy assignment operator.
 
 BasicSocket (BasicSocket &&other)
 move constructor.
 
BasicSocketoperator= (BasicSocket &&other)
 move assignment operator.
 
virtual ~BasicSocket ()
 destroy the socket instance.
 
virtual int open (const Protocol &protocol=Protocol()) noexcept
 open socket using the given protocol.
 
virtual void close () noexcept
 close the socket.
 
virtual int bind (const Endpoint &endpoint) noexcept
 assigns the specified endpoint to the socket.
 
virtual int canRead () const noexcept
 get the number of readable bytes.
 
virtual bool waitReadyRead (int timeout=0) const noexcept
 block until new data is available for reading.
 
virtual int read (char *data, unsigned long maxSize) noexcept
 read data.
 
virtual bool waitReadyWrite (int timeout=0) const noexcept
 block until at least one byte can be written.
 
virtual int write (const char *data, unsigned long maxSize) noexcept
 write data.
 
void setMode (Mode mode) noexcept
 set the socket to the non-blocking or blocking mode.
 
virtual int setOption (Option option, int value) noexcept
 set the given option to the given value.
 
Endpoint localEndpoint () const
 determine the local endpoint associated with this socket.
 
bool opened () const noexcept
 check if the socket is opened.
 
virtual bool encrypted () const noexcept
 check if the socket is secure.
 
int family () const noexcept
 get socket address family.
 
int type () const noexcept
 get the protocol communication semantic.
 
int protocol () const noexcept
 get socket protocol.
 
int handle () const noexcept override
 get socket native handle.
 
- Public Member Functions inherited from join::EventHandler
 EventHandler ()=default
 create instance.
 
virtual ~EventHandler ()=default
 destroy instance.
 

Static Public Member Functions

static uint16_t checksum (const uint16_t *data, size_t len, uint16_t current=0)
 get standard 1s complement checksum.
 

Protected Member Functions

int wait (bool wantRead, bool wantWrite, int timeout) const noexcept
 wait for the socket handle to become ready.
 
- Protected Member Functions inherited from join::EventHandler
virtual void onReceive ()
 method called when data are ready to be read on handle.
 
virtual void onClose ()
 method called when handle is closed.
 
virtual void onError ()
 method called when an error occured on handle.
 

Protected Attributes

State _state = State::Closed
 socket state.
 
Mode _mode = Mode::NonBlocking
 socket mode.
 
int _handle = -1
 socket handle.
 
Protocol _protocol
 protocol.
 

Detailed Description

template<class Protocol>
class join::BasicSocket< Protocol >

basic socket class.

Member Typedef Documentation

◆ Endpoint

template<class Protocol >
using join::BasicSocket< Protocol >::Endpoint = typename Protocol::Endpoint

◆ Ptr

template<class Protocol >
using join::BasicSocket< Protocol >::Ptr = std::unique_ptr <BasicSocket <Protocol>>

Member Enumeration Documentation

◆ Mode

template<class Protocol >
enum join::BasicSocket::Mode

socket modes.

Enumerator
Blocking 

the socket will block

NonBlocking 

the socket will not block

◆ Option

template<class Protocol >
enum join::BasicSocket::Option

socket options.

Enumerator
NoDelay 

set the TCP_NODELAY option to disable/enable the nagle's algorithm.

KeepAlive 

set the SO_KEEPALIVE option.

KeepIdle 

set the keepalive idle timeout option.

KeepIntvl 

set the keepalive probe interval option.

KeepCount 

set the keepalive probe count option.

SndBuffer 

set the socket send buffer size at the OS level.

RcvBuffer 

set the socket receive buffer size at the OS level.

TimeStamp 

enable or disable the receiving of the SO_TIMESTAMP control message.

ReuseAddr 

allow reuse of local addresses.

ReusePort 

permits multiple sockets to be bound to an identical socket address.

Broadcast 

allow datagram sockets to send packets to a broadcast address.

Ttl 

set the time-to-live value of outgoing packets.

MulticastLoop 

determines whether multicast packets should be looped back to the local sockets.

MulticastTtl 

set the time-to-live value of outgoing multicast packets.

PathMtuDiscover 

set the Path MTU Discovery setting for a socket.

RcvError 

enable extended reliable error message passing.

AuxData 

enable extended metadata message passing.

◆ State

template<class Protocol >
enum join::BasicSocket::State

socket states.

Enumerator
Connecting 

socket is connecting.

Connected 

socket is connected.

Disconnecting 

socket is disconnecting.

Disconnected 

socket is disconnected.

Closed 

socket is closed.

Constructor & Destructor Documentation

◆ BasicSocket() [1/4]

template<class Protocol >
join::BasicSocket< Protocol >::BasicSocket ( )
inline

default constructor.

◆ BasicSocket() [2/4]

template<class Protocol >
join::BasicSocket< Protocol >::BasicSocket ( Mode mode)
inline

create socket instance specifying the mode.

Parameters
modeblocking mode.

◆ BasicSocket() [3/4]

template<class Protocol >
join::BasicSocket< Protocol >::BasicSocket ( const BasicSocket< Protocol > & other)
delete

copy constructor.

Parameters
otherother object to copy.

◆ BasicSocket() [4/4]

template<class Protocol >
join::BasicSocket< Protocol >::BasicSocket ( BasicSocket< Protocol > && other)
inline

move constructor.

Parameters
otherother object to move.

◆ ~BasicSocket()

template<class Protocol >
virtual join::BasicSocket< Protocol >::~BasicSocket ( )
inlinevirtual

destroy the socket instance.

Member Function Documentation

◆ bind()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::bind ( const Endpoint & endpoint)
inlinevirtualnoexcept

assigns the specified endpoint to the socket.

Parameters
endpointendpoint to assign to the socket.
Returns
0 on success, -1 on failure.

◆ canRead()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::canRead ( ) const
inlinevirtualnoexcept

get the number of readable bytes.

Returns
the number of readable bytes, -1 on failure.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ checksum()

template<class Protocol >
static uint16_t join::BasicSocket< Protocol >::checksum ( const uint16_t * data,
size_t len,
uint16_t current = 0 )
inlinestatic

get standard 1s complement checksum.

Parameters
datadata pointer.
lendata len.
currentCurrent sum.
Returns
checksum.

◆ close()

template<class Protocol >
virtual void join::BasicSocket< Protocol >::close ( )
inlinevirtualnoexcept

◆ encrypted()

template<class Protocol >
virtual bool join::BasicSocket< Protocol >::encrypted ( ) const
inlinevirtualnoexcept

check if the socket is secure.

Returns
true if encrypted, false otherwise.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ family()

template<class Protocol >
int join::BasicSocket< Protocol >::family ( ) const
inlinenoexcept

get socket address family.

Returns
socket address family.

◆ handle()

template<class Protocol >
int join::BasicSocket< Protocol >::handle ( ) const
inlineoverridevirtualnoexcept

get socket native handle.

Returns
socket native handle.

Implements join::EventHandler.

◆ localEndpoint()

template<class Protocol >
Endpoint join::BasicSocket< Protocol >::localEndpoint ( ) const
inline

determine the local endpoint associated with this socket.

Returns
local endpoint.

◆ open()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::open ( const Protocol & protocol = Protocol ())
inlinevirtualnoexcept

open socket using the given protocol.

Parameters
protocolprotocol to use.
Returns
0 on success, -1 on failure.

Reimplemented in join::BasicDatagramSocket< Protocol >.

◆ opened()

template<class Protocol >
bool join::BasicSocket< Protocol >::opened ( ) const
inlinenoexcept

check if the socket is opened.

Returns
true if opened, false otherwise.

◆ operator=() [1/2]

template<class Protocol >
BasicSocket & join::BasicSocket< Protocol >::operator= ( BasicSocket< Protocol > && other)
inline

move assignment operator.

Parameters
otherother object to assign.
Returns
current object.

◆ operator=() [2/2]

template<class Protocol >
BasicSocket & join::BasicSocket< Protocol >::operator= ( const BasicSocket< Protocol > & other)
delete

copy assignment operator.

Parameters
otherother object to assign.
Returns
current object.

◆ protocol()

template<class Protocol >
int join::BasicSocket< Protocol >::protocol ( ) const
inlinenoexcept

get socket protocol.

Returns
socket protocol.

◆ read()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::read ( char * data,
unsigned long maxSize )
inlinevirtualnoexcept

read data.

Parameters
databuffer used to store the data received.
maxSizemaximum number of bytes to read.
Returns
the number of bytes received, -1 on failure.

Reimplemented in join::BasicDatagramSocket< Protocol >, and join::BasicTlsSocket< Protocol >.

◆ setMode()

template<class Protocol >
void join::BasicSocket< Protocol >::setMode ( Mode mode)
inlinenoexcept

set the socket to the non-blocking or blocking mode.

Parameters
modeblocking mode.

◆ setOption()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::setOption ( Option option,
int value )
inlinevirtualnoexcept

set the given option to the given value.

Parameters
optionsocket option.
valueoption value.
Returns
0 on success, -1 on failure.

Reimplemented in join::BasicDatagramSocket< Protocol >, and join::BasicStreamSocket< Protocol >.

◆ type()

template<class Protocol >
int join::BasicSocket< Protocol >::type ( ) const
inlinenoexcept

get the protocol communication semantic.

Returns
the protocol communication semantic.

◆ wait()

template<class Protocol >
int join::BasicSocket< Protocol >::wait ( bool wantRead,
bool wantWrite,
int timeout ) const
inlineprotectednoexcept

wait for the socket handle to become ready.

Parameters
wantReadset to true if want read
wantWriteset to true if want write.
timeouttimeout in milliseconds.
Returns
0 on success, -1 on failure.

◆ waitReadyRead()

template<class Protocol >
virtual bool join::BasicSocket< Protocol >::waitReadyRead ( int timeout = 0) const
inlinevirtualnoexcept

block until new data is available for reading.

Parameters
timeouttimeout in milliseconds.
Returns
true if there is new data available for reading, false otherwise.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ waitReadyWrite()

template<class Protocol >
virtual bool join::BasicSocket< Protocol >::waitReadyWrite ( int timeout = 0) const
inlinevirtualnoexcept

block until at least one byte can be written.

Parameters
timeouttimeout in milliseconds.
Returns
true if data can be written, false otherwise.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ write()

template<class Protocol >
virtual int join::BasicSocket< Protocol >::write ( const char * data,
unsigned long maxSize )
inlinevirtualnoexcept

write data.

Parameters
datadata buffer to send.
maxSizemaximum number of bytes to write.
Returns
the number of bytes written, -1 on failure.

Reimplemented in join::BasicDatagramSocket< Protocol >, and join::BasicTlsSocket< Protocol >.

Member Data Documentation

◆ _handle

template<class Protocol >
int join::BasicSocket< Protocol >::_handle = -1
protected

socket handle.

◆ _mode

template<class Protocol >
Mode join::BasicSocket< Protocol >::_mode = Mode::NonBlocking
protected

socket mode.

◆ _protocol

template<class Protocol >
Protocol join::BasicSocket< Protocol >::_protocol
protected

protocol.

◆ _state

template<class Protocol >
State join::BasicSocket< Protocol >::_state = State::Closed
protected

socket state.


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