/// <summary> /// Parses the handshake from the received data /// </summary> private int ParseHandshake() { int offset = 0; ushort length = BitConverter.ToUInt16(DataBuffer, 0); offset = (HandshakeHeaderLength + length); byte[] handshakeData = new ArraySegment <byte>(DataBuffer, HandshakeHeaderLength, length).ToArray(); PacketReader reader = new PacketReader(ref handshakeData, IncomingPacketType.NoHeader); ushort version = reader.ReadUInt16(); ushort subversion = Convert.ToUInt16(reader.ReadMapleString()); uint localVector = reader.ReadUInt32(); uint remoteVector = reader.ReadUInt32(); byte locale = reader.ReadInt8(); byte isLoginContext = 255; if (reader.AbsoluteLength >= 15) //Decode CRC 'n shit { isLoginContext = reader.ReadInt8(); } LocalCipher.Initialize(localVector, version); RemoteCipher.Initialize(remoteVector, version); HandshakeReceived?.Invoke(version, subversion, locale, isLoginContext); Handshaken = true; return(offset); }
/// <summary> /// Extracts packets from the DataBuffer /// </summary> private void ProcessRawData(int received) { int total = received + BufferOffset; int offset = 0; if (!Handshaken) { offset += ParseHandshake(); } while (total - offset > RegularHeaderLength) { int length = GetPacketLength(DataBuffer, offset); if ((total - offset) < length) { byte[] leftover = new ArraySegment <byte>(DataBuffer, offset, (total - offset)).ToArray(); Buffer.BlockCopy(leftover, 0, DataBuffer, 0, leftover.Length); BufferOffset = leftover.Length; return; } offset += RegularHeaderLength; byte[] packetData = new ArraySegment <byte>(DataBuffer, offset, length).ToArray(); RemoteCipher.Decrypt(ref packetData); offset += length; PacketReceived?.Invoke(new PacketReader(ref packetData, IncomingPacketType.DynamicHeader)); } }