steam-min-cpp
Loading...
Searching...
No Matches
cmclient.hpp
1
3
4#pragma once
5
6#include <steamclient/external/event_emitter.h>
7
8#include <boost/asio/steady_timer.hpp>
9#include <cstdint>
10#include <memory>
12#include <steamclient/crypto/crypto.hpp>
13#include <steamclient/routing/commands/router.hpp>
14#include <steamclient/routing/events/dispatcher.hpp>
15#include <steamclient/types/msgbase.hpp>
16#include <steamclient/types/packetbase.hpp>
17#include <vector>
18
19namespace Steam::Messaging {
20class JobIDs {
21 using Job = std::function<void(const Steam::Messaging::Packets::IPacketMsg&)>;
22
23 public:
24 static uint64_t generate_jobid() { return next_jobid_++; }
25
26 private:
27 inline static uint64_t next_jobid_ = 0;
28 inline static std::unordered_map<uint64_t, Job> pending_jobs_;
29};
30
31class CMClient : public medooze::EventEmitter {
32 public:
33 CMClient(std::unique_ptr<Steam::Networking::IConnection> connection,
34 boost::asio::io_context& ctx)
35 : io_ctx_(ctx), connection_(std::move(connection)) {}
36
37 ~CMClient() { shutdown(); }
38
39 void start_session();
40 void shutdown();
41 inline bool is_connected() const { return connection_->is_connected(); }
42
43 inline bool is_channel_secured() const { return channel_secured_; }
44
45 inline void set_channel_secured(bool val) { channel_secured_ = val; }
46
47 inline Steam::Crypto::EncryptionManager& crypto() { return crypto_; }
48
49 template <typename Request>
50 inline void execute(const Request& req) {
51 using namespace Steam::Dispatch;
52
53 size_t id = Steam::Dispatch::request_id<Request>();
54 auto fn = Steam::Dispatch::g_request_router.table[id];
55
56 if (fn) fn(*this, &req);
57 }
58
59 // Send a message to the connected Steam server.
60 template <typename THdr>
61 inline void send_msg(const Messages::MsgBaseHdr<THdr>& msg) {
62 send_msg_impl(msg.Serialize());
63 }
64
65 void consume_frame(const std::vector<uint8_t>& frame, bool encrypt = true);
66 void kickoff_heartbeat(int interval);
67
68 private:
69 void schedule_heartbeat();
70 void send_msg_impl(std::vector<byte> buffer);
71 void reset();
72
73 inline const bool is_encryption_msg(uint32_t emsg_code) {
74 return (
75 emsg_code ==
76 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptRequest ||
77 emsg_code ==
78 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptResponse ||
79 emsg_code ==
80 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptResult);
81 }
82
83 bool channel_secured_ = false;
84 boost::asio::io_context& io_ctx_;
85 std::unique_ptr<Steam::Networking::IConnection> connection_;
86 Steam::Crypto::EncryptionManager crypto_;
87 std::optional<boost::asio::steady_timer> heartbeat_timer_;
88 std::chrono::seconds heartbeat_interval_{};
89};
90} // namespace Steam::Messaging
Abstract transport interface used by the Steam networking layer.