Bitcoin ABC 0.30.3
P2P Digital Currency
|
RAII helper class that manages a socket. More...
#include <sock.h>
Public Types | |
using | Event = uint8_t |
Public Member Functions | |
Sock () | |
Default constructor, creates an empty object that does nothing when destroyed. More... | |
Sock (SOCKET s) | |
Take ownership of an existent socket. More... | |
Sock (const Sock &)=delete | |
Copy constructor, disabled because closing the same socket twice is undesirable. More... | |
Sock (Sock &&other) | |
Move constructor, grab the socket from another object and close ours (if set). More... | |
virtual | ~Sock () |
Destructor, close the socket or do nothing if empty. More... | |
Sock & | operator= (const Sock &)=delete |
Copy assignment operator, disabled because closing the same socket twice is undesirable. More... | |
virtual Sock & | operator= (Sock &&other) |
Move assignment operator, grab the socket from another object and close ours (if set). More... | |
virtual SOCKET | Get () const |
Get the value of the contained socket. More... | |
virtual SOCKET | Release () |
Get the value of the contained socket and drop ownership. More... | |
virtual void | Reset () |
Close if non-empty. More... | |
virtual ssize_t | Send (const void *data, size_t len, int flags) const |
send(2) wrapper. More... | |
virtual ssize_t | Recv (void *buf, size_t len, int flags) const |
recv(2) wrapper. More... | |
virtual int | Connect (const sockaddr *addr, socklen_t addr_len) const |
connect(2) wrapper. More... | |
virtual int | GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const |
getsockopt(2) wrapper. More... | |
virtual bool | Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const |
Wait for readiness for input (recv) or output (send). More... | |
virtual void | SendComplete (const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const |
Send the given data, retrying on transient errors. More... | |
virtual std::string | RecvUntilTerminator (uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const |
Read from socket until a terminator character is encountered. More... | |
virtual bool | IsConnected (std::string &errmsg) const |
Check if still connected. More... | |
Static Public Attributes | |
static constexpr Event | RECV = 0b01 |
If passed to Wait() , then it will wait for readiness to read from the socket. More... | |
static constexpr Event | SEND = 0b10 |
If passed to Wait() , then it will wait for readiness to send to the socket. More... | |
Protected Attributes | |
SOCKET | m_socket |
Contained socket. More... | |
RAII helper class that manages a socket.
Mimics std::unique_ptr
, but instead of a pointer it contains a socket and closes it automatically when it goes out of scope.
using Sock::Event = uint8_t |
Sock::Sock | ( | ) |
|
explicit |
|
delete |
Copy constructor, disabled because closing the same socket twice is undesirable.
Sock::Sock | ( | Sock && | other | ) |
|
virtual |
|
virtual |
|
virtual |
|
virtual |
|
virtual |
Copy assignment operator, disabled because closing the same socket twice is undesirable.
|
virtual |
|
virtual |
Read from socket until a terminator character is encountered.
Will never consume bytes past the terminator from the socket.
[in] | terminator | Character up to which to read from the socket. |
[in] | timeout | Timeout for the entire operation. |
[in] | interrupt | If this is signaled then the operation is canceled. |
[in] | max_data | The maximum amount of data (in bytes) to receive. If this many bytes are received and there is still no terminator, then this method will throw an exception. |
std::runtime_error | if the operation cannot be completed. In this case some bytes may have been consumed from the socket. |
Definition at line 194 of file sock.cpp.
|
virtual |
|
virtual |
|
virtual |
send(2) wrapper.
Equivalent to send(this->Get(), data, len, flags);
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Definition at line 64 of file sock.cpp.
|
virtual |
Send the given data, retrying on transient errors.
[in] | data | Data to send. |
[in] | timeout | Timeout for the entire operation. |
[in] | interrupt | If this is signaled then the operation is canceled. |
std::runtime_error | if the operation cannot be completed. In this case only some of the data will be written to the socket. |
Definition at line 149 of file sock.cpp.
|
virtual |
Wait for readiness for input (recv) or output (send).
[in] | timeout | Wait this much for at least one of the requested events to occur. |
[in] | requested | Wait for those events, bitwise-or of RECV and SEND . |
[out] | occurred | If not nullptr and true is returned, then upon return this indicates which of the requested events occurred. A timeout is indicated by return value of true and occurred being set to 0. |
Definition at line 82 of file sock.cpp.
|
protected |
|
staticconstexpr |
|
staticconstexpr |