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

basic datagram socket class. More...

#include <datagram_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 BasicRawSocket<Protocol>::Mode
 
using Option = typename BasicRawSocket<Protocol>::Option
 
using State = typename BasicRawSocket<Protocol>::State
 
using Endpoint = typename Protocol::Endpoint
 
- Public Types inherited from join::BasicRawSocket< Protocol >
enum  Option {
  NoDelay , KeepAlive , KeepIdle , KeepIntvl ,
  KeepCount , SndBuffer , RcvBuffer , TimeStamp ,
  ReuseAddr , ReusePort , Broadcast , Ttl ,
  MulticastLoop , MulticastTtl , PathMtuDiscover , RcvError ,
  AuxData
}
 socket options. More...
 
using Ptr = std::unique_ptr<BasicRawSocket<Protocol>>
 
using Mode = typename BasicSocket<Protocol>::Mode
 
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  State {
  Connecting , Connected , Disconnecting , Disconnected ,
  Closed
}
 socket states. More...
 
using Ptr = std::unique_ptr<BasicSocket<Protocol>>
 
using Endpoint = typename Protocol::Endpoint
 
using TimePoint = std::chrono::steady_clock::time_point
 

Public Member Functions

 BasicDatagramSocket () noexcept
 Default constructor.
 
 BasicDatagramSocket (int ttl) noexcept
 Create instance specifying the time to live.
 
 BasicDatagramSocket (Mode mode, int ttl=60) noexcept
 Create instance specifying the mode.
 
 BasicDatagramSocket (const BasicDatagramSocket &other)=delete
 Copy constructor.
 
BasicDatagramSocketoperator= (const BasicDatagramSocket &other)=delete
 Copy assignment operator.
 
 BasicDatagramSocket (BasicDatagramSocket &&other) noexcept
 Move constructor.
 
BasicDatagramSocketoperator= (BasicDatagramSocket &&other) noexcept
 Move assignment operator.
 
 ~BasicDatagramSocket ()=default
 Destroy the instance.
 
int open (const Protocol &protocol=Protocol()) noexcept override
 open socket using the given protocol.
 
int connect (const Endpoint &endpoint) noexcept
 assign the default remote endpoint for this socket.
 
int disconnect () noexcept
 remove the default remote endpoint.
 
void close () noexcept override
 close the socket handle.
 
ssize_t readFrom (char *data, size_t maxSize, Endpoint *endpoint=nullptr) noexcept
 read data on the socket.
 
ssize_t writeTo (const char *data, size_t maxSize, const Endpoint &endpoint) noexcept
 write data on the socket.
 
const EndpointremoteEndpoint () const noexcept
 determine the remote endpoint associated with this socket.
 
bool connected () const noexcept
 check if the socket is connected.
 
int mtu () const noexcept
 get socket mtu.
 
int ttl () const noexcept
 returns the Time-To-Live value.
 
- Public Member Functions inherited from join::BasicRawSocket< Protocol >
 BasicRawSocket () noexcept
 default constructor.
 
 BasicRawSocket (Mode mode) noexcept
 create socket instance specifying the mode.
 
 BasicRawSocket (const BasicRawSocket &other)=delete
 copy constructor.
 
BasicRawSocketoperator= (const BasicRawSocket &other)=delete
 copy assignment operator.
 
 BasicRawSocket (BasicRawSocket &&other) noexcept=default
 move constructor.
 
BasicRawSocketoperator= (BasicRawSocket &&other) noexcept=default
 move assignment operator.
 
virtual ~BasicRawSocket ()=default
 destroy the socket instance.
 
int bind (const Endpoint &endpoint) noexcept override
 assigns the specified endpoint to the socket.
 
int bindToDevice (const std::string &device) noexcept
 assigns the specified device to the socket.
 
ssize_t canRead () const noexcept
 get the number of readable bytes.
 
ssize_t read (char *data, size_t maxSize) noexcept
 read data.
 
ssize_t write (const char *data, size_t maxSize) noexcept
 write data.
 
virtual int setOption (Option option, int value) noexcept
 set the given option to the given value.
 
- Public Member Functions inherited from join::BasicSocket< Protocol >
 BasicSocket () noexcept
 default constructor.
 
 BasicSocket (Mode mode) noexcept
 create socket instance specifying the mode.
 
 BasicSocket (const BasicSocket &other)=delete
 copy constructor.
 
BasicSocketoperator= (const BasicSocket &other)=delete
 copy assignment operator.
 
 BasicSocket (BasicSocket &&other) noexcept
 move constructor.
 
BasicSocketoperator= (BasicSocket &&other) noexcept
 move assignment operator.
 
virtual ~BasicSocket ()
 destroy the socket instance.
 
bool waitReadyRead () const noexcept
 block until new data is available for reading.
 
bool waitReadyRead (std::chrono::nanoseconds timeout) const noexcept
 block until new data is available for reading, giving up after the given duration.
 
bool waitReadyRead (TimePoint deadline) const noexcept
 block until new data is available for reading, giving up at the given time point.
 
bool waitReadyWrite () const noexcept
 block until at least one byte can be written.
 
bool waitReadyWrite (std::chrono::nanoseconds timeout) const noexcept
 block until at least one byte can be written, giving up after the given duration.
 
bool waitReadyWrite (TimePoint deadline) const noexcept
 block until at least one byte can be written, giving up at the given time point.
 
void setMode (Mode mode) noexcept
 set the socket to the non-blocking or blocking mode.
 
Endpoint localEndpoint () const noexcept
 determine the local endpoint associated with this socket.
 
bool opened () const noexcept
 check if the socket is opened.
 
int family () const noexcept
 get socket address family.
 
int type () const noexcept
 get the protocol communication semantic.
 
int protocol () const noexcept
 get socket protocol.
 
Mode mode () const noexcept
 get the blocking mode of the socket.
 
int handle () const noexcept
 get socket native handle.
 
int wait (bool wantRead, bool wantWrite) const noexcept
 wait for the socket handle to become ready.
 
int waitFor (bool wantRead, bool wantWrite, std::chrono::nanoseconds timeout) const noexcept
 wait for the socket handle to become ready, giving up after the given duration.
 
int waitUntil (bool wantRead, bool wantWrite, TimePoint deadline) const noexcept
 wait for the socket handle to become ready, giving up at the given time point.
 

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.
 

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 BasicRawSocket<Protocol>::Mode

◆ Option

template<class Protocol >
using join::BasicDatagramSocket< Protocol >::Option = typename BasicRawSocket<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 BasicRawSocket<Protocol>::State

Constructor & Destructor Documentation

◆ BasicDatagramSocket() [1/5]

template<class Protocol >
join::BasicDatagramSocket< Protocol >::BasicDatagramSocket ( )
inlinenoexcept

Default constructor.

◆ BasicDatagramSocket() [2/5]

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

Create instance specifying the time to live.

Parameters
ttlpacket time to live.

◆ BasicDatagramSocket() [3/5]

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

Create instance specifying the mode.

Parameters
modeSet the socket blocking mode.
ttlpacket time to live.

◆ BasicDatagramSocket() [4/5]

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

Copy constructor.

Parameters
otherOther object to copy.

◆ BasicDatagramSocket() [5/5]

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

Move constructor.

Parameters
otherOther object to move.

◆ ~BasicDatagramSocket()

template<class Protocol >
join::BasicDatagramSocket< Protocol >::~BasicDatagramSocket ( )
default

Destroy the instance.

Member Function Documentation

◆ close()

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

close the socket handle.

Reimplemented from join::BasicSocket< Protocol >.

◆ connect()

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

assign the default remote endpoint for this socket.

Parameters
endpointendpoint to assign.
Returns
0 on success, -1 on failure.
Note
for a datagram socket connect only sets the default peer, it does not perform any handshake, so there is no connecting state.

◆ connected()

template<class Protocol >
bool join::BasicDatagramSocket< Protocol >::connected ( ) const
inlinenoexcept

check if the socket is connected.

Returns
true if connected, false otherwise.

◆ disconnect()

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

remove the default remote endpoint.

Returns
0 on success, -1 on failure.

◆ mtu()

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

get socket mtu.

Returns
mtu on success, -1 on failure.

◆ open()

template<class Protocol >
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)
inlinenoexcept

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.

◆ readFrom()

template<class Protocol >
ssize_t join::BasicDatagramSocket< Protocol >::readFrom ( char * data,
size_t maxSize,
Endpoint * endpoint = nullptr )
inlinenoexcept

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
inlinenoexcept

determine the remote endpoint associated with this socket.

Returns
remote endpoint.

◆ ttl()

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

returns the Time-To-Live value.

Returns
The Time-To-Live value.

◆ writeTo()

template<class Protocol >
ssize_t join::BasicDatagramSocket< Protocol >::writeTo ( const char * data,
size_t maxSize,
const Endpoint & endpoint )
inlinenoexcept

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: