FiberIO
Fiber-based C++ network library
Public Member Functions | Static Public Attributes | List of all members
fiberio::socket Class Reference

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...
 
socketoperator= (const socket &other)
 Copy assignment. More...
 
socketoperator= (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
 

Detailed Description

Client socket for communicating over a network and opening connections.

Definition at line 12 of file socket.hpp.

Constructor & Destructor Documentation

◆ socket() [1/4]

fiberio::socket::socket ( )

Creates a non-connected socket.

◆ socket() [2/4]

fiberio::socket::socket ( const socket )

Creates a socket based on another.

◆ socket() [3/4]

fiberio::socket::socket ( socket &&  )

Creates a socket based on another, which will be invalid after.

◆ socket() [4/4]

fiberio::socket::socket ( std::shared_ptr< socket_impl > &&  impl)

For internal use only.

◆ ~socket()

fiberio::socket::~socket ( )

Destructor. Closes the socket if still open.

Member Function Documentation

◆ close()

void fiberio::socket::close ( )

Closes the socket if it's not already closed.

It's safe to call this repeatedly as it's idempotent.

◆ connect()

void fiberio::socket::connect ( const std::string &  host,
uint16_t  port 
)

Connects to host:port and throws an exception on failure.

◆ is_open()

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.

◆ operator=() [1/2]

socket& fiberio::socket::operator= ( const socket other)

Copy assignment.

◆ operator=() [2/2]

socket& fiberio::socket::operator= ( socket &&  other)

Move assignment.

◆ read()

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.

◆ read_exactly()

void fiberio::socket::read_exactly ( char *  buf,
std::size_t  size 
)

The same as read() but always fills the buffer completely (or fails)

◆ read_string()

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.

◆ read_string_exactly()

std::string fiberio::socket::read_string_exactly ( std::size_t  count)

The same as read_string() but reads exactly count bytes (or fails)

◆ write() [1/2]

void fiberio::socket::write ( const char *  data,
std::size_t  len 
)

Writes data from the buffer and returns once the buffer can be freed.

◆ write() [2/2]

void fiberio::socket::write ( const std::string &  data)

Writes data from the buffer and returns once the buffer can be freed.

Member Data Documentation

◆ DEFAULT_BUF_SIZE

constexpr std::size_t fiberio::socket::DEFAULT_BUF_SIZE = 256 * 1024
static

Definition at line 15 of file socket.hpp.


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