protocol.h

main 108 lines · 3.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef PROTOCOL_H
24#define PROTOCOL_H
25
26#include "declarations.h"
27#include "inputmessage.h"
28#include "outputmessage.h"
29#include "connection.h"
30
31#include <framework/luaengine/luaobject.h>
32#include <framework/proxy/proxy.h>
33
34#include <zlib.h>
35
36// @bindclass
37class Protocol : public LuaObject
38{
39public:
40 Protocol();
41 virtual ~Protocol();
42
43
44 void connect(const std::string& host, uint16 port);
45 void disconnect();
46
47 void setRecorder(PacketRecorderPtr recorder);
48 void playRecord(PacketPlayerPtr player);
49
50 bool isConnected();
51 bool isConnecting();
52 ticks_t getElapsedTicksSinceLastRead() { return m_connection ? m_connection->getElapsedTicksSinceLastRead() : -1; }
53
54 ConnectionPtr getConnection() { return m_connection; }
55 void setConnection(const ConnectionPtr& connection) { m_connection = connection; }
56
57 void generateXteaKey();
58 void setXteaKey(uint32 a, uint32 b, uint32 c, uint32 d);
59 std::vector<uint32> getXteaKey();
60 void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
61
62 void enableChecksum() { m_checksumEnabled = true; }
63 void enabledSequencedPackets() { m_sequencedPackets = true; }
64 void enableBigPackets() { m_bigPackets = true; }
65 void enableCompression() { m_compression = true; }
66
67 virtual void send(const OutputMessagePtr& outputMessage, bool rawPacket = false);
68 virtual void recv();
69
70 ProtocolPtr asProtocol() { return static_self_cast<Protocol>(); }
71
72protected:
73 virtual void onConnect();
74 virtual void onRecv(const InputMessagePtr& inputMessage);
75 virtual void onError(const boost::system::error_code& err);
76
77 void onProxyPacket(const std::shared_ptr<std::vector<uint8_t>>& packet);
78 void onPlayerPacket(const std::shared_ptr<std::vector<uint8_t>>& packet);
79 void onLocalDisconnected(boost::system::error_code ec);
80 bool m_disconnected = false;
81 uint32_t m_proxy = 0;
82
83 uint32 m_xteaKey[4];
84 uint32 m_packetNumber;
85
86 PacketPlayerPtr m_player;
87 PacketRecorderPtr m_recorder;
88
89private:
90 void internalRecvHeader(uint8* buffer, uint32 size);
91 void internalRecvData(uint8* buffer, uint32 size);
92
93 bool xteaDecrypt(const InputMessagePtr& inputMessage);
94 void xteaEncrypt(const OutputMessagePtr& outputMessage);
95
96 bool m_checksumEnabled;
97 bool m_sequencedPackets;
98 bool m_xteaEncryptionEnabled;
99 bool m_bigPackets;
100 bool m_compression;
101 ConnectionPtr m_connection;
102 InputMessagePtr m_inputMessage;
103 z_stream m_zstream;
104 std::vector<uint8_t> m_zstreamBuffer;
105};
106
107#endif
108
Top