#include <tcp_socket.hpp>
|
enum | type { CLIENT,
SERVER,
UNKNOWN
} |
|
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.
◆ 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
◆ 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
-
fd | fd of the raw socket that will be used to init the tcp_socket object |
host | host associated to the socket |
port | port associated to the socket |
t | type of the socket (client or server) |
◆ accept()
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
-
host | Hostname to be bind to |
port | Port 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
-
host | Hostname of the target server |
port | Port of the target server |
timeout_msecs | maximum 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_queue | Size 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_read | Number 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
-
data | Buffer containing bytes to be written |
size_to_write | Number 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
-
The documentation for this class was generated from the following file: