#include <tcp.hpp>
|
| socket () |
| Constructs an empty, unconnected socket. More...
|
|
| socket (std::string server_ip, uint16_t server_port) |
| Constructs a socket connected to the specified address and port, blocking until the connection succeeds. More...
|
|
| socket (socket &&s) |
|
socket & | operator= (socket &s)=delete |
|
socket & | operator= (socket &&s) |
|
| ~socket () |
|
bool | is_empty () const |
|
std::string | get_self_ip () |
|
std::string | get_remote_ip () const |
|
int | try_connect (std::string servername, int port, int timeout_ms=20000) |
| Attempts to connect the socket to the specified address and port, but returns promptly with an error code if the connection attempt fails. More...
|
|
bool | read (char *buffer, size_t size) |
| Reads size bytes from the socket and writes them to the given buffer. More...
|
|
ssize_t | read_partial (char *buffer, size_t max_size) |
| Attempts to read up to max_size bytes from socket and write them to the given buffer, but returns immediately even if fewer than max_size bytes are available to be read. More...
|
|
bool | probe () |
| Returns true if there is any data available to be read from the socket. More...
|
|
bool | write (const char *buffer, size_t size) |
| Writes size bytes from the given buffer to the socket. More...
|
|
template<typename T > |
bool | write (const T &obj) |
| Convenience method for sending a single POD object (e.g. More...
|
|
template<typename T > |
bool | read (T &obj) |
| Convenience method for reading a single POD object from the socket and writing it over a local value of that type. More...
|
|
template<class T > |
bool | exchange (T local, T &remote) |
|
Definition at line 13 of file tcp.hpp.
◆ socket() [1/5]
tcp::socket::socket |
( |
int |
_sock | ) |
|
|
inlineexplicitprivate |
◆ socket() [2/5]
tcp::socket::socket |
( |
int |
_sock, |
|
|
std::string |
remote_ip |
|
) |
| |
|
inlineexplicitprivate |
◆ socket() [3/5]
Constructs an empty, unconnected socket.
Definition at line 28 of file tcp.hpp.
◆ socket() [4/5]
tcp::socket::socket |
( |
std::string |
server_ip, |
|
|
uint16_t |
server_port |
|
) |
| |
Constructs a socket connected to the specified address and port, blocking until the connection succeeds.
- Parameters
-
server_ip | The IP address of the remote host, as a string |
server_port | The port to connect to on the remote host |
- Exceptions
-
Definition at line 21 of file tcp.cpp.
◆ socket() [5/5]
tcp::socket::socket |
( |
socket && |
s | ) |
|
◆ ~socket()
◆ exchange()
template<class T >
bool tcp::socket::exchange |
( |
T |
local, |
|
|
T & |
remote |
|
) |
| |
|
inline |
◆ get_remote_ip()
std::string tcp::socket::get_remote_ip |
( |
| ) |
const |
|
inline |
◆ get_self_ip()
std::string tcp::socket::get_self_ip |
( |
| ) |
|
◆ is_empty()
bool tcp::socket::is_empty |
( |
| ) |
const |
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ probe()
bool tcp::socket::probe |
( |
| ) |
|
Returns true if there is any data available to be read from the socket.
Definition at line 165 of file tcp.cpp.
◆ read() [1/2]
bool tcp::socket::read |
( |
char * |
buffer, |
|
|
size_t |
size |
|
) |
| |
Reads size bytes from the socket and writes them to the given buffer.
- Parameters
-
buffer | A pointer to a byte buffer that should be used to store the result of the read. |
size | The number of bytes to read. |
- Returns
- True if the read was successful, false if there was an error before size bytes could be read.
Definition at line 138 of file tcp.cpp.
◆ read() [2/2]
template<typename T >
bool tcp::socket::read |
( |
T & |
obj | ) |
|
|
inline |
Convenience method for reading a single POD object from the socket and writing it over a local value of that type.
Hides the ugly cast to char*.
- Parameters
-
obj | A local value of type T, which will be overwritten by a value of the same size read from the socket. |
Definition at line 115 of file tcp.hpp.
◆ read_partial()
ssize_t tcp::socket::read_partial |
( |
char * |
buffer, |
|
|
size_t |
max_size |
|
) |
| |
Attempts to read up to max_size bytes from socket and write them to the given buffer, but returns immediately even if fewer than max_size bytes are available to be read.
A very thin wrapper around a single read() system call.
- Parameters
-
buffer | A pointer to a byte buffer that should be used to store the result of the read |
max_size | The number of bytes to attempt to read |
- Returns
- The number of bytes actually read, or -1 if there was an error
Definition at line 156 of file tcp.cpp.
◆ try_connect()
int tcp::socket::try_connect |
( |
std::string |
servername, |
|
|
int |
port, |
|
|
int |
timeout_ms = 20000 |
|
) |
| |
Attempts to connect the socket to the specified address and port, but returns promptly with an error code if the connection attempt fails.
Also allows the caller to specify the timeout after which the connection attempt will give up and return ETIMEDOUT.
- Parameters
-
servername | The IP address of the remote host, as a string |
port | The port to connect to on the remote host |
timeout_ms | The number of milliseconds to wait for the remote host to accept the connection; default is 20 seconds. |
- Returns
- Zero if the connection was successful, or the error code (from the set defined in sys/socket.h) that resulted from a failed connect() system call.
Definition at line 66 of file tcp.cpp.
◆ write() [1/2]
bool tcp::socket::write |
( |
const char * |
buffer, |
|
|
size_t |
size |
|
) |
| |
Writes size bytes from the given buffer to the socket.
- Parameters
-
buffer | A pointer to a byte buffer whose data should be sent over the socket. |
size | The number of bytes from the buffer to send. |
- Returns
- True if the write was successful, false if there was an error before size bytes could be written.
Definition at line 171 of file tcp.cpp.
◆ write() [2/2]
template<typename T >
bool tcp::socket::write |
( |
const T & |
obj | ) |
|
|
inline |
Convenience method for sending a single POD object (e.g.
an int) over the socket.
Definition at line 104 of file tcp.hpp.
◆ connection_listener
◆ remote_ip
std::string tcp::socket::remote_ip |
|
private |
◆ sock
The documentation for this class was generated from the following files: