/// <summary> /// Encrypts the packet then send it to the client. /// </summary> /// <param name="input">The byte array to be sent.</param> public void SendPacket(byte[] input) { byte[] cryptData = input; byte[] sendData = new byte[cryptData.Length + 4]; byte[] header = _type == SessionType.SERVER_TO_CLIENT ? _SIV.GetHeaderToClient(cryptData.Length) : _SIV.GetHeaderToServer(cryptData.Length); MapleCustomEncryption.Encrypt(cryptData); _SIV.Crypt(cryptData); System.Buffer.BlockCopy(header, 0, sendData, 0, 4); System.Buffer.BlockCopy(cryptData, 0, sendData, 4, cryptData.Length); SendRawPacket(sendData); }
/// <summary> /// Data received event handler /// </summary> /// <param name="iar">IAsyncResult of the data received event</param> private void OnDataReceived(IAsyncResult iar) { SocketInfo socketInfo = (SocketInfo)iar.AsyncState; try { int received = socketInfo.Socket.EndReceive(iar); if (received == 0) { if (OnClientDisconnected != null) { OnClientDisconnected(this); } return; } socketInfo.Index += received; if (socketInfo.Index == socketInfo.DataBuffer.Length) { switch (socketInfo.State) { case SocketInfo.StateEnum.Header: if (socketInfo.NoEncryption) { PacketReader headerReader = new PacketReader(socketInfo.DataBuffer); short packetHeader = headerReader.ReadShort(); socketInfo.State = SocketInfo.StateEnum.Content; socketInfo.DataBuffer = new byte[packetHeader]; socketInfo.Index = 0; WaitForData(socketInfo); } else { PacketReader headerReader = new PacketReader(socketInfo.DataBuffer); byte[] packetHeaderB = headerReader.ToArray(); int packetHeader = headerReader.ReadInt(); short packetLength = (short)MapleCrypto.GetPacketLength(packetHeader); if (_type == SessionType.SERVER_TO_CLIENT && !_RIV.CheckPacketToServer(BitConverter.GetBytes(packetHeader))) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Packet check failed. Disconnecting client."); //this.Socket.Close(); } socketInfo.State = SocketInfo.StateEnum.Content; socketInfo.DataBuffer = new byte[packetLength]; socketInfo.Index = 0; WaitForData(socketInfo); } break; case SocketInfo.StateEnum.Content: byte[] data = socketInfo.DataBuffer; if (socketInfo.NoEncryption) { socketInfo.NoEncryption = false; PacketReader reader = new PacketReader(data); short version = reader.ReadShort(); string unknown = reader.ReadMapleString(); _SIV = new MapleCrypto(reader.ReadBytes(4), version); _RIV = new MapleCrypto(reader.ReadBytes(4), version); byte serverType = reader.ReadByte(); if (_type == SessionType.CLIENT_TO_SERVER) { OnInitPacketReceived(version, serverType); } OnPacketReceived(new PacketReader(data), true); WaitForData(); } else { _RIV.Crypt(data); MapleCustomEncryption.Decrypt(data); if (data.Length != 0 && OnPacketReceived != null) { OnPacketReceived(new PacketReader(data), false); } WaitForData(); } break; } } else { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Warning] Not enough data"); WaitForData(socketInfo); } } catch (ObjectDisposedException) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: Socket has been closed"); } catch (SocketException se) { if (se.ErrorCode != 10054) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + se); } } catch (Exception e) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + e); } }