Otclient  14/8/2020
protocol.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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 
32 
33 // @bindclass
34 class Protocol : public LuaObject
35 {
36 public:
37  Protocol();
38  virtual ~Protocol();
39 
40  void connect(const std::string& host, uint16 port);
41  void disconnect();
42 
43  bool isConnected();
44  bool isConnecting();
45  ticks_t getElapsedTicksSinceLastRead() { return m_connection ? m_connection->getElapsedTicksSinceLastRead() : -1; }
46 
47  ConnectionPtr getConnection() { return m_connection; }
48  void setConnection(const ConnectionPtr& connection) { m_connection = connection; }
49 
50  void generateXteaKey();
51  void setXteaKey(uint32 a, uint32 b, uint32 c, uint32 d);
52  std::vector<uint32> getXteaKey();
53  void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
54 
55  void enableChecksum() { m_checksumEnabled = true; }
56 
57  virtual void send(const OutputMessagePtr& outputMessage);
58  virtual void recv();
59 
60  ProtocolPtr asProtocol() { return static_self_cast<Protocol>(); }
61 
62 protected:
63  virtual void onConnect();
64  virtual void onRecv(const InputMessagePtr& inputMessage);
65  virtual void onError(const boost::system::error_code& err);
66 
68 
69 private:
70  void internalRecvHeader(uint8* buffer, uint16 size);
71  void internalRecvData(uint8* buffer, uint16 size);
72 
73  bool xteaDecrypt(const InputMessagePtr& inputMessage);
74  void xteaEncrypt(const OutputMessagePtr& outputMessage);
75 
76  bool m_checksumEnabled;
77  bool m_xteaEncryptionEnabled;
78  ConnectionPtr m_connection;
79  InputMessagePtr m_inputMessage;
80 };
81 
82 #endif
Protocol::disconnect
void disconnect()
Definition: protocol.cpp:50
Protocol::asProtocol
ProtocolPtr asProtocol()
Definition: protocol.h:60
outputmessage.h
Protocol::Protocol
Protocol()
Definition: protocol.cpp:28
uint32
uint32_t uint32
Definition: types.h:35
Protocol::onError
virtual void onError(const boost::system::error_code &err)
Definition: protocol.cpp:247
Protocol::onConnect
virtual void onConnect()
Definition: protocol.cpp:237
Protocol::connect
void connect(const std::string &host, uint16 port)
Definition: protocol.cpp:43
ticks_t
int64 ticks_t
Definition: types.h:43
Connection::getElapsedTicksSinceLastRead
ticks_t getElapsedTicksSinceLastRead()
Definition: connection.h:64
luaobject.h
Protocol
Definition: protocol.h:34
uint16
uint16_t uint16
Definition: types.h:36
Protocol::m_xteaKey
uint32 m_xteaKey[4]
Definition: protocol.h:67
declarations.h
Protocol::enableXteaEncryption
void enableXteaEncryption()
Definition: protocol.h:53
Protocol::setConnection
void setConnection(const ConnectionPtr &connection)
Definition: protocol.h:48
Protocol::enableChecksum
void enableChecksum()
Definition: protocol.h:55
Protocol::getXteaKey
std::vector< uint32 > getXteaKey()
Definition: protocol.cpp:163
Protocol::getElapsedTicksSinceLastRead
ticks_t getElapsedTicksSinceLastRead()
Definition: protocol.h:45
Protocol::recv
virtual void recv()
Definition: protocol.cpp:93
Protocol::isConnected
bool isConnected()
Definition: protocol.cpp:58
Protocol::send
virtual void send(const OutputMessagePtr &outputMessage)
Definition: protocol.cpp:72
Protocol::onRecv
virtual void onRecv(const InputMessagePtr &inputMessage)
Definition: protocol.cpp:242
connection.h
Protocol::generateXteaKey
void generateXteaKey()
Definition: protocol.cpp:145
stdext::shared_object_ptr< Connection >
inputmessage.h
Protocol::isConnecting
bool isConnecting()
Definition: protocol.cpp:65
Protocol::setXteaKey
void setXteaKey(uint32 a, uint32 b, uint32 c, uint32 d)
Definition: protocol.cpp:155
Protocol::getConnection
ConnectionPtr getConnection()
Definition: protocol.h:47
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
Protocol::~Protocol
virtual ~Protocol()
Definition: protocol.cpp:35
uint8
uint8_t uint8
Definition: types.h:37