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

basic stream socket class. More...

#include <stream_socket.hpp>

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

Public Types

using Ptr = std::unique_ptr<BasicStreamSocket<Protocol>>
 
using Mode = typename BasicRawSocket<Protocol>::Mode
 
using Option = typename BasicRawSocket<Protocol>::Option
 
using State = typename BasicRawSocket<Protocol>::State
 
using Endpoint = typename Protocol::Endpoint
 
using TimePoint = typename BasicRawSocket<Protocol>::TimePoint
 
- 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

 BasicStreamSocket () noexcept
 default constructor.
 
 BasicStreamSocket (Mode mode) noexcept
 create instance specifying the mode.
 
 BasicStreamSocket (int fd, const Endpoint &remote, Mode mode=Mode::NonBlocking)
 create the socket instance adopting an accepted file descriptor.
 
 BasicStreamSocket (const BasicStreamSocket &other)=delete
 copy constructor.
 
BasicStreamSocketoperator= (const BasicStreamSocket &other)=delete
 copy assignment operator.
 
 BasicStreamSocket (BasicStreamSocket &&other) noexcept
 move constructor.
 
BasicStreamSocketoperator= (BasicStreamSocket &&other) noexcept
 move assignment operator.
 
 ~BasicStreamSocket ()=default
 destroy the instance.
 
int connect (const Endpoint &endpoint) noexcept
 make a connection to the given endpoint.
 
bool waitConnected ()
 block until connected.
 
bool waitConnected (std::chrono::nanoseconds timeout)
 block until connected, giving up after the given duration.
 
bool waitConnected (TimePoint deadline)
 block until connected, giving up at the given time point.
 
int disconnect () noexcept
 shutdown the connection.
 
bool waitDisconnected ()
 wait until the connection as been shut down. return true if the connection as been shut down, false otherwise.
 
bool waitDisconnected (std::chrono::nanoseconds timeout)
 wait until the connection as been shut down, giving up after the given duration.
 
bool waitDisconnected (TimePoint deadline)
 wait until the connection as been shut down, giving up at the given time point.
 
void close () noexcept override
 close the socket handle.
 
ssize_t read (char *data, size_t maxSize) noexcept
 read data.
 
int readExactly (char *data, size_t size) noexcept
 read data until size is reached or an error occurred.
 
int readExactly (char *data, size_t size, std::chrono::nanoseconds timeout) noexcept
 read data until size is reached, an error occurred or the given duration elapsed.
 
int readExactly (char *data, size_t size, TimePoint deadline) noexcept
 read data until size is reached, an error occurred or the deadline expired.
 
int writeExactly (const char *data, size_t size) noexcept
 write data until size is reached or an error occurred.
 
int writeExactly (const char *data, size_t size, std::chrono::nanoseconds timeout) noexcept
 write data until size is reached, an error occurred or the given duration elapsed.
 
int writeExactly (const char *data, size_t size, TimePoint deadline) noexcept
 write data until size is reached, an error occurred or the deadline expired.
 
int setOption (Option option, int value) noexcept override
 set the given option to the given value.
 
const EndpointremoteEndpoint () const noexcept
 determine the remote endpoint associated with this socket.
 
bool connecting () const noexcept
 check if the socket is connecting.
 
bool connected () noexcept
 check if the socket is connected.
 
int mtu () const noexcept
 get socket mtu.
 
- 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.
 
- 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.
 
virtual int open (const Protocol &protocol=Protocol()) noexcept
 open socket using the given protocol.
 
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.
 
- 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.
 

Friends

template<class P , class E , size_t N>
class BasicAsyncStreamSocket
 friendship with basic asynchronous stream socket
 

Detailed Description

template<class Protocol>
class join::BasicStreamSocket< Protocol >

basic stream socket class.

Member Typedef Documentation

◆ Endpoint

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

◆ Mode

template<class Protocol >
using join::BasicStreamSocket< Protocol >::Mode = typename BasicRawSocket<Protocol>::Mode

◆ Option

template<class Protocol >
using join::BasicStreamSocket< Protocol >::Option = typename BasicRawSocket<Protocol>::Option

◆ Ptr

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

◆ State

template<class Protocol >
using join::BasicStreamSocket< Protocol >::State = typename BasicRawSocket<Protocol>::State

◆ TimePoint

template<class Protocol >
using join::BasicStreamSocket< Protocol >::TimePoint = typename BasicRawSocket<Protocol>::TimePoint

Constructor & Destructor Documentation

◆ BasicStreamSocket() [1/5]

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

default constructor.

◆ BasicStreamSocket() [2/5]

template<class Protocol >
join::BasicStreamSocket< Protocol >::BasicStreamSocket ( Mode mode)
inlineexplicitnoexcept

create instance specifying the mode.

Parameters
modeSet the socket blocking mode.

◆ BasicStreamSocket() [3/5]

template<class Protocol >
join::BasicStreamSocket< Protocol >::BasicStreamSocket ( int fd,
const Endpoint & remote,
Mode mode = Mode::NonBlocking )
inlineexplicit

create the socket instance adopting an accepted file descriptor.

Parameters
fdaccepted file descriptor.
remoteendpoint of the connected peer, as reported by the accept.
modeblocking mode the file descriptor was accepted with.

◆ BasicStreamSocket() [4/5]

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

copy constructor.

Parameters
otherother object to copy.

◆ BasicStreamSocket() [5/5]

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

move constructor.

Parameters
otherother object to move.

◆ ~BasicStreamSocket()

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

destroy the instance.

Member Function Documentation

◆ close()

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

close the socket handle.

Reimplemented from join::BasicSocket< Protocol >.

◆ connect()

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

make a connection to the given endpoint.

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

◆ connected()

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

check if the socket is connected.

Returns
true if connected, false otherwise.
Note
this method probes SO_ERROR and updates the internal state when a pending connection completes, it is therefore not const.

◆ connecting()

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::connecting ( ) const
inlinenoexcept

check if the socket is connecting.

Returns
true if connecting, false otherwise.

◆ disconnect()

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

shutdown the connection.

Returns
0 on success, -1 on failure.

◆ mtu()

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

get socket mtu.

Returns
mtu on success, -1 on failure.

◆ operator=() [1/2]

template<class Protocol >
BasicStreamSocket & join::BasicStreamSocket< Protocol >::operator= ( BasicStreamSocket< Protocol > && other)
inlinenoexcept

move assignment operator.

Parameters
otherother object to assign.
Returns
assigned object.

◆ operator=() [2/2]

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

copy assignment operator.

Parameters
otherother object to assign.
Returns
assigned object.

◆ read()

template<class Protocol >
ssize_t join::BasicStreamSocket< Protocol >::read ( char * data,
size_t maxSize )
inlinenoexcept

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.

◆ readExactly() [1/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::readExactly ( char * data,
size_t size )
inlinenoexcept

read data until size is reached or an error occurred.

Parameters
databuffer used to store the data received.
sizenumber of bytes to read.
Returns
0 on success, -1 on failure.

◆ readExactly() [2/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::readExactly ( char * data,
size_t size,
std::chrono::nanoseconds timeout )
inlinenoexcept

read data until size is reached, an error occurred or the given duration elapsed.

Parameters
databuffer used to store the data received.
sizenumber of bytes to read.
timeoutmaximum time granted to the whole read.
Returns
0 on success, -1 on failure.

◆ readExactly() [3/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::readExactly ( char * data,
size_t size,
TimePoint deadline )
inlinenoexcept

read data until size is reached, an error occurred or the deadline expired.

Parameters
databuffer used to store the data received.
sizenumber of bytes to read.
deadlinetime point at which to give up, max to wait indefinitely.
Returns
0 on success, -1 on failure.

◆ remoteEndpoint()

template<class Protocol >
const Endpoint & join::BasicStreamSocket< Protocol >::remoteEndpoint ( ) const
inlinenoexcept

determine the remote endpoint associated with this socket.

Returns
remote endpoint.

◆ setOption()

template<class Protocol >
int join::BasicStreamSocket< 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::BasicRawSocket< Protocol >.

◆ waitConnected() [1/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitConnected ( )
inline

block until connected.

Returns
true if connected, false otherwise.

◆ waitConnected() [2/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitConnected ( std::chrono::nanoseconds timeout)
inline

block until connected, giving up after the given duration.

Parameters
timeoutmaximum time to wait.
Returns
true if connected, false otherwise.

◆ waitConnected() [3/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitConnected ( TimePoint deadline)
inline

block until connected, giving up at the given time point.

Parameters
deadlinetime point at which to give up, max to wait indefinitely.
Returns
true if connected, false otherwise.

◆ waitDisconnected() [1/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitDisconnected ( )
inline

wait until the connection as been shut down. return true if the connection as been shut down, false otherwise.

◆ waitDisconnected() [2/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitDisconnected ( std::chrono::nanoseconds timeout)
inline

wait until the connection as been shut down, giving up after the given duration.

Parameters
timeoutmaximum time to wait. return true if the connection as been shut down, false otherwise.

◆ waitDisconnected() [3/3]

template<class Protocol >
bool join::BasicStreamSocket< Protocol >::waitDisconnected ( TimePoint deadline)
inline

wait until the connection as been shut down, giving up at the given time point.

Parameters
deadlinetime point at which to give up, max to wait indefinitely. return true if the connection as been shut down, false otherwise.

◆ writeExactly() [1/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::writeExactly ( const char * data,
size_t size )
inlinenoexcept

write data until size is reached or an error occurred.

Parameters
datadata buffer to send.
sizenumber of bytes to write.
Returns
0 on success, -1 on failure.

◆ writeExactly() [2/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::writeExactly ( const char * data,
size_t size,
std::chrono::nanoseconds timeout )
inlinenoexcept

write data until size is reached, an error occurred or the given duration elapsed.

Parameters
datadata buffer to send.
sizenumber of bytes to write.
timeoutmaximum time granted to the whole write.
Returns
0 on success, -1 on failure.

◆ writeExactly() [3/3]

template<class Protocol >
int join::BasicStreamSocket< Protocol >::writeExactly ( const char * data,
size_t size,
TimePoint deadline )
inlinenoexcept

write data until size is reached, an error occurred or the deadline expired.

Parameters
datadata buffer to send.
sizenumber of bytes to write.
deadlinetime point at which to give up, max to wait indefinitely.
Returns
0 on success, -1 on failure.

Friends And Related Symbol Documentation

◆ BasicAsyncStreamSocket

template<class Protocol >
template<class P , class E , size_t N>
friend class BasicAsyncStreamSocket
friend

friendship with basic asynchronous stream socket

Member Data Documentation

◆ _remote

template<class Protocol >
Endpoint join::BasicStreamSocket< Protocol >::_remote
mutableprotected

remote endpoint.


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