Class ClientSocket
Defined in File socket.hpp
Nested Relationships
Nested Types
Inheritance Relationships
Base Type
public c2k::AbstractSocket(Class AbstractSocket)
Class Documentation
-
class ClientSocket : public c2k::AbstractSocket
Represents a client socket for communication.
This class extends the AbstractSocket class and provides additional functionality for sending and receiving data over a network connection. It manages send and receive tasks asynchronously and handles socket operations.
Public Functions
-
ClientSocket(ClientSocket &&other) noexcept = default
-
ClientSocket &operator=(ClientSocket &&other) noexcept = default
-
~ClientSocket()
-
inline bool is_connected() const
Checks if the socket is currently connected.
This function returns a boolean value indicating whether the socket is currently connected or not.
- Returns:
True if the socket is connected, false otherwise.
-
std::future<std::size_t> send(std::vector<std::byte> data)
Sends the given data through the socket.
The send method sends the provided data through the socket. It returns a std::future<std::size_t> that represents the amount of data that has been transmitted.
If the data vector is empty, a SendError exception will be thrown.
- Parameters:
data – The data to be sent through the socket.
- Throws:
SendError – If the data vector is empty.
- Returns:
A std::future<std::size_t> that represents the amount of data that has been transmitted.
-
inline std::future<std::size_t> send(std::integral auto... values)
Sends data over the socket.
This method sends the given values over the socket. It constructs a message package by inserting the values into a message buffer. It then sends it using through the socket.
- Template Parameters:
std::integral... – values The values to send
- Returns:
A std::future<std::size_t> that will hold the number of bytes sent
-
inline std::future<std::size_t> send(MessageBuffer const &package)
Sends data over the socket.
This method sends the given message over the socket.
The send method returns a std::future<std::size_t> that represents the amount of data that has been transmitted.
If the data vector is empty, a SendError exception will be thrown.
- Parameters:
package – The message package to be sent through the socket.
- Throws:
SendError – If the data vector is empty.
- Returns:
A std::future<std::size_t> that represents the amount of data that has been transmitted.
-
inline std::future<std::size_t> send(MessageBuffer &&package)
Sends data over the socket.
This method sends the given message over the socket.
The send method returns a std::future<std::size_t> that represents the amount of data that has been transmitted.
If the data vector is empty, a SendError exception will be thrown.
- Parameters:
package – The message package to be sent through the socket.
- Throws:
SendError – If the data vector is empty.
- Returns:
A std::future<std::size_t> that represents the amount of data that has been transmitted.
-
std::future<std::vector<std::byte>> receive(std::size_t max_num_bytes)
Asynchronously receives up to a specified maximum number of bytes from the socket.
This method receives up to a specified number of bytes from the socket asynchronously. It returns an std::future object that can be used to retrieve the received data.
- Parameters:
max_num_bytes – The maximum number of bytes to receive from the socket
- Returns:
An std::future object representing the asynchronous receive operation. The future will hold an std::vector<std::byte> containing the received bytes. If no data is received within the default timeout of 1 second, an exception will be stored in the future.
-
std::future<std::vector<std::byte>> receive(std::size_t max_num_bytes, Timeout timeout)
Asynchronously receives up to a specified maximum number of bytes from the socket.
This method receives up to a specified number of bytes from the socket asynchronously. It returns an std::future object that can be used to retrieve the received data.
- Parameters:
max_num_bytes – The maximum number of bytes to receive from the socket
timeout – The maximum amount of time to wait for incoming data
- Returns:
An std::future object representing the asynchronous receive operation. The future will hold an std::vector<std::byte> containing the received bytes. If no data is received within the specified timeout, an exception will be stored in the future.
-
std::future<std::vector<std::byte>> receive_exact(std::size_t num_bytes)
Receives a specified number of bytes from the client socket.
This method receives exactly the specified number of bytes from the client socket. It returns a future object that will be fulfilled with a vector of bytes containing the received data.
- Parameters:
num_bytes – The number of bytes to receive
- Returns:
A future that holds the received bytes as a vector of std::byte. If the operation cannot be completed within the default timeout of 1 second, an exception will be stored in the future.
-
std::future<std::vector<std::byte>> receive_exact(std::size_t num_bytes, Timeout timeout)
Receives a specified number of bytes from the socket.
This method receives exactly the specified number of bytes from the client socket. It returns a future object that will be fulfilled with a vector of bytes containing the received data.
- Parameters:
num_bytes – The number of bytes to receive
timeout – The timeout for the receive operation
- Returns:
A future that holds the received bytes as a vector of std::byte. If the operation cannot be completed within the specified timeout, an exception will be stored in the future.
-
template<std::integral... Ts>
inline auto receive(Timeout const timeout = default_timeout) Reads one or multiple integral values from the socket.
- Template Parameters:
Ts – std::integral… The types of the values to read from the socket
- Parameters:
timeout – The timeout for the receive operation
- Returns:
A future that holds either the read value, if only one type parameter was provided, or a tuple of all the read values according to the provided types. If the operation cannot be completed within the specified timeout, an exception will be stored in the future.
-
void close()
Closes the client socket.
This method closes the client socket and performs necessary cleanup operations. It stops the socket from running and clears any pending queues.
-
ClientSocket(ClientSocket &&other) noexcept = default