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 <cstdint>
9#include <memory>
11#include <steamclient/crypto/crypto.hpp>
12#include <steamclient/routing/commands/router.hpp>
13#include <steamclient/routing/events/dispatcher.hpp>
14#include <steamclient/types/msgbase.hpp>
15#include <steamclient/types/packetbase.hpp>
16#include <vector>
17
18namespace Steam::Messaging {
19class JobIDs {
20 using Job = std::function<void(const Steam::Messaging::Packets::IPacketMsg&)>;
21
22 public:
23 static uint64_t generate_jobid() { return next_jobid_++; }
24
25 private:
26 inline static uint64_t next_jobid_ = 0;
27 inline static std::unordered_map<uint64_t, Job> pending_jobs_;
28};
29
30class CMClient : public medooze::EventEmitter {
31 public:
32 CMClient(std::unique_ptr<Steam::Networking::IConnection> connection)
33 : connection_(std::move(connection)) {}
34
35 ~CMClient() {
36 if (connection_ && connection_->is_connected())
37 connection_->network_close();
38 }
39
40 void start_session();
41 void shutdown();
42 inline bool is_connected() const { return connection_->is_connected(); }
43
44 inline bool is_channel_secured() const { return channel_secured_; }
45
46 inline void set_channel_secured(bool val) { channel_secured_ = val; }
47
48 inline Steam::Crypto::EncryptionManager& crypto() { return crypto_; }
49
50 template <typename Request>
51 inline void execute(const Request& req) {
52 using namespace Steam::Dispatch;
53
54 size_t id = Steam::Dispatch::request_id<Request>();
55 auto fn = Steam::Dispatch::g_request_router.table[id];
56
57 if (fn) fn(*this, &req);
58 }
59
60 // This solution is super hacky; we will 100% want to correct this later and
61 // fix the base classes.
62 template <typename THdr>
63 inline void send_msg(const Messages::MsgBaseHdr<THdr>& msg) {
64 send_msg_impl(msg.Serialize());
65 }
66
67 void consume_frame(const std::vector<uint8_t>& frame, bool encrypt = true);
68
69 private:
70 void send_msg_impl(std::vector<byte> buffer);
71
72 inline const bool is_encryption_msg(uint32_t emsg_code) {
73 return (
74 emsg_code ==
75 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptRequest ||
76 emsg_code ==
77 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptResponse ||
78 emsg_code ==
79 (uint32_t)Steam::Internal::Enums::EMsg::ChannelEncryptResult);
80 }
81
82 bool channel_secured_ = false;
83 std::unique_ptr<Steam::Networking::IConnection> connection_;
84 Steam::Crypto::EncryptionManager crypto_;
85};
86} // namespace Steam::Messaging
Abstract transport interface used by the Steam networking layer.