/// <summary> /// The read. /// </summary> /// <param name="client"> /// </param> /// <param name="packet"> /// </param> public static void Read(Client client, byte[] packet) { LogUtil.Debug(DebugInfoDetail.Network, "\r\nReceived:\r\n" + HexOutput.Output(packet)); MemoryStream m_stream = new MemoryStream(packet); BinaryReader m_reader = new BinaryReader(m_stream); // now we should do password check and then send OK or Error // sending OK now m_stream.Position = 8; short userNameLength = IPAddress.NetworkToHostOrder(m_reader.ReadInt16()); string userName = Encoding.ASCII.GetString(m_reader.ReadBytes(userNameLength)); short loginKeyLength = IPAddress.NetworkToHostOrder(m_reader.ReadInt16()); string loginKey = Encoding.ASCII.GetString(m_reader.ReadBytes(loginKeyLength)); LoginEncryption loginEncryption = new LoginEncryption(); if (loginEncryption.IsValidLogin(loginKey, client.ServerSalt, userName)) { client.IsBot = true; byte[] chars = AccountCharacterList.Create(userName); LogUtil.Debug(DebugInfoDetail.Network, "\r\nReceived:\r\n" + HexOutput.Output(chars)); client.Send(chars); } else { byte[] loginerr = LoginError.Create(); client.Send(loginerr); client.Server.DisconnectClient(client); } }
/// <summary> /// </summary> /// <param name="buffer"> /// </param> public void SendCompressed(byte[] buffer) { // We can not be multithreaded here. packet numbers would be jumbled lock (this.locker) { // Discard the packet for now, if we can not write to the stream if (this.netStream.CanWrite) { byte[] pn = BitConverter.GetBytes(this.packetNumber++); buffer[0] = pn[1]; buffer[1] = pn[0]; try { this.zStream.Write(buffer, 0, buffer.Length); this.zStream.Flush(); } catch (Exception e) { LogUtil.Debug(DebugInfoDetail.Error, "Error writing to zStream"); LogUtil.ErrorException(e); this.server.DisconnectClient(this); } } } LogUtil.Debug(DebugInfoDetail.Network, HexOutput.Output(buffer)); }
/// <summary> /// </summary> /// <param name="receiver"> /// </param> /// <param name="messageBody"> /// </param> public void Send(int receiver, MessageBody messageBody) { // TODO: Investigate if reciever is a timestamp var message = new Message { Body = messageBody, Header = new Header { MessageId = BitConverter.ToUInt16(new byte[] { 0xDF, 0xDF }, 0), PacketType = messageBody.PacketType, Unknown = 0x0001, Sender = 0x00000001, Receiver = receiver } }; byte[] buffer = this.messageSerializer.Serialize(message); buffer[0] = BitConverter.GetBytes(this.packetNumber)[0]; buffer[1] = BitConverter.GetBytes(this.packetNumber)[1]; this.packetNumber++; LogUtil.Debug(DebugInfoDetail.Network, "Sent:\r\n" + HexOutput.Output(buffer)); if (buffer.Length % 4 > 0) { Array.Resize(ref buffer, buffer.Length + (4 - (buffer.Length % 4))); } this.Send(buffer); }
/// <summary> /// </summary> /// <param name="buffer"> /// </param> /// <returns> /// </returns> protected bool OnReceive(BufferSegment buffer) { // return false, if header cannot be complete (00FF55AA, <Length of packet>) // Loop if more than one packet frame received while (true) { if (this._remainingLength == 0) { return(true); } if (this._remainingLength < 8) { return(false); } int expectedLength = this.CheckData(buffer); if (expectedLength == -1) { // MALFORMED PACKET RECEIVED !!! LogUtil.Debug(DebugInfoDetail.Error, "Malformed packet received: "); byte[] data = new byte[this._remainingLength]; buffer.SegmentData.CopyTo(data, this._remainingLength); LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(data)); this._remainingLength = 0; this._offset = 0; // Lets clear the buffer and try this again, no need to drop the connection return(true); } if (expectedLength + 8 > this._remainingLength) { return(false); } if (this._remainingLength >= expectedLength + 8) { // Handle packet payload here byte[] dataBytes = new byte[expectedLength]; Array.Copy(buffer.SegmentData, 8 + this._offset, dataBytes, 0, expectedLength); this.DataReceived(dataBytes); } if (expectedLength + 8 <= this._remainingLength) { // If we have received a full packet frame // then move the remaining data to a new buffer (with offset 0) // only adjusting offset and length here // Then do the whole thing again this._remainingLength -= expectedLength + 8; this._offset += expectedLength + 8; } } }
/// <summary> /// </summary> /// <param name="buffer"> /// </param> /// <returns> /// </returns> /// <exception cref="NotImplementedException"> /// </exception> protected override bool OnReceive(BufferSegment buffer) { Message message = null; var packet = new byte[this._remainingLength]; Array.Copy(buffer.SegmentData, packet, this._remainingLength); LogUtil.Debug(DebugInfoDetail.Network, "\r\nReceived: \r\n" + HexOutput.Output(packet)); this._remainingLength = 0; try { message = this.messageSerializer.Deserialize(packet); } catch (Exception) { uint messageNumber = this.GetMessageNumber(packet); this.Server.Warning( this, "Client sent malformed message {0}", messageNumber.ToString(CultureInfo.InvariantCulture)); LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(packet)); return(false); } buffer.IncrementUsage(); if (message == null) { uint messageNumber = this.GetMessageNumber(packet); this.Server.Warning( this, "Client sent unknown message {0}", messageNumber.ToString(CultureInfo.InvariantCulture)); return(false); } // FUUUUUGLY Type wrapperType = typeof(MessageWrapper <>); Type genericWrapperType = wrapperType.MakeGenericType(message.Body.GetType()); object wrapped = Activator.CreateInstance(genericWrapperType); wrapped.GetType().GetProperty("Client").SetValue(wrapped, (IZoneClient)this, null); wrapped.GetType().GetProperty("Message").SetValue(wrapped, message, null); wrapped.GetType().GetProperty("MessageBody").SetValue(wrapped, message.Body, null); this.bus.Publish(wrapped); return(true); }
/// <summary> /// </summary> /// <param name="receiver"> /// </param> /// <param name="packetBytes"> /// </param> public void Send(int receiver, byte[] packetBytes) { packetBytes[0] = BitConverter.GetBytes(this.packetNumber)[0]; packetBytes[1] = BitConverter.GetBytes(this.packetNumber)[1]; this.packetNumber++; LogUtil.Debug(DebugInfoDetail.Network, "\r\nSent:\r\n" + HexOutput.Output(packetBytes)); if (packetBytes.Length % 4 > 0) { Array.Resize(ref packetBytes, packetBytes.Length + (4 - (packetBytes.Length % 4))); } this.Send(packetBytes); }
/// <summary> /// </summary> /// <param name="buffer"> /// </param> /// <returns> /// </returns> protected override bool OnReceive(BufferSegment buffer) { if (this._remainingLength > 4) { byte[] packet = new byte[this._remainingLength]; Array.Copy(buffer.SegmentData, 0, packet, 0, this._remainingLength); LogUtil.Debug(DebugInfoDetail.Network, "\r\nReceived:\r\n" + HexOutput.Output(packet)); ushort messageNumber = this.GetMessageNumber(packet); Parser parser = new Parser(); return(parser.Parse(this, packet, messageNumber)); } // TODO: check what needs to be done if no suitable packet was found return(true); }
/// <summary> /// </summary> /// <param name="messageBody"> /// </param> public void SendInitiateCompressionMessage(MessageBody messageBody) { // TODO: Investigate if reciever is a timestamp var message = new Message { Body = messageBody, Header = new Header { MessageId = 0xdfdf, PacketType = messageBody.PacketType, Unknown = 0x0001, // TODO: Make compression choosable in config.xml Sender = 0x01000000, // 01000000 = uncompressed, 03000000 = compressed Receiver = 0 // this.character.Identity.Instance } }; byte[] buffer = this.messageSerializer.Serialize(message); LogUtil.Debug(DebugInfoDetail.Network, HexOutput.Output(buffer)); this.packetNumber = 1; this.Send(buffer); // Now create the compressed stream try { if (!this.zStreamSetup) { // CreateIM the zStream this.netStream = new NetworkStream(this.TcpSocket); this.zStream = new ZlibStream(this.netStream, CompressionMode.Compress, CompressionLevel.BestSpeed); this.zStream.FlushMode = FlushType.Sync; this.zStreamSetup = true; } } catch (Exception e) { LogUtil.ErrorException(e); } }
/// <summary> /// </summary> /// <param name="buffer"> /// </param> /// <returns> /// </returns> protected override bool OnReceive(BufferSegment buffer) { Message message = null; var packet = new byte[this._remainingLength]; Array.Copy(buffer.SegmentData, packet, this._remainingLength); LogUtil.Debug( DebugInfoDetail.Network, "Offset: " + buffer.Offset.ToString() + " -- RemainingLength: " + this._remainingLength); LogUtil.Debug(DebugInfoDetail.Network, HexOutput.Output(packet)); this._remainingLength = 0; try { message = this.messageSerializer.Deserialize(packet); } catch (Exception) { uint messageNumber = this.GetMessageNumber(packet); this.Server.Warning( this, "Client sent malformed message {0}", messageNumber.ToString(CultureInfo.InvariantCulture)); LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(packet)); return(false); } buffer.IncrementUsage(); if (message == null) { uint messageNumber = this.GetMessageNumber(packet); this.Server.Warning( this, "Client sent unknown message {0}", messageNumber.ToString(CultureInfo.InvariantCulture)); return(false); } this.bus.Publish(new MessageReceivedEvent(this, message)); return(true); }
/// <summary> /// The parse args. /// </summary> /// <param name="funcNum"> /// The func num. /// </param> /// <param name="R"> /// The r. /// </param> /// <returns> /// The <see cref="object[]"/>. /// </returns> /// <exception cref="IndexOutOfRangeException"> /// </exception> private object[] ParseArgs(int funcNum, ref bool R) { bool flag = !this.FunctionSets.ContainsKey(funcNum.ToString()); if (flag) { TextWriter lastitem = new StreamWriter("lastitem.txt"); lastitem.WriteLine(HexOutput.Output(this.br.Buffer)); lastitem.Close(); throw new IndexOutOfRangeException("Not handled function " + funcNum.ToString()); } string[] array = this.FunctionSets[funcNum.ToString()].Split(','); List <object> list = new List <object>(); string[] array2 = array; checked { for (int i = 0; i < array2.Length; i++) { string str = array2[i]; int num = int.Parse(str.Trim().Substring(0, str.Length - 1)); string text = str.Trim().ToLower().Substring(str.Length - 1, 1); // Strings.LCase(Strings.Right(Strings.Trim(str), 1)); string left = text; flag = left == "n"; if (flag) { int arg_A6_0 = 1; int num2 = num; int num3 = arg_A6_0; while (true) { int arg_D9_0 = num3; int num4 = num2; if (arg_D9_0 > num4) { break; } int value = this.br.ReadInt32(); list.Add(value); num3++; } } else { flag = left == "h"; if (flag) { int arg_FD_0 = 1; int num5 = num; int num6 = arg_FD_0; while (true) { int arg_12B_0 = num6; int num4 = num5; if (arg_12B_0 > num4) { break; } string item = this.br.ReadHash(); list.Add(item); num6++; } } else { flag = left == "s"; if (flag) { int arg_14F_0 = 1; int num7 = num; int num8 = arg_14F_0; while (true) { int arg_1B5_0 = num8; int num4 = num7; if (arg_1B5_0 > num4) { break; } string item2 = string.Empty; int num9 = this.br.ReadInt32() - 1; flag = num9 > 0; if (flag) { item2 = this.br.ReadString(); } this.br.Skip(1); list.Add(item2); num8++; } } else { flag = left == "x"; if (flag) { this.br.Skip(num); } else { R = true; } } } } } return(list.ToArray()); } }