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