Thrill  0.1
Connection Class Referencefinal

Detailed Description

Connection is a rich point-to-point socket connection to another client (worker, master, or whatever).

Messages are fixed-length integral items or opaque byte strings with a length.

If any function fails to send or receive, then a NetException is thrown instead of explicit error handling. If ever an error occurs, we probably have to rebuild the whole network explicitly.

Definition at line 54 of file connection.hpp.

+ Inheritance diagram for Connection:
+ Collaboration diagram for Connection:

#include <connection.hpp>

Public Member Functions

 Connection ()=default
 default construction, contains invalid socket More...
 
 Connection (Socket &&s)
 Construct Connection from a Socket. More...
 
 Connection (Socket &&s, size_t group_id, size_t peer_id)
 
 Connection (Connection &&other)
 move-constructor More...
 
 ~Connection ()
 
void Close ()
 Close this Connection. More...
 
int GetError () const
 Return the associated socket error. More...
 
std::string GetPeerAddress () const
 Return the socket peer address. More...
 
SocketGetSocket ()
 Return the raw socket object for more low-level network programming. More...
 
const SocketGetSocket () const
 Return the raw socket object for more low-level network programming. More...
 
size_t group_id () const
 Gets the id of the net group this connection is associated with. More...
 
bool IsValid () const final
 Check whether the contained file descriptor is valid. More...
 
Connectionoperator= (Connection &&other)
 move assignment-operator More...
 
bool operator== (const Connection &c) const noexcept
 Checks wether two connections have the same underlying socket or not. More...
 
std::ostream & OutputOstream (std::ostream &os) const final
 make ostreamable More...
 
size_t peer_id () const
 Gets the id of the worker this connection is connected to. More...
 
ssize_t RecvOne (void *out_data, size_t size) final
 
ssize_t SendOne (const void *data, size_t size, Flags flags) final
 
void set_group_id (size_t groupId)
 Sets the group id of this connection. More...
 
void set_peer_id (size_t peerId)
 Sets the id of the worker this connection is connected to. More...
 
void set_state (ConnectionState state)
 Sets the state of this connection. More...
 
void SetNonBlocking (bool non_blocking)
 Set socket to non-blocking. More...
 
ConnectionState state () const
 Gets the state of this connection. More...
 
void SyncRecv (void *out_data, size_t size) final
 
void SyncRecvSend (const void *send_data, size_t send_size, void *recv_data, size_t recv_size) final
 
void SyncSend (const void *data, size_t size, Flags flags) final
 
void SyncSendRecv (const void *send_data, size_t send_size, void *recv_data, size_t recv_size) final
 
std::string ToString () const final
 return a string representation of this connection, for user output. More...
 
- Public Member Functions inherited from Connection
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type Send (const T &value)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type Send (const T &value)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type Send (const T &value)
 
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type Receive (T *out_value)
 Receive any serializable POD item T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type Receive (T *out_value)
 Receive any serializable non-POD fixed-length item T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type Receive (T *out_value)
 Receive any serializable non-POD fixed-length item T. More...
 
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type SendReceive (const T *value, T *out_value, size_t n=1)
 SendReceive any serializable POD item T. More...
 
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type ReceiveSend (const T &value, T *out_value)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type SendReceive (const T *value, T *out_value, size_t n=1)
 SendReceive any serializable non-POD fixed-length item T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type ReceiveSend (const T &value, T *out_value)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type SendReceive (const T *value, T *out_value, size_t n=1)
 SendReceive any serializable non-POD fixed-length item T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type ReceiveSend (const T &value, T *out_value)
 
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type SendN (const T *value, size_t n)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type SendN (const T *value, size_t n)
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type SendN (const T *value, size_t n)
 
template<typename T >
std::enable_if< std::is_pod< T >::value, void >::type ReceiveN (T *out_value, size_t n)
 Receive an array of serializable POD items T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type ReceiveN (T *out_value, size_t n)
 Receive an array of serializable non-POD fixed-length items T. More...
 
template<typename T >
std::enable_if< !std::is_pod< T >::value &&!data::Serialization< BufferBuilder, T >::is_fixed_size, void >::type ReceiveN (T *out_value, size_t n)
 Receive an array of serializable non-POD fixed-length items T. More...
 

Private Attributes

size_t group_id_ = size_t(-1)
 The id of the group this connection is associated with. More...
 
size_t peer_id_ = size_t(-1)
 The id of the worker this connection is connected to. More...
 
Socket socket_
 Underlying socket or connection handle. More...
 
ConnectionState state_ = ConnectionState::Invalid
 

Static Private Attributes

static constexpr bool debug = false
 

Additional Inherited Members

- Public Types inherited from Connection
enum  Flags : size_t { NoFlags = 0, MsgMore = 1 }
 Additional flags for sending or receiving. More...
 
- Public Attributes inherited from Connection
bool is_loopback_ = false
 
std::atomic< uint32_t > tx_seq_ { 0 }
 send sequence More...
 
std::atomic< uint32_t > rx_seq_ { 0 }
 receive sequence More...
 
std::atomic< size_t > tx_bytes_ { 0 }
 sent bytes More...
 
std::atomic< size_t > rx_bytes_ = { 0 }
 received bytes More...
 
size_t prev_tx_bytes_ = 0
 previous read of sent bytes More...
 
size_t prev_rx_bytes_ = 0
 previous read of received bytes More...
 
std::atomic< size_t > tx_active_ { 0 }
 active send requests More...
 
std::atomic< size_t > rx_active_ = { 0 }
 active recv requests More...
 
- Static Public Attributes inherited from Connection
static constexpr bool self_verify_ = common::g_self_verify
 

Constructor & Destructor Documentation

◆ Connection() [1/4]

Connection ( )
default

default construction, contains invalid socket

◆ Connection() [2/4]

Connection ( Socket &&  s)
inlineexplicit

Construct Connection from a Socket.

Definition at line 63 of file connection.hpp.

◆ Connection() [3/4]

Connection ( Socket &&  s,
size_t  group_id,
size_t  peer_id 
)
inline

Construct Connection from a Socket, with immediate initialization. (Currently used by tests).

Definition at line 69 of file connection.hpp.

◆ Connection() [4/4]

Connection ( Connection &&  other)
inline

move-constructor

Definition at line 75 of file connection.hpp.

References thrill::net::tcp::Invalid.

◆ ~Connection()

~Connection ( )
inline

Destruction of Connection should be explicitly done by a NetGroup or other network class.

Definition at line 157 of file connection.hpp.

References Connection::Close(), and Connection::IsValid().

Member Function Documentation

◆ Close()

void Close ( )
inline

Close this Connection.

Definition at line 213 of file connection.hpp.

References Socket::close(), and Connection::socket_.

Referenced by Connection::operator=(), and Connection::~Connection().

◆ GetError()

int GetError ( ) const
inline

Return the associated socket error.

Definition at line 138 of file connection.hpp.

References Socket::GetError(), and Connection::socket_.

◆ GetPeerAddress()

std::string GetPeerAddress ( ) const
inline

Return the socket peer address.

Definition at line 148 of file connection.hpp.

References Socket::GetPeerAddress(), Connection::socket_, and SocketAddress::ToStringHostPort().

Referenced by Connection::OutputOstream().

◆ GetSocket() [1/2]

Socket& GetSocket ( )
inline

◆ GetSocket() [2/2]

const Socket& GetSocket ( ) const
inline

Return the raw socket object for more low-level network programming.

Definition at line 134 of file connection.hpp.

References Connection::socket_.

◆ group_id()

size_t group_id ( ) const
inline

Gets the id of the net group this connection is associated with.

Definition at line 103 of file connection.hpp.

References Connection::group_id_.

◆ IsValid()

bool IsValid ( ) const
inlinefinalvirtual

Check whether the contained file descriptor is valid.

Implements Connection.

Definition at line 123 of file connection.hpp.

References Socket::IsValid(), and Connection::socket_.

Referenced by Connection::operator=(), Connection::OutputOstream(), and Connection::~Connection().

◆ operator=()

◆ operator==()

bool operator== ( const Connection c) const
inlinenoexcept

Checks wether two connections have the same underlying socket or not.

Definition at line 152 of file connection.hpp.

References Socket::fd(), and Connection::GetSocket().

◆ OutputOstream()

std::ostream& OutputOstream ( std::ostream &  os) const
inlinefinalvirtual

make ostreamable

Implements Connection.

Definition at line 218 of file connection.hpp.

References Socket::fd(), Connection::GetPeerAddress(), Connection::GetSocket(), and Connection::IsValid().

◆ peer_id()

size_t peer_id ( ) const
inline

Gets the id of the worker this connection is connected to.

Definition at line 107 of file connection.hpp.

References Connection::peer_id_.

◆ RecvOne()

ssize_t RecvOne ( void *  out_data,
size_t  size 
)
inlinefinalvirtual

Non-blocking receive of at most size data. returns number of bytes actually received. check errno for errors.

Implements Connection.

Definition at line 190 of file connection.hpp.

References Socket::recv_one(), Connection::rx_bytes_, Connection::SetNonBlocking(), and Connection::socket_.

◆ SendOne()

ssize_t SendOne ( const void *  data,
size_t  size,
Flags  flags 
)
inlinefinalvirtual

Non-blocking send of a (data,size) message. returns number of bytes possible to send. check errno for errors.

Implements Connection.

Definition at line 171 of file connection.hpp.

References MSG_MORE, Connection::MsgMore, Socket::send_one(), Connection::SetNonBlocking(), Connection::socket_, and Connection::tx_bytes_.

◆ set_group_id()

void set_group_id ( size_t  groupId)
inline

Sets the group id of this connection.

Definition at line 115 of file connection.hpp.

References Connection::group_id_.

◆ set_peer_id()

void set_peer_id ( size_t  peerId)
inline

Sets the id of the worker this connection is connected to.

Definition at line 119 of file connection.hpp.

References Connection::peer_id_.

◆ set_state()

void set_state ( ConnectionState  state)
inline

Sets the state of this connection.

Definition at line 111 of file connection.hpp.

References Connection::state(), and Connection::state_.

◆ SetNonBlocking()

void SetNonBlocking ( bool  non_blocking)
inline

Set socket to non-blocking.

Definition at line 142 of file connection.hpp.

References Socket::SetNonBlocking(), and Connection::socket_.

Referenced by Connection::RecvOne(), Connection::SendOne(), Connection::SyncRecv(), and Connection::SyncSend().

◆ state()

ConnectionState state ( ) const
inline

Gets the state of this connection.

Definition at line 99 of file connection.hpp.

References Connection::state_.

Referenced by Connection::set_state().

◆ SyncRecv()

void SyncRecv ( void *  out_data,
size_t  size 
)
inlinefinalvirtual

Synchronous blocking receive a message of given size. The size must match the SyncSend size for some network layers may only support matching messages (read: RDMA!, but also true for the mock net). Throws a net::Exception on errors.

Implements Connection.

Definition at line 183 of file connection.hpp.

References Socket::recv(), Connection::rx_bytes_, Connection::SetNonBlocking(), and Connection::socket_.

Referenced by Connection::SyncRecvSend(), and Connection::SyncSendRecv().

◆ SyncRecvSend()

void SyncRecvSend ( const void *  send_data,
size_t  send_size,
void *  recv_data,
size_t  recv_size 
)
inlinefinalvirtual

Implements Connection.

Definition at line 206 of file connection.hpp.

References Connection::NoFlags, Connection::SyncRecv(), and Connection::SyncSend().

◆ SyncSend()

void SyncSend ( const void *  data,
size_t  size,
Flags  flags 
)
inlinefinalvirtual

Synchronous blocking send of the (data,size) packet. if sending fails, a net::Exception is thrown.

Implements Connection.

Definition at line 162 of file connection.hpp.

References MSG_MORE, Connection::MsgMore, Socket::send(), Connection::SetNonBlocking(), Connection::socket_, and Connection::tx_bytes_.

Referenced by Connection::SyncRecvSend(), and Connection::SyncSendRecv().

◆ SyncSendRecv()

void SyncSendRecv ( const void *  send_data,
size_t  send_size,
void *  recv_data,
size_t  recv_size 
)
inlinefinalvirtual

Synchronous blocking sending and receive a message of given size. The size must match the SyncSendRecv size for some network layers may only support matching messages (read: RDMA!, but also true for the mock net). Throws a net::Exception on errors.

Implements Connection.

Definition at line 200 of file connection.hpp.

References Connection::NoFlags, Connection::SyncRecv(), and Connection::SyncSend().

◆ ToString()

std::string ToString ( ) const
inlinefinalvirtual

return a string representation of this connection, for user output.

Implements Connection.

Definition at line 126 of file connection.hpp.

References Connection::GetSocket(), and thrill::mem::to_string().

Member Data Documentation

◆ debug

constexpr bool debug = false
staticprivate

Definition at line 56 of file connection.hpp.

◆ group_id_

size_t group_id_ = size_t(-1)
private

The id of the group this connection is associated with.

Definition at line 237 of file connection.hpp.

Referenced by Connection::group_id(), Connection::operator=(), and Connection::set_group_id().

◆ peer_id_

size_t peer_id_ = size_t(-1)
private

The id of the worker this connection is connected to.

Definition at line 240 of file connection.hpp.

Referenced by Connection::operator=(), Connection::peer_id(), and Connection::set_peer_id().

◆ socket_

◆ state_

ConnectionState state_ = ConnectionState::Invalid
private

The connection state of this connection in the Thrill network state machine.

Definition at line 234 of file connection.hpp.

Referenced by Connection::operator=(), Connection::set_state(), and Connection::state().


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