protocol.h

main 112 lines · 2.9 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_PROTOCOL_H_D71405071ACF4137A4B1203899DE80E1
21#define FS_PROTOCOL_H_D71405071ACF4137A4B1203899DE80E1
22
23#include <zlib.h>
24
25#include "connection.h"
26#include "xtea.h"
27
28class Protocol : public std::enable_shared_from_this<Protocol>
29{
30 public:
31 explicit Protocol(Connection_ptr connection) : connection(connection) {}
32 virtual ~Protocol();
33
34 // non-copyable
35 Protocol(const Protocol&) = delete;
36 Protocol& operator=(const Protocol&) = delete;
37
38 virtual void parsePacket(NetworkMessage&) {}
39
40 virtual void onSendMessage(const OutputMessage_ptr& msg) const;
41 void onRecvMessage(NetworkMessage& msg);
42 virtual void onRecvFirstMessage(NetworkMessage& msg) = 0;
43 virtual void onConnect() {}
44
45 bool isConnectionExpired() const {
46 return connection.expired();
47 }
48
49 Connection_ptr getConnection() const {
50 return connection.lock();
51 }
52
53 uint32_t getIP() const;
54
55 //Use this function for autosend messages only
56 OutputMessage_ptr getOutputBuffer(int32_t size);
57
58 OutputMessage_ptr& getCurrentBuffer() {
59 return outputBuffer;
60 }
61
62 void send(OutputMessage_ptr msg) const {
63 if (auto connection = getConnection()) {
64 connection->send(msg);
65 }
66 }
67
68 protected:
69 void disconnect() const {
70 if (auto connection = getConnection()) {
71 connection->close();
72 }
73 }
74 void enableXTEAEncryption() {
75 encryptionEnabled = true;
76 }
77 void setXTEAKey(xtea::key key) {
78 this->key = std::move(key);
79 }
80 void disableChecksum() {
81 checksumEnabled = false;
82 }
83
84 static bool RSA_decrypt(NetworkMessage& msg);
85
86 void setRawMessages(bool value) {
87 rawMessages = value;
88 }
89 void enableCompression();
90
91 virtual void release() {}
92
93 private:
94 void XTEA_encrypt(OutputMessage& msg) const;
95 bool XTEA_decrypt(NetworkMessage& msg) const;
96 void compress(OutputMessage& msg) const;
97
98 friend class Connection;
99
100 OutputMessage_ptr outputBuffer;
101
102 const ConnectionWeak_ptr connection;
103 xtea::key key;
104 bool encryptionEnabled = false;
105 bool checksumEnabled = true;
106 bool rawMessages = false;
107 bool compression = false;
108 mutable z_stream zstream = {0};
109};
110
111#endif
112
Top