LIBWIRE
Next-generation C++17 networking library.
|
Descriptor wrapper for TCP socket. More...
#include <socket.hpp>
Public Member Functions | |
socket () noexcept=default | |
Create new socket object. More... | |
socket (internal_::socket &&i) noexcept | |
Initialize socket from underlying raw handle. More... | |
socket (const socket &)=delete | |
socket (socket &&) noexcept=default | |
~socket () | |
Shutdown and then close socket. More... | |
void | close () noexcept |
Close and destroy underlying socket. More... | |
void | connect (endpoint target, std::error_code &ec) noexcept |
Initialize underlying socket and connect to remote endpoint. More... | |
bool | is_open () const |
Check whether underlying socket is open. More... | |
internal_::socket::native_handle_t | native_handle () const noexcept |
Get native handle/descriptor for socket. More... | |
socket & | operator= (const socket &)=delete |
socket & | operator= (socket &&) noexcept=default |
void | shutdown (bool read=true, bool write=true) noexcept |
Shutdown reading/writing part of full-duplex connection (or both if read and write is true). More... | |
Socket options | |
Several aspects of socket behavior can be changes by setting flags. For example: You can disable Nagle's algorithm and specify smaller retransmission timeout using following code: socket.set_option(tcp::user_timeout, 500ms);
Even more, if some platform-specific option is not provided by generic libwire interface you can add it easily because option is just a class with two static functions. struct example_option_t {
bool get(const tcp::socket& sock) const noexcept {
return true;
}
void set(tcp::socket& sock, bool value) const noexcept {
// You can just not declare this function if option
// can't be set.
// Use sock.native_handle() here to directly
// interact with system API.
}
} example_option{};
With example above you can write: | |
template<typename Option > | |
auto | option (const Option &) const noexcept |
Query socket option value specified by type tag Option. More... | |
template<typename Option , typename... Args> | |
void | set_option (const Option &, Args &&...args) noexcept |
Set socket option value specified by type tag Option to value value. More... | |
Connection Endpoints Information | |
Get address and port of local/remote end of connection. Pair of endpoints uniquely identifies active connection, server can use remote_endpoint() to identify "sessions", client can use local_endpoint() for same purpose. Return value is undefined if is_open() = false. Example | |
endpoint | local_endpoint () const noexcept |
Get address and port of local end of connection. More... | |
endpoint | remote_endpoint () const noexcept |
Get address and port of remote end of connection. More... | |
Blocking I/O | |
I/O functions in this category usually block thread until operation is completed. | |
template<typename Buffer = std::vector<uint8_t>> | |
Buffer & | read (size_t bytes_count, Buffer &, std::error_code &) noexcept |
Read up to bytes_count bytes from socket into buffer passed by reference. More... | |
template<typename Buffer = std::vector<uint8_t>> | |
Buffer | read (size_t bytes_count, std::error_code &) noexcept |
Same as overload with Buffer argument but return newly allocated buffer every time. More... | |
template<typename Buffer = std::vector<uint8_t>> | |
Buffer & | read_until (uint8_t delimiter, Buffer &buf, std::error_code &, size_t max_size=0) noexcept |
Read from socket until until gives byte is found or max_size bytes read. More... | |
template<typename Buffer = std::vector<uint8_t>> | |
Buffer | read_until (uint8_t delimiter, std::error_code &, size_t max_size=0) noexcept |
Same as overload with buffer argument but returns newly allocated buffer every time. More... | |
template<typename Buffer = std::vector<uint8_t>> | |
size_t | write (const Buffer &, std::error_code &) noexcept |
Write contents of buffer to socket. More... | |
Descriptor wrapper for TCP socket.
TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol. It is especially well suited for continuous transmission of data.
There is simplified graph of TCP socket states:
Definition at line 59 of file socket.hpp.
|
defaultnoexcept |
Create new socket object.
Implementation Note: Constructor itself doesn't allocates socket descriptor. It's done on connect call. This is done to allow socket to be constructed without knowing remote endpoint IP version ahead-of-time.
|
noexcept |
Initialize socket from underlying raw handle.
Used by tcp::listener for listener::accept function. Not part of the public API.
|
delete |
|
defaultnoexcept |
libwire::tcp::socket::~socket | ( | ) |
Shutdown and then close socket.
|
noexcept |
Close and destroy underlying socket.
Closed socket object CAN be reused by calling connect.
May block if socket has untransmitted data even if socket is in non-blocking mode.
|
noexcept |
Initialize underlying socket and connect to remote endpoint.
bool libwire::tcp::socket::is_open | ( | ) | const |
Check whether underlying socket is open.
|
noexcept |
Get address and port of local end of connection.
Address is a local address of interface used for connection and port is randomly picked from implementation-defined range of ephemeral ports (usually 49152-65535).
|
noexcept |
Get native handle/descriptor for socket.
Returned value is undefined if is_open returns false.
|
inlinenoexcept |
Query socket option value specified by type tag Option.
Example:
Definition at line 160 of file socket.hpp.
|
noexcept |
Read up to bytes_count bytes from socket into buffer passed by reference.
Buffer will be resized to actual count of bytes received.
Error code will be set to error code if anything went wrong, buffer will be resized to 0 elements.
Buffer type requirements:
Buffer must be container that encapsulates dynamic array, so it must have data, size and resize member functions with behavior as in std::vector.
Definition at line 380 of file socket.hpp.
|
noexcept |
Same as overload with Buffer argument but return newly allocated buffer every time.
Definition at line 404 of file socket.hpp.
|
noexcept |
Read from socket until until gives byte is found or max_size bytes read.
Buffer Type Requirements
size(), clear() and push_back() functions with behavior defined in SequenceContainer concept.
Definition at line 429 of file socket.hpp.
|
noexcept |
Same as overload with buffer argument but returns newly allocated buffer every time.
Definition at line 442 of file socket.hpp.
|
noexcept |
Get address and port of remote end of connection.
Usually same as address/port passed in connect.
|
inlinenoexcept |
Set socket option value specified by type tag Option to value value.
Setting option not supported on current platform or specifying invalid value results in undefined behavior. But usually new value just ignored and corresponding option(tag) will return old value.
Example:
Definition at line 182 of file socket.hpp.
|
noexcept |
Shutdown reading/writing part of full-duplex connection (or both if read and write is true).
This function have no effect if socket is not connected ("Not connected" error is silently ignored).
If both read and write arguments set to false behavior is undefined.
|
noexcept |
Write contents of buffer to socket.
Error code will be set if anything went wrong.
Returns actual amount of bytes written, usually same as buffer size unless socket is in non-blocking mode.
Buffer type requirements
Buffer must be container that encapsulates dynamic array, so it must have data and size member functions with behavior as in std::vector.
Definition at line 416 of file socket.hpp.