/// <summary> /// Waits for more data to arrive /// </summary> /// <param name="pSocketInfo">Info about data to be received</param> private void WaitForData(SocketInfo pSocketInfo) { try { mSocket.BeginReceive(pSocketInfo.DataBuffer, pSocketInfo.Index, pSocketInfo.DataBuffer.Length - pSocketInfo.Index, SocketFlags.None, new AsyncCallback(OnDataReceived), pSocketInfo); } catch (Exception se) { Console.WriteLine("[Error] Session.WaitForData: " + se); } }
/// <summary> /// Waits for more data to arrive /// </summary> /// <param name="socketInfo">Info about data to be received</param> private void WaitForData(SocketInfo socketInfo) { try { Socket.BeginReceive(socketInfo.DataBuffer, socketInfo.Index, socketInfo.DataBuffer.Length - socketInfo.Index, SocketFlags.None, OnDataReceived, socketInfo); } catch (Exception se) { Log.LogError("Session.WaitForData", se); } }
/// <summary> /// Waits for more data to arrive /// </summary> /// <param name="socketInfo">Info about data to be received</param> private void WaitForData(SocketInfo socketInfo) { try { _socket.BeginReceive(socketInfo.DataBuffer, socketInfo.Index, socketInfo.DataBuffer.Length - socketInfo.Index, SocketFlags.None, new AsyncCallback(OnDataReceived), socketInfo); } catch (Exception se) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.WaitForData: " + se); //Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.WaitForData: " + se); } }
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; byte[] dataa = new byte[received]; Buffer.BlockCopy(socketInfo.DataBuffer, 0, dataa, 0, received); if (OnPacketReceived != null) { OnPacketReceived.Invoke(new PacketReader(dataa)); } //Console.WriteLine(BitConverter.ToString(dataa)); //Console.WriteLine(HexEncoding.ToStringFromAscii(dataa)); WaitForData(); /*if (socketInfo.Index == socketInfo.DataBuffer.Length) { * switch (socketInfo.State) { * case SocketInfo.StateEnum.Header: * PacketReader headerReader = new PacketReader(socketInfo.DataBuffer); * byte[] packetHeaderB = headerReader.ToArray(); * int packetHeader = headerReader.ReadInt(); * short packetLength = (short)MapleCrypto.getPacketLength(packetHeader); * if (!_RIV.checkPacket(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; * * _RIV.crypt(data); * MapleCustomEncryption.Decrypt(data); * * if (data.Length != 0 && OnPacketReceived != null) { * OnPacketReceived(new PacketReader(data)); * } * 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"); //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); //Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + se); } } catch (Exception e) { Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + e); //Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + e); } }
/// <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); } }