Botcraft 1.21.4
Loading...
Searching...
No Matches
NetworkManager.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <vector>
7#include <queue>
8#include <thread>
9#include <mutex>
10#include <condition_variable>
11
12#if PROTOCOL_VERSION > 759 /* > 1.19 */
14#endif
15#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
16#include <atomic>
17#endif
18
19namespace Botcraft
20{
21 class TCP_Com;
22 class Authentifier;
23
25 {
26 public:
27 NetworkManager(const std::string& address, const std::string& login, const bool force_microsoft_auth, const std::vector<ProtocolCraft::Handler*>& handlers = {});
28 // Used to create a dummy network manager that does not fire any message
29 // but is always in constant_connection_state
30 NetworkManager(const ProtocolCraft::ConnectionState constant_connection_state);
32
33 void Stop();
34
36 void Send(const std::shared_ptr<ProtocolCraft::Message> msg);
38 const std::string& GetMyName() const;
39
40 void SendChatMessage(const std::string& message);
41 void SendChatCommand(const std::string& command);
42
43 std::thread::id GetProcessingThreadId() const;
44
45 private:
46 void WaitForNewPackets();
47 void ProcessPacket(const std::vector<unsigned char>& packet);
48 void OnNewRawData(const std::vector<unsigned char>& packet);
49
50
52#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
53 virtual void Handle(ProtocolCraft::ClientboundGameProfilePacket& msg) override;
54#else
55 virtual void Handle(ProtocolCraft::ClientboundLoginFinishedPacket& msg) override;
56#endif
57 virtual void Handle(ProtocolCraft::ClientboundHelloPacket& msg) override;
58 virtual void Handle(ProtocolCraft::ClientboundKeepAlivePacket& msg) override;
59#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
60 virtual void Handle(ProtocolCraft::ClientboundPingPacket& msg) override;
61#endif
62#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
63 virtual void Handle(ProtocolCraft::ClientboundCustomQueryPacket& msg) override;
64#endif
65#if PROTOCOL_VERSION > 759 /* > 1.19 */
66 virtual void Handle(ProtocolCraft::ClientboundPlayerChatPacket& msg) override;
67#if PROTOCOL_VERSION < 761 /* < 1.19.3 */
68 virtual void Handle(ProtocolCraft::ClientboundPlayerChatHeaderPacket& msg) override;
69#endif
70#endif
71#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
72 virtual void Handle(ProtocolCraft::ClientboundLoginPacket& msg) override;
73#endif
74#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
81#endif
82#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
84#endif
85
86 private:
87 std::vector<ProtocolCraft::Handler*> subscribed;
88
89 std::shared_ptr<TCP_Com> com;
90 std::shared_ptr<Authentifier> authentifier;
92
93 std::thread m_thread_process;//Thread running to process incoming packets without blocking com
94
95 std::queue<std::vector<unsigned char> > packets_to_process;
96 std::mutex mutex_process;
97 std::condition_variable process_condition;
99
100 std::mutex mutex_send;
101
102 std::string name;
103
104#if PROTOCOL_VERSION > 759 /* > 1.19 */
106#endif
107#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
109 std::atomic<int> message_sent_index;
110#endif
111
112#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
113 std::chrono::steady_clock::time_point chunk_batch_start_time;
114#endif
115
116 };
117}
std::condition_variable process_condition
void AddHandler(ProtocolCraft::Handler *h)
std::thread::id GetProcessingThreadId() const
void SendChatCommand(const std::string &command)
virtual void Handle(ProtocolCraft::ClientboundLoginCompressionPacket &msg) override
void SendChatMessage(const std::string &message)
LastSeenMessagesTracker chat_context
void Send(const std::shared_ptr< ProtocolCraft::Message > msg)
std::shared_ptr< TCP_Com > com
std::chrono::steady_clock::time_point chunk_batch_start_time
const std::string & GetMyName() const
std::shared_ptr< Authentifier > authentifier
const ProtocolCraft::ConnectionState GetConnectionState() const
std::vector< ProtocolCraft::Handler * > subscribed
std::atomic< int > message_sent_index
ProtocolCraft::ConnectionState state
void ProcessPacket(const std::vector< unsigned char > &packet)
std::queue< std::vector< unsigned char > > packets_to_process
ProtocolCraft::UUID chat_session_uuid
void OnNewRawData(const std::vector< unsigned char > &packet)
std::array< unsigned char, 16 > UUID