|
FiberIO
Fiber-based C++ network library
|
Client socket for communicating over a network and opening connections. More...
#include <socket.hpp>
Public Member Functions | |
| socket () | |
| Creates a non-connected socket. More... | |
| socket (const socket &) | |
| Creates a socket based on another. More... | |
| socket (socket &&) | |
| Creates a socket based on another, which will be invalid after. More... | |
| socket (std::shared_ptr< socket_impl > &&impl) | |
| For internal use only. More... | |
| ~socket () | |
| Destructor. Closes the socket if still open. More... | |
| socket & | operator= (const socket &other) |
| Copy assignment. More... | |
| socket & | operator= (socket &&other) |
| Move assignment. More... | |
| void | connect (const std::string &host, uint16_t port) |
| Connects to host:port and throws an exception on failure. More... | |
| std::size_t | read (char *buf, std::size_t size) |
| Reads up to size bytes into buf. More... | |
| void | read_exactly (char *buf, std::size_t size) |
| The same as read() but always fills the buffer completely (or fails) More... | |
| std::string | read_string (std::size_t count=DEFAULT_BUF_SIZE, bool shrink_to_fit=true) |
| Reads up to count bytes and returns it as an std::string. More... | |
| std::string | read_string_exactly (std::size_t count) |
| The same as read_string() but reads exactly count bytes (or fails) More... | |
| void | write (const char *data, std::size_t len) |
| Writes data from the buffer and returns once the buffer can be freed. More... | |
| void | write (const std::string &data) |
| Writes data from the buffer and returns once the buffer can be freed. More... | |
| void | close () |
| Closes the socket if it's not already closed. More... | |
| bool | is_open () |
| Check if the connection is open. More... | |
Static Public Attributes | |
| static constexpr std::size_t | DEFAULT_BUF_SIZE = 256 * 1024 |
Client socket for communicating over a network and opening connections.
Definition at line 12 of file socket.hpp.
| fiberio::socket::socket | ( | ) |
Creates a non-connected socket.
| fiberio::socket::socket | ( | const socket & | ) |
Creates a socket based on another.
| fiberio::socket::socket | ( | socket && | ) |
Creates a socket based on another, which will be invalid after.
| fiberio::socket::socket | ( | std::shared_ptr< socket_impl > && | impl | ) |
For internal use only.
| fiberio::socket::~socket | ( | ) |
Destructor. Closes the socket if still open.
| void fiberio::socket::close | ( | ) |
Closes the socket if it's not already closed.
It's safe to call this repeatedly as it's idempotent.
| void fiberio::socket::connect | ( | const std::string & | host, |
| uint16_t | port | ||
| ) |
Connects to host:port and throws an exception on failure.
| bool fiberio::socket::is_open | ( | ) |
Check if the connection is open.
This starts out true and changes when the socket is closed or discovers that the underlying connection was closed.
| std::size_t fiberio::socket::read | ( | char * | buf, |
| std::size_t | size | ||
| ) |
Reads up to size bytes into buf.
Throws an exception on failure. fiberio::socket_closed_error is thrown if the socket was already closed. If the end-of-stream is encountered during the read, no exception is thrown unless another read call is made.
| void fiberio::socket::read_exactly | ( | char * | buf, |
| std::size_t | size | ||
| ) |
The same as read() but always fills the buffer completely (or fails)
| std::string fiberio::socket::read_string | ( | std::size_t | count = DEFAULT_BUF_SIZE, |
| bool | shrink_to_fit = true |
||
| ) |
Reads up to count bytes and returns it as an std::string.
Allocates a buffer of size count. If shrink_to_fit is true, it will free unused buffer space before returning the string. Otherwise, it will return a string with a capacity of count bytes.
This works the same as read() except that it returns a string.
| std::string fiberio::socket::read_string_exactly | ( | std::size_t | count | ) |
The same as read_string() but reads exactly count bytes (or fails)
| void fiberio::socket::write | ( | const char * | data, |
| std::size_t | len | ||
| ) |
Writes data from the buffer and returns once the buffer can be freed.
| void fiberio::socket::write | ( | const std::string & | data | ) |
Writes data from the buffer and returns once the buffer can be freed.
|
static |
Definition at line 15 of file socket.hpp.
1.8.14