Otclient  14/8/2020
inputmessage.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 INPUTMESSAGE_H
24 #define INPUTMESSAGE_H
25 
26 #include "declarations.h"
28 
29 // @bindclass
30 class InputMessage : public LuaObject
31 {
32 public:
33  enum {
34  BUFFER_MAXSIZE = 65536,
36  };
37 
38  InputMessage();
39 
40  void setBuffer(const std::string& buffer);
41  std::string getBuffer() { return std::string((char*)m_buffer + m_headerPos, m_messageSize); }
42 
43  void skipBytes(uint16 bytes) { m_readPos += bytes; }
44  void setReadPos(uint16 readPos) { m_readPos = readPos; }
45  uint8 getU8();
46  uint16 getU16();
47  uint32 getU32();
48  uint64 getU64();
49  std::string getString();
50  double getDouble();
51 
52  uint8 peekU8() { uint8 v = getU8(); m_readPos-=1; return v; }
53  uint16 peekU16() { uint16 v = getU16(); m_readPos-=2; return v; }
54  uint32 peekU32() { uint32 v = getU32(); m_readPos-=4; return v; }
55  uint64 peekU64() { uint64 v = getU64(); m_readPos-=8; return v; }
56 
57  bool decryptRsa(int size);
58 
59  int getReadSize() { return m_readPos - m_headerPos; }
60  int getReadPos() { return m_readPos; }
61  int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); }
62  uint16 getMessageSize() { return m_messageSize; }
63 
64  bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; }
65 
66 protected:
67  void reset();
68  void fillBuffer(uint8 *buffer, uint16 size);
69 
70  void setHeaderSize(uint16 size);
71  void setMessageSize(uint16 size) { m_messageSize = size; }
72 
73  uint8* getReadBuffer() { return m_buffer + m_readPos; }
74  uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
75  uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
76  uint16 getHeaderSize() { return (MAX_HEADER_SIZE - m_headerPos); }
77 
78  uint16 readSize() { return getU16(); }
79  bool readChecksum();
80 
81  friend class Protocol;
82 
83 private:
84  bool canRead(int bytes);
85  void checkRead(int bytes);
86  void checkWrite(int bytes);
87 
88  uint16 m_headerPos;
89  uint16 m_readPos;
90  uint16 m_messageSize;
91  uint8 m_buffer[BUFFER_MAXSIZE];
92 };
93 
94 #endif
InputMessage::setBuffer
void setBuffer(const std::string &buffer)
Definition: inputmessage.cpp:38
InputMessage::skipBytes
void skipBytes(uint16 bytes)
Definition: inputmessage.h:43
InputMessage::MAX_HEADER_SIZE
@ MAX_HEADER_SIZE
Definition: inputmessage.h:35
InputMessage::getDouble
double getDouble()
Definition: inputmessage.cpp:89
InputMessage::setHeaderSize
void setHeaderSize(uint16 size)
Definition: inputmessage.cpp:110
InputMessage::getU16
uint16 getU16()
Definition: inputmessage.cpp:56
uint32
uint32_t uint32
Definition: types.h:35
InputMessage::getReadPos
int getReadPos()
Definition: inputmessage.h:60
InputMessage
Definition: inputmessage.h:30
InputMessage::peekU8
uint8 peekU8()
Definition: inputmessage.h:52
InputMessage::getU8
uint8 getU8()
Definition: inputmessage.cpp:48
InputMessage::eof
bool eof()
Definition: inputmessage.h:64
InputMessage::InputMessage
InputMessage()
Definition: inputmessage.cpp:26
InputMessage::getUnreadSize
int getUnreadSize()
Definition: inputmessage.h:61
luaobject.h
Protocol
Definition: protocol.h:34
uint16
uint16_t uint16
Definition: types.h:36
InputMessage::decryptRsa
bool decryptRsa(int size)
Definition: inputmessage.cpp:96
declarations.h
InputMessage::peekU64
uint64 peekU64()
Definition: inputmessage.h:55
InputMessage::setMessageSize
void setMessageSize(uint16 size)
Definition: inputmessage.h:71
InputMessage::getString
std::string getString()
Definition: inputmessage.cpp:80
InputMessage::getU64
uint64 getU64()
Definition: inputmessage.cpp:72
InputMessage::getU32
uint32 getU32()
Definition: inputmessage.cpp:64
InputMessage::peekU32
uint32 peekU32()
Definition: inputmessage.h:54
InputMessage::setReadPos
void setReadPos(uint16 readPos)
Definition: inputmessage.h:44
uint64
uint64_t uint64
Definition: types.h:34
InputMessage::peekU16
uint16 peekU16()
Definition: inputmessage.h:53
InputMessage::getBuffer
std::string getBuffer()
Definition: inputmessage.h:41
InputMessage::getHeaderSize
uint16 getHeaderSize()
Definition: inputmessage.h:76
InputMessage::getReadBuffer
uint8 * getReadBuffer()
Definition: inputmessage.h:73
InputMessage::reset
void reset()
Definition: inputmessage.cpp:31
InputMessage::getHeaderBuffer
uint8 * getHeaderBuffer()
Definition: inputmessage.h:74
InputMessage::fillBuffer
void fillBuffer(uint8 *buffer, uint16 size)
Definition: inputmessage.cpp:103
InputMessage::getDataBuffer
uint8 * getDataBuffer()
Definition: inputmessage.h:75
InputMessage::getReadSize
int getReadSize()
Definition: inputmessage.h:59
InputMessage::readSize
uint16 readSize()
Definition: inputmessage.h:78
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
InputMessage::readChecksum
bool readChecksum()
Definition: inputmessage.cpp:117
InputMessage::BUFFER_MAXSIZE
@ BUFFER_MAXSIZE
Definition: inputmessage.h:34
uint8
uint8_t uint8
Definition: types.h:37
InputMessage::getMessageSize
uint16 getMessageSize()
Definition: inputmessage.h:62