Derecho  0.9
Distributed systems toolkit for RDMA
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
tcp::socket Class Reference

#include <tcp.hpp>

Collaboration diagram for tcp::socket:
Collaboration graph
[legend]

Public Member Functions

 socket ()
 Constructs an empty, unconnected socket. More...
 
 socket (std::string server_ip, uint16_t server_port)
 Constructs a socket connected to the specified address and port, blocking until the connection succeeds. More...
 
 socket (socket &&s)
 
socketoperator= (socket &s)=delete
 
socketoperator= (socket &&s)
 
 ~socket ()
 
bool is_empty () const
 
std::string get_self_ip ()
 
std::string get_remote_ip () const
 
int try_connect (std::string servername, int port, int timeout_ms=20000)
 Attempts to connect the socket to the specified address and port, but returns promptly with an error code if the connection attempt fails. More...
 
bool read (char *buffer, size_t size)
 Reads size bytes from the socket and writes them to the given buffer. More...
 
ssize_t read_partial (char *buffer, size_t max_size)
 Attempts to read up to max_size bytes from socket and write them to the given buffer, but returns immediately even if fewer than max_size bytes are available to be read. More...
 
bool probe ()
 Returns true if there is any data available to be read from the socket. More...
 
bool write (const char *buffer, size_t size)
 Writes size bytes from the given buffer to the socket. More...
 
template<typename T >
bool write (const T &obj)
 Convenience method for sending a single POD object (e.g. More...
 
template<typename T >
bool read (T &obj)
 Convenience method for reading a single POD object from the socket and writing it over a local value of that type. More...
 
template<class T >
bool exchange (T local, T &remote)
 

Private Member Functions

 socket (int _sock)
 
 socket (int _sock, std::string remote_ip)
 

Private Attributes

int sock
 
std::string remote_ip
 

Friends

class connection_listener
 

Detailed Description

Definition at line 13 of file tcp.hpp.

Constructor & Destructor Documentation

◆ socket() [1/5]

tcp::socket::socket ( int  _sock)
inlineexplicitprivate

Definition at line 16 of file tcp.hpp.

◆ socket() [2/5]

tcp::socket::socket ( int  _sock,
std::string  remote_ip 
)
inlineexplicitprivate

Definition at line 17 of file tcp.hpp.

◆ socket() [3/5]

tcp::socket::socket ( )
inline

Constructs an empty, unconnected socket.

Definition at line 28 of file tcp.hpp.

◆ socket() [4/5]

tcp::socket::socket ( std::string  server_ip,
uint16_t  server_port 
)

Constructs a socket connected to the specified address and port, blocking until the connection succeeds.

Parameters
server_ipThe IP address of the remote host, as a string
server_portThe port to connect to on the remote host
Exceptions
connection_failureif local socket construction or IP address lookup fails.

Definition at line 21 of file tcp.cpp.

◆ socket() [5/5]

tcp::socket::socket ( socket &&  s)

Definition at line 48 of file tcp.cpp.

◆ ~socket()

tcp::socket::~socket ( )

Definition at line 60 of file tcp.cpp.

Member Function Documentation

◆ exchange()

template<class T >
bool tcp::socket::exchange ( local,
T &  remote 
)
inline

Definition at line 120 of file tcp.hpp.

◆ get_remote_ip()

std::string tcp::socket::get_remote_ip ( ) const
inline

Definition at line 47 of file tcp.hpp.

◆ get_self_ip()

std::string tcp::socket::get_self_ip ( )

Definition at line 192 of file tcp.cpp.

◆ is_empty()

bool tcp::socket::is_empty ( ) const

Definition at line 64 of file tcp.cpp.

◆ operator=() [1/2]

socket& tcp::socket::operator= ( socket s)
delete

◆ operator=() [2/2]

socket & tcp::socket::operator= ( socket &&  s)

Definition at line 53 of file tcp.cpp.

◆ probe()

bool tcp::socket::probe ( )

Returns true if there is any data available to be read from the socket.

Definition at line 165 of file tcp.cpp.

◆ read() [1/2]

bool tcp::socket::read ( char *  buffer,
size_t  size 
)

Reads size bytes from the socket and writes them to the given buffer.

Parameters
bufferA pointer to a byte buffer that should be used to store the result of the read.
sizeThe number of bytes to read.
Returns
True if the read was successful, false if there was an error before size bytes could be read.

Definition at line 138 of file tcp.cpp.

◆ read() [2/2]

template<typename T >
bool tcp::socket::read ( T &  obj)
inline

Convenience method for reading a single POD object from the socket and writing it over a local value of that type.

Hides the ugly cast to char*.

Parameters
objA local value of type T, which will be overwritten by a value of the same size read from the socket.

Definition at line 115 of file tcp.hpp.

◆ read_partial()

ssize_t tcp::socket::read_partial ( char *  buffer,
size_t  max_size 
)

Attempts to read up to max_size bytes from socket and write them to the given buffer, but returns immediately even if fewer than max_size bytes are available to be read.

A very thin wrapper around a single read() system call.

Parameters
bufferA pointer to a byte buffer that should be used to store the result of the read
max_sizeThe number of bytes to attempt to read
Returns
The number of bytes actually read, or -1 if there was an error

Definition at line 156 of file tcp.cpp.

◆ try_connect()

int tcp::socket::try_connect ( std::string  servername,
int  port,
int  timeout_ms = 20000 
)

Attempts to connect the socket to the specified address and port, but returns promptly with an error code if the connection attempt fails.

Also allows the caller to specify the timeout after which the connection attempt will give up and return ETIMEDOUT.

Parameters
servernameThe IP address of the remote host, as a string
portThe port to connect to on the remote host
timeout_msThe number of milliseconds to wait for the remote host to accept the connection; default is 20 seconds.
Returns
Zero if the connection was successful, or the error code (from the set defined in sys/socket.h) that resulted from a failed connect() system call.

Definition at line 66 of file tcp.cpp.

◆ write() [1/2]

bool tcp::socket::write ( const char *  buffer,
size_t  size 
)

Writes size bytes from the given buffer to the socket.

Parameters
bufferA pointer to a byte buffer whose data should be sent over the socket.
sizeThe number of bytes from the buffer to send.
Returns
True if the write was successful, false if there was an error before size bytes could be written.

Definition at line 171 of file tcp.cpp.

◆ write() [2/2]

template<typename T >
bool tcp::socket::write ( const T &  obj)
inline

Convenience method for sending a single POD object (e.g.

an int) over the socket.

Definition at line 104 of file tcp.hpp.

Friends And Related Function Documentation

◆ connection_listener

friend class connection_listener
friend

Definition at line 20 of file tcp.hpp.

Member Data Documentation

◆ remote_ip

std::string tcp::socket::remote_ip
private

Definition at line 21 of file tcp.hpp.

◆ sock

int tcp::socket::sock
private

Definition at line 14 of file tcp.hpp.


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