From 9b84d68d27fb3ce152db04fb9af51e9222f2461c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 3 Nov 2013 11:58:49 +0100 Subject: Protocol 1.7: Rewritten packet-sending to use cPacketizer. Implemented enough of the protocol that the client now spawns in the world (but cannot do anything). --- source/Protocol/Protocol17x.h | 93 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'source/Protocol/Protocol17x.h') diff --git a/source/Protocol/Protocol17x.h b/source/Protocol/Protocol17x.h index f11d8e2f9..4be166814 100644 --- a/source/Protocol/Protocol17x.h +++ b/source/Protocol/Protocol17x.h @@ -95,6 +95,85 @@ public: protected: + /// Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed + class cPacketizer + { + public: + cPacketizer(cProtocol172 & a_Protocol, UInt32 a_PacketType) : + m_Protocol(a_Protocol), + m_Out(a_Protocol.m_OutPacketBuffer), + m_Lock(a_Protocol.m_CSPacket) + { + m_Out.WriteVarInt(a_PacketType); + } + + ~cPacketizer(); + + void WriteBool(bool a_Value) + { + m_Out.WriteBool(a_Value); + } + + void WriteByte(Byte a_Value) + { + m_Out.WriteByte(a_Value); + } + + void WriteChar(char a_Value) + { + m_Out.WriteChar(a_Value); + } + + void WriteShort(short a_Value) + { + m_Out.WriteBEShort(a_Value); + } + + void WriteInt(int a_Value) + { + m_Out.WriteBEInt(a_Value); + } + + void WriteInt64(Int64 a_Value) + { + m_Out.WriteBEInt64(a_Value); + } + + void WriteFloat(float a_Value) + { + m_Out.WriteBEFloat(a_Value); + } + + void WriteDouble(double a_Value) + { + m_Out.WriteBEDouble(a_Value); + } + + void WriteVarInt(UInt32 a_Value) + { + m_Out.WriteVarInt(a_Value); + } + + void WriteString(const AString & a_Value) + { + m_Out.WriteVarUTF8String(a_Value); + } + + void WriteBuf(const char * a_Data, int a_Size) + { + m_Out.Write(a_Data, a_Size); + } + + void WriteItem(const cItem & a_Item); + void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte + void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer + + protected: + cProtocol172 & m_Protocol; + cByteBuffer & m_Out; + cCSLock m_Lock; + } ; + AString m_ServerAddress; UInt16 m_ServerPort; @@ -107,13 +186,16 @@ protected: /// Buffer for the received data cByteBuffer m_ReceivedData; + /// Buffer for composing the outgoing packets, through cPacketizer + cByteBuffer m_OutPacketBuffer; + + /// Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) + cByteBuffer m_OutPacketLenBuffer; + bool m_IsEncrypted; CryptoPP::CFB_Mode::Decryption m_Decryptor; CryptoPP::CFB_Mode::Encryption m_Encryptor; - /// (Unencrypted) data to be sent to the client. Written by SendData, cleared by Flush() - AString m_DataToSend; - /// Adds the received (unencrypted) data to m_ReceivedData, parses complete packets void AddReceivedData(const char * a_Data, int a_Size); @@ -157,12 +239,9 @@ protected: /// Writes an entire packet into the output stream. a_Packet is expected to start with the packet type; data length is prepended here. void WritePacket(cByteBuffer & a_Packet); - /// Adds unencrypted data to the outgoing data buffer + /// Sends the data to the client, encrypting them if needed. virtual void SendData(const char * a_Data, int a_Size) override; - /// Flushes m_DataToSend through the optional encryption into the outgoing socket data - virtual void Flush(void) override; - void SendCompass(const cWorld & a_World); } ; -- cgit v1.2.3