Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
Public Types | Public Member Functions | List of all members
tacopie::tcp_socket Class Reference

#include <tcp_socket.hpp>

Public Types

enum  type { CLIENT, SERVER, UNKNOWN }
 

Public Member Functions

 tcp_socket (void)
 ctor
 
 ~tcp_socket (void)=default
 dtor
 
 tcp_socket (fd_t fd, const std::string &host, std::uint32_t port, type t)
 
 tcp_socket (tcp_socket &&)
 move ctor
 
 tcp_socket (const tcp_socket &)=delete
 copy ctor
 
tcp_socketoperator= (const tcp_socket &)=delete
 assignment operator
 
bool operator== (const tcp_socket &rhs) const
 
bool operator!= (const tcp_socket &rhs) const
 
std::vector< char > recv (std::size_t size_to_read)
 
std::size_t send (const std::vector< char > &data, std::size_t size_to_write)
 
void connect (const std::string &host, std::uint32_t port, std::uint32_t timeout_msecs=0)
 
void bind (const std::string &host, std::uint32_t port)
 
void listen (std::size_t max_connection_queue)
 
tcp_socket accept (void)
 
void close (void)
 
const std::string & get_host (void) const
 
std::uint32_t get_port (void) const
 
type get_type (void) const
 
void set_type (type t)
 
fd_t get_fd (void) const
 
bool is_ipv6 (void) const
 

Detailed Description

tacopie::tcp_socket is the class providing low-level TCP socket features. The tcp_socket provides a simple but convenient abstraction to unix and windows sockets. It also provides a socket type checker to ensure that server-only operations are only processable on server sockets, and client-only operations are only processable on client sockets.

Member Enumeration Documentation

◆ type

possible types of a TCP socket, either a client or a server type is used to prevent the used of client specific operations on a server socket (and vice-versa)

UNKNOWN is used when socket type could not be determined for now

Constructor & Destructor Documentation

◆ tcp_socket()

tacopie::tcp_socket::tcp_socket ( fd_t  fd,
const std::string &  host,
std::uint32_t  port,
type  t 
)

custom ctor build socket from existing file descriptor

Parameters
fdfd of the raw socket that will be used to init the tcp_socket object
hosthost associated to the socket
portport associated to the socket
ttype of the socket (client or server)

Member Function Documentation

◆ accept()

tcp_socket tacopie::tcp_socket::accept ( void  )

Accept a new incoming connection. The socket must be of type server to process this operation. If the type of the socket is unknown, the socket type will be set to server.

Returns
Return the tcp_socket associated to the newly accepted connection.

◆ bind()

void tacopie::tcp_socket::bind ( const std::string &  host,
std::uint32_t  port 
)

Binds the socket to the given host and port. The socket must be of type server to process this operation. If the type of the socket is unknown, the socket type will be set to server.

Parameters
hostHostname to be bind to
portPort to be bind to

◆ close()

void tacopie::tcp_socket::close ( void  )

Close the underlying socket.

◆ connect()

void tacopie::tcp_socket::connect ( const std::string &  host,
std::uint32_t  port,
std::uint32_t  timeout_msecs = 0 
)

Connect the socket to the remote server. The socket must be of type client to process this operation. If the type of the socket is unknown, the socket type will be set to client.

Parameters
hostHostname of the target server
portPort of the target server
timeout_msecsmaximum time to connect (will block until connect succeed or timeout expire). 0 will block undefinitely. If timeout expires, connection fails

◆ get_fd()

fd_t tacopie::tcp_socket::get_fd ( void  ) const

direct access to the underlying fd

Returns
underlying socket fd

◆ get_host()

const std::string& tacopie::tcp_socket::get_host ( void  ) const
Returns
the hostname associated with the underlying socket.

◆ get_port()

std::uint32_t tacopie::tcp_socket::get_port ( void  ) const
Returns
the port associated with the underlying socket.

◆ get_type()

type tacopie::tcp_socket::get_type ( void  ) const
Returns
the type associated with the underlying socket.

◆ is_ipv6()

bool tacopie::tcp_socket::is_ipv6 ( void  ) const
Returns
whether the host is IPV6

◆ listen()

void tacopie::tcp_socket::listen ( std::size_t  max_connection_queue)

Make the socket listen for incoming connections. Socket must be of type server to process this operation. If the type of the socket is unknown, the socket type will be set to server.

Parameters
max_connection_queueSize of the queue for incoming connections to be processed by the server

◆ operator!=()

bool tacopie::tcp_socket::operator!= ( const tcp_socket rhs) const

comparison operator

Returns
true when the underlying sockets are different (different file descriptor or socket type).

◆ operator==()

bool tacopie::tcp_socket::operator== ( const tcp_socket rhs) const

comparison operator

Returns
true when the underlying sockets are the same (same file descriptor and socket type).

◆ recv()

std::vector<char> tacopie::tcp_socket::recv ( std::size_t  size_to_read)

Read data synchronously from the underlying socket. The socket must be of type client to process this operation. If the type of the socket is unknown, the socket type will be set to client.

Parameters
size_to_readNumber of bytes to read (might read less than requested)
Returns
Returns the read bytes

◆ send()

std::size_t tacopie::tcp_socket::send ( const std::vector< char > &  data,
std::size_t  size_to_write 
)

Send data synchronously to the underlying socket. The socket must be of type client to process this operation. If the type of the socket is unknown, the socket type will be set to client.

Parameters
dataBuffer containing bytes to be written
size_to_writeNumber of bytes to send
Returns
Returns the number of bytes that were effectively sent.

◆ set_type()

void tacopie::tcp_socket::set_type ( type  t)

set type, should be used if some operations determining socket type have been done on the behalf of the tcp_socket instance

Parameters
ttype of the socket

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