Exemplo n.º 1
0
        /// <summary>
        /// Handle packet
        /// </summary>
        /// <param name="client">Client connection</param>
        /// <param name="packetBody">packet data</param>
        public static void Process(ClientConnection client, byte[] packetBody)
        {
            if (packetBody[0] == 1) //if crypto flag is 1, decrypt
            {
                client.Session.Transform(ref packetBody);
            }

            using (var stream = new MemoryStream(packetBody))
                using (var reader = new BinaryReader(stream))
                {
                    var isCrypt  = reader.ReadBoolean(); // need just read for move position, we decrypt before
                    var sequence = reader.ReadInt16();
                    var opCode   = reader.ReadInt16();
                    var body     = reader.ReadBytes(packetBody.Length - 5); // without crypt flag, sequence and opCode length

                    client.SequenceId = sequence;                           // install sequence id

                    lock (RLock)
                        RecvEvent?.Invoke(opCode, body, isCrypt);

                    if (ClientFrames.ContainsKey(opCode))                                                         // check, if packet exist
                    {
                        ((APacketProcessor)Activator.CreateInstance(ClientFrames[opCode])).Process(client, body); // process packet
                    }
                    else if (CfgCore.Default.LogUnkPackets)
                    {
                        Console.WriteLine($"Unknown packet\nOpCode {opCode}\nData:\n {body.FormatHex()}"); // if packet not exist, we cannot proccess hem, just write log
                    }
                }
        }
 public static void OnRecvEvent(string message)
 {
     RecvEvent?.Invoke(message);
 }