aboutsummaryrefslogtreecommitdiff
path: root/src/util/sock.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/sock.h')
-rw-r--r--src/util/sock.h81
1 files changed, 74 insertions, 7 deletions
diff --git a/src/util/sock.h b/src/util/sock.h
index 26fe60f18f..59cc8c0b1d 100644
--- a/src/util/sock.h
+++ b/src/util/sock.h
@@ -6,11 +6,19 @@
#define BITCOIN_UTIL_SOCK_H
#include <compat.h>
+#include <threadinterrupt.h>
+#include <util/time.h>
#include <chrono>
#include <string>
/**
+ * Maximum time to wait for I/O readiness.
+ * It will take up until this time to break off in case of an interruption.
+ */
+static constexpr auto MAX_WAIT_FOR_IO = 1s;
+
+/**
* 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.
*/
@@ -56,7 +64,7 @@ public:
* Get the value of the contained socket.
* @return socket or INVALID_SOCKET if empty
*/
- virtual SOCKET Get() const;
+ [[nodiscard]] virtual SOCKET Get() const;
/**
* Get the value of the contained socket and drop ownership. It will not be closed by the
@@ -72,15 +80,31 @@ public:
/**
* 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.
+ * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual ssize_t Send(const void* data, size_t len, int flags) const;
+ [[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
/**
* recv(2) wrapper. Equivalent to `recv(this->Get(), buf, len, flags);`. Code that uses this
- * wrapper can be unit-tested if this method is overridden by a mock Sock implementation.
+ * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
+ */
+ [[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
+
+ /**
+ * connect(2) wrapper. Equivalent to `connect(this->Get(), addr, addrlen)`. Code that uses this
+ * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual ssize_t Recv(void* buf, size_t len, int flags) const;
+ [[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
+
+ /**
+ * getsockopt(2) wrapper. Equivalent to
+ * `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
+ * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
+ */
+ [[nodiscard]] virtual int GetSockOpt(int level,
+ int opt_name,
+ void* opt_val,
+ socklen_t* opt_len) const;
using Event = uint8_t;
@@ -98,11 +122,54 @@ public:
* Wait for readiness for input (recv) or output (send).
* @param[in] timeout Wait this much for at least one of the requested events to occur.
* @param[in] requested Wait for those events, bitwise-or of `RECV` and `SEND`.
+ * @param[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.
* @return true on success and false otherwise
*/
- virtual bool Wait(std::chrono::milliseconds timeout, Event requested) const;
+ [[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
+ Event requested,
+ Event* occurred = nullptr) const;
+
+ /* Higher level, convenience, methods. These may throw. */
+
+ /**
+ * Send the given data, retrying on transient errors.
+ * @param[in] data Data to send.
+ * @param[in] timeout Timeout for the entire operation.
+ * @param[in] interrupt If this is signaled then the operation is canceled.
+ * @throws std::runtime_error if the operation cannot be completed. In this case only some of
+ * the data will be written to the socket.
+ */
+ virtual void SendComplete(const std::string& data,
+ std::chrono::milliseconds timeout,
+ CThreadInterrupt& interrupt) const;
+
+ /**
+ * Read from socket until a terminator character is encountered. Will never consume bytes past
+ * the terminator from the socket.
+ * @param[in] terminator Character up to which to read from the socket.
+ * @param[in] timeout Timeout for the entire operation.
+ * @param[in] interrupt If this is signaled then the operation is canceled.
+ * @param[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.
+ * @return The data that has been read, without the terminating character.
+ * @throws std::runtime_error if the operation cannot be completed. In this case some bytes may
+ * have been consumed from the socket.
+ */
+ [[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
+ std::chrono::milliseconds timeout,
+ CThreadInterrupt& interrupt,
+ size_t max_data) const;
+
+ /**
+ * Check if still connected.
+ * @param[out] errmsg The error string, if the socket has been disconnected.
+ * @return true if connected
+ */
+ [[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
-private:
+protected:
/**
* Contained socket. `INVALID_SOCKET` designates the object is empty.
*/