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

basic datagram socket class. More...

#include <socket.hpp>

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

Public Types

using Ptr = std::unique_ptr <BasicDatagramSocket <Protocol>>
 
using Mode = typename BasicSocket <Protocol>::Mode
 
using Option = typename BasicSocket <Protocol>::Option
 
using State = typename BasicSocket <Protocol>::State
 
using Endpoint = typename Protocol::Endpoint
 
- Public Types inherited from join::BasicSocket< Protocol >
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

 BasicDatagramSocket (int ttl=60)
 Default constructor.
 
 BasicDatagramSocket (Mode mode, int ttl=60)
 Create instance specifying the mode.
 
 BasicDatagramSocket (const BasicDatagramSocket &other)=delete
 Copy constructor.
 
BasicDatagramSocketoperator= (const BasicDatagramSocket &other)=delete
 Copy assignment operator.
 
 BasicDatagramSocket (BasicDatagramSocket &&other)
 Move constructor.
 
BasicDatagramSocketoperator= (BasicDatagramSocket &&other)
 Move assignment operator.
 
virtual ~BasicDatagramSocket ()=default
 Destroy the instance.
 
virtual int open (const Protocol &protocol=Protocol()) noexcept override
 open socket using the given protocol.
 
virtual int bindToDevice (const std::string &device) noexcept
 assigns the specified device to the socket.
 
virtual int connect (const Endpoint &endpoint)
 make a connection to the given endpoint.
 
virtual int disconnect ()
 shutdown the connection.
 
virtual void close () noexcept override
 close the socket handle.
 
virtual int read (char *data, unsigned long maxSize) noexcept override
 read data.
 
virtual int readFrom (char *data, unsigned long maxSize, Endpoint *endpoint=nullptr) noexcept
 read data on the socket.
 
virtual int write (const char *data, unsigned long maxSize) noexcept override
 write data.
 
virtual int writeTo (const char *data, unsigned long maxSize, const Endpoint &endpoint) noexcept
 write data on the socket.
 
virtual int setOption (Option option, int value) noexcept override
 set the given option to the given value.
 
const EndpointremoteEndpoint () const
 determine the remote endpoint associated with this socket.
 
virtual bool connected () noexcept
 check if the socket is connected.
 
int mtu () const
 get socket mtu.
 
int ttl () const
 returns the Time-To-Live value.
 
- Public Member Functions inherited from join::BasicSocket< Protocol >
 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 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 bool waitReadyWrite (int timeout=0) const noexcept
 block until at least one byte can be written.
 
void setMode (Mode mode) noexcept
 set the socket to the non-blocking or blocking mode.
 
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.
 

Protected Attributes

Endpoint _remote
 remote endpoint.
 
int _ttl = 60
 packet time to live.
 
- Protected Attributes inherited from join::BasicSocket< Protocol >
State _state = State::Closed
 socket state.
 
Mode _mode = Mode::NonBlocking
 socket mode.
 
int _handle = -1
 socket handle.
 
Protocol _protocol
 protocol.
 

Additional Inherited Members

- Static Public Member Functions inherited from join::BasicSocket< Protocol >
static uint16_t checksum (const uint16_t *data, size_t len, uint16_t current=0)
 get standard 1s complement checksum.
 
- Protected Member Functions inherited from join::BasicSocket< Protocol >
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.
 

Detailed Description

template<class Protocol>
class join::BasicDatagramSocket< Protocol >

basic datagram socket class.

Member Typedef Documentation

◆ Endpoint

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

◆ Mode

template<class Protocol >
using join::BasicDatagramSocket< Protocol >::Mode = typename BasicSocket <Protocol>::Mode

◆ Option

template<class Protocol >
using join::BasicDatagramSocket< Protocol >::Option = typename BasicSocket <Protocol>::Option

◆ Ptr

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

◆ State

template<class Protocol >
using join::BasicDatagramSocket< Protocol >::State = typename BasicSocket <Protocol>::State

Constructor & Destructor Documentation

◆ BasicDatagramSocket() [1/4]

template<class Protocol >
join::BasicDatagramSocket< Protocol >::BasicDatagramSocket ( int ttl = 60)
inline

Default constructor.

◆ BasicDatagramSocket() [2/4]

template<class Protocol >
join::BasicDatagramSocket< Protocol >::BasicDatagramSocket ( Mode mode,
int ttl = 60 )
inline

Create instance specifying the mode.

Parameters
modeSet the socket blocking mode.

◆ BasicDatagramSocket() [3/4]

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

Copy constructor.

Parameters
otherOther object to copy.

◆ BasicDatagramSocket() [4/4]

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

Move constructor.

Parameters
otherOther object to move.

◆ ~BasicDatagramSocket()

template<class Protocol >
virtual join::BasicDatagramSocket< Protocol >::~BasicDatagramSocket ( )
virtualdefault

Destroy the instance.

Member Function Documentation

◆ bindToDevice()

template<class Protocol >
virtual int join::BasicDatagramSocket< Protocol >::bindToDevice ( const std::string & device)
inlinevirtualnoexcept

assigns the specified device to the socket.

Parameters
devicedevice name.
Returns
0 on success, -1 on failure.

◆ close()

template<class Protocol >
virtual void join::BasicDatagramSocket< Protocol >::close ( )
inlineoverridevirtualnoexcept

close the socket handle.

Reimplemented from join::BasicSocket< Protocol >.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ connect()

template<class Protocol >
virtual int join::BasicDatagramSocket< Protocol >::connect ( const Endpoint & endpoint)
inlinevirtual

make a connection to the given endpoint.

Parameters
endpointendpoint to connect to.
Returns
0 on success, -1 on failure.

◆ connected()

template<class Protocol >
virtual bool join::BasicDatagramSocket< Protocol >::connected ( )
inlinevirtualnoexcept

check if the socket is connected.

Returns
true if connected, false otherwise.

Reimplemented in join::BasicStreamSocket< Protocol >.

◆ disconnect()

template<class Protocol >
virtual int join::BasicDatagramSocket< Protocol >::disconnect ( )
inlinevirtual

shutdown the connection.

Returns
0 on success, -1 on failure.

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

◆ mtu()

template<class Protocol >
int join::BasicDatagramSocket< Protocol >::mtu ( ) const
inline

get socket mtu.

Returns
mtu on success, -1 on failure.

◆ open()

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

open socket using the given protocol.

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

Reimplemented from join::BasicSocket< Protocol >.

◆ operator=() [1/2]

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

Move assignment operator.

Parameters
otherother object to assign.
Returns
assigned object.

◆ operator=() [2/2]

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

Copy assignment operator.

Parameters
otherother object to assign.
Returns
assigned object.

◆ read()

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

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 from join::BasicSocket< Protocol >.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ readFrom()

template<class Protocol >
virtual int join::BasicDatagramSocket< Protocol >::readFrom ( char * data,
unsigned long maxSize,
Endpoint * endpoint = nullptr )
inlinevirtualnoexcept

read data on the socket.

Parameters
databuffer used to store the data received.
maxSizemaximum number of bytes to read.
endpointendpoint from where data are coming (optional).
Returns
The number of bytes received, -1 on failure.

◆ remoteEndpoint()

template<class Protocol >
const Endpoint & join::BasicDatagramSocket< Protocol >::remoteEndpoint ( ) const
inline

determine the remote endpoint associated with this socket.

Returns
remote endpoint.

◆ setOption()

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

set the given option to the given value.

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

Reimplemented from join::BasicSocket< Protocol >.

Reimplemented in join::BasicStreamSocket< Protocol >.

◆ ttl()

template<class Protocol >
int join::BasicDatagramSocket< Protocol >::ttl ( ) const
inline

returns the Time-To-Live value.

Returns
The Time-To-Live value.

◆ write()

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

write data.

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

Reimplemented from join::BasicSocket< Protocol >.

Reimplemented in join::BasicTlsSocket< Protocol >.

◆ writeTo()

template<class Protocol >
virtual int join::BasicDatagramSocket< Protocol >::writeTo ( const char * data,
unsigned long maxSize,
const Endpoint & endpoint )
inlinevirtualnoexcept

write data on the socket.

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

Member Data Documentation

◆ _remote

template<class Protocol >
Endpoint join::BasicDatagramSocket< Protocol >::_remote
protected

remote endpoint.

◆ _ttl

template<class Protocol >
int join::BasicDatagramSocket< Protocol >::_ttl = 60
protected

packet time to live.


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