Furrent
socket.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <memory>
5 #include <vector>
6 
7 #include "asio.hpp"
8 #include "util/result.hpp"
9 
10 using namespace fur::util;
11 
12 namespace fur::download::socket {
13 using timeout = std::chrono::steady_clock::duration;
14 
15 enum class SocketError {
17  Timeout,
19  Other,
20 };
21 
22 struct AsioEngine {
24  asio::io_context ctx;
26  asio::ip::tcp::socket socket{ctx};
27 };
28 
32 class Socket {
33  public:
35  Outcome<SocketError> connect(uint32_t ip, uint16_t port, timeout timeout);
36 
38  bool is_open();
39 
42  Outcome<SocketError> write(const std::vector<uint8_t>& buf, timeout timeout);
45 
47  Outcome<SocketError> close();
48 
49  private:
51  std::unique_ptr<AsioEngine> engine = std::make_unique<AsioEngine>();
52 
55  void run(timeout timeout);
56 };
57 } // namespace fur::download::socket
fur::download::socket::AsioEngine::ctx
asio::io_context ctx
Asynchronous runtime. Restarted before each operation.
Definition: socket.hpp:24
fur::download::socket::AsioEngine
Definition: socket.hpp:22
fur::download::socket
Definition: socket.cpp:8
fur::download::socket::SocketError
SocketError
Definition: socket.hpp:15
fur::download::socket::Socket
Definition: socket.hpp:32
fur::util::Result
Used to handle errors without using exceptions.
Definition: result.hpp:18
fur::download::socket::timeout
std::chrono::steady_clock::duration timeout
Definition: socket.hpp:13
fur::download::socket::SocketError::Timeout
@ Timeout
Socket timed out.
fur::download::socket::SocketError::Other
@ Other
Socket experienced some other generic error.
fur::util
Definition: result.hpp:12
result.hpp