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

#include <tcp_socket.hpp>

Public Types

enum  type { type::CLIENT, type::SERVER, type::UNKNOWN }
 

Public Member Functions

 tcp_socket (void)
 ctor More...
 
 ~tcp_socket (void)=default
 dtor More...
 
 tcp_socket (fd_t fd, const std::string &host, std::uint32_t port, type t)
 
 tcp_socket (tcp_socket &&)
 move ctor More...
 
 tcp_socket (const tcp_socket &)=delete
 copy ctor More...
 
tcp_socketoperator= (const tcp_socket &)=delete
 assignment operator More...
 
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
 

Private Member Functions

void create_socket_if_necessary (void)
 
void check_or_set_type (type t)
 

Private Attributes

fd_t m_fd
 
std::string m_host
 
std::uint32_t m_port
 
type m_type
 

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

Enumerator
CLIENT 
SERVER 
UNKNOWN 

Constructor & Destructor Documentation

◆ tcp_socket() [1/4]

tacopie::tcp_socket::tcp_socket ( void  )

ctor

◆ ~tcp_socket()

tacopie::tcp_socket::~tcp_socket ( void  )
default

dtor

◆ tcp_socket() [2/4]

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)

◆ tcp_socket() [3/4]

tacopie::tcp_socket::tcp_socket ( tcp_socket &&  )

move ctor

◆ tcp_socket() [4/4]

tacopie::tcp_socket::tcp_socket ( const tcp_socket )
delete

copy ctor

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

◆ check_or_set_type()

void tacopie::tcp_socket::check_or_set_type ( type  t)
private

check whether the current socket has an approriate type for that kind of operation if current type is UNKNOWN, update internal type with given type

Parameters
texpected type of our socket to process the operation

◆ 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

◆ create_socket_if_necessary()

void tacopie::tcp_socket::create_socket_if_necessary ( void  )
private

create a new socket if no socket has been initialized yet

◆ 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.

◆ 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=()

tcp_socket& tacopie::tcp_socket::operator= ( const tcp_socket )
delete

assignment operator

◆ 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

Member Data Documentation

◆ m_fd

fd_t tacopie::tcp_socket::m_fd
private

fd associated to the socket

◆ m_host

std::string tacopie::tcp_socket::m_host
private

socket hostname information

◆ m_port

std::uint32_t tacopie::tcp_socket::m_port
private

socket port information

◆ m_type

type tacopie::tcp_socket::m_type
private

type of the socket


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