/// <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> /// Prepares a packet to be sent and sends it onto the Socket /// </summary> public void SendPacket(PacketWriter packet, bool reusePacket) { if (!Connected) { //TODO: Notify main thread of possible error return; } if (!reusePacket) { WritePacketLength(ref packet._DataBuffer, packet.Position); LocalCipher.Encrypt(ref packet._DataBuffer, packet.Position); Send(ref packet._DataBuffer, packet.Position); } else { byte[] data = packet; WritePacketLength(ref data, data.Length); LocalCipher.Encrypt(ref data, data.Length); Send(ref data, data.Length); } }