steam-min-cpp
Loading...
Searching...
No Matches
tcp.hpp
1#pragma once
2#include <vector>
3#include <queue>
4#include <mutex>
5#include <thread>
6#include <condition_variable>
7#include <cstdint>
8#include <memory>
9#include <iostream>
10#include <cstring>
11#include <stdexcept>
12#include "connection.hpp"
13
14#include <boost/asio.hpp>
15#include <boost/system/error_code.hpp>
16
17namespace Steam::Networking::Web {
18 class CMFetcher;
19}
20
21namespace Steam::Networking {
22 class TCPConnection: public IConnection {
23 public:
24 TCPConnection(boost::asio::io_context& ctx);
25 ~TCPConnection() override;
26
27 inline ConnectionState state() const override {
28 return state_;
29 }
30
31 void network_connect() override;
32 void network_close() override;
33
34 void async_send(const std::vector<uint8_t>& data) override;
35
37 std::function<void(std::vector<uint8_t>)> handler
38 ) override { on_frame_ = std::move(handler); }
39
40 private:
41 void start_read_header();
42 void start_read_body(uint32_t length);
43 void do_write();
44
45 void handle_disconnect(const std::string& reason);
46 void handle_disconnect(const std::error_code& reason);
47
48 std::unique_ptr<Web::CMFetcher> fetcher_;
49
50
51 std::array<uint8_t, 8> header_buffer_;
52 std::vector<uint8_t> body_buffer_;
53 std::deque<std::vector<uint8_t>> write_queue_;
54
55 ConnectionState state_ = ConnectionState::DISCONNECTED;
56
57 const std::array<uint8_t, 4> MAGIC = {'V','T','0','1'};
58
59 boost::asio::io_context& ctx;
60 boost::asio::ip::tcp::socket socket_;
61 std::function<void(const std::vector<uint8_t>&)> wait_for_callback;
62 std::function<void(std::vector<uint8_t>)> on_frame_;
63
64 };
65}
Transport interface used by the CM networking client.
Definition connection.hpp:23
void network_close() override
Close the active connection.
void async_send(const std::vector< uint8_t > &data) override
Send a raw message frame.
ConnectionState state() const override
Retrieve the current connection state.
Definition tcp.hpp:27
void network_connect() override
Initiate the network connection.
void set_on_frame(std::function< void(std::vector< uint8_t >)> handler) override
Register a frame receive handler.
Definition tcp.hpp:36
Abstract transport interface used by the Steam networking layer.
ConnectionState
Connection lifecycle state.
Definition connection.hpp:12