Esempio n. 1
0
        public void SendPacket(byte[] rawPacket)
        {
            Logger.Warn($"[{_clientId}] Outgoing packet:{Environment.NewLine}" +
                        $"Packet Length: {rawPacket.Length}{Environment.NewLine}" +
                        $"Packet ID: {EncodeHelper.DecodeShort(new []{rawPacket[6], rawPacket[7]}, true)}{Environment.NewLine}" +
                        $"Packet Hex dump:{Environment.NewLine}" +
                        $"{HexUtils.HexDump(rawPacket)}");

            _socket.Send(rawPacket);
        }
Esempio n. 2
0
        private void ProcessorOnPacketProcessed(object sender, PacketProcessedEventArgs e)
        {
            var payload = e.ClientPacket.Payload;

            //Logger.Debug("Received packet");
            Logger.Debug($"Received packet:\n" +
                         $"\t{"Id",-9}: {payload.Opcode} ({(int)payload.Opcode})\n" +
                         $"\t{"Length",-9}: {payload.Length}\n" +
                         $"\t{"Packet type",-9}: {payload.PacketType} \n" +
                         $"\t{"Hex dump",-9}:\n" +
                         $"{HexUtils.HexDump(payload.Payload)}");
        }
Esempio n. 3
0
        public void Send(ServerPacket packet)
        {
            var rawPacket = packet.ToArray();

            // Debug to console.
            Logger.Fatal($"Sending packet:\n" +
                         $"\t{"Id",-9}: {packet.Opcode}\n" +
                         $"\t{"Length",-9}: {packet.Length}\n" +
                         $"\t{"Hex dump",-9}:\n" +
                         $"{HexUtils.HexDump(packet.PacketRaw)}");
            // Send to client.
            Socket.Send(rawPacket);

            // Clean-up packet resources.
            packet.Dispose();
        }
Esempio n. 4
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                var byteCount = _socket.EndReceive(ar);
                if (byteCount == 0)
                {
                    _socket.Close();
                    _socket.Dispose();

                    Logger.Debug($"[{_clientId}] Connection from {_remoteEndPoint.Address} was dropped.");
                    return;
                }

                var packet = new byte[byteCount];
                Buffer.BlockCopy(_buffer, 0, packet, 0, byteCount);

                /*  Packet 502 - RsLoginFail
                 *
                 *  new Packet(502)
                 *      .Append(EncodeHelper.CreatePadding(4))
                 *      .Append(EncodeHelper.EncodeShort(1101, true));
                 */

                /*  Packet 602 - LsLoginFail
                 *
                 *  new Packet(602)
                 *      .Append(EncodeHelper.CreatePadding(4))
                 *      .Append(EncodeHelper.EncodeInteger(819, true))                      // Custom error
                 *      .Append(Encoding.Unicode.GetBytes("Some kind of custom error"));    // Custom error
                 */

                var packetLength = EncodeHelper.DecodeShort(new[] { packet[1], packet[0] });
                var packetHeader = packet[2]; // Not sure :)

                Logger.Trace($"[{_clientId}] Incoming packet:{Environment.NewLine}" +
                             $"Packet Length Raw: {packet.Length}{Environment.NewLine}" +
                             $"Packet Length: {packetLength}{Environment.NewLine}" +
                             $"Packet ID: {packetHeader}{Environment.NewLine}" +
                             $"Packet Hex:{Environment.NewLine}" +
                             $"{HexUtils.HexDump(packet)}");

                var packetList = new List <byte[]>();
                switch (packetHeader)
                {
                case 255:
                {
                    // Connect to port 8005??
                    packetList.Add(new Packet(501)
                                   .Append(EncodeHelper.CreatePadding(4))
                                   .Append(0x74)
                                   .Append(0x0)
                                   .Append(0x0)
                                   .Append(0x1)
                                   .GetPacket());

                    break;
                }

                case 5:
                {
                    packetList.Add(new Packet(601)
                                   .Append(EncodeHelper.CreatePadding(4))
                                   .Append(Encoding.ASCII.GetBytes("Cool"))
                                   .Append(EncodeHelper.CreatePadding(42))
                                   .Append(Encoding.Unicode.GetBytes("Test"))
                                   .GetPacket());
                    break;
                }

                case 3:
                {
                    packetList.Add(new Packet(2).GetPacket());
                    break;
                }

                default:
                    Logger.Error($"[{_clientId}] Incoming packet was not known.");
                    break;
                }

                if (packetList.Count > 0)
                {
                    foreach (var outgoingPacket in packetList)
                    {
                        SendPacket(outgoingPacket);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $"[{_clientId}] An exception occured during receive.");
            }
            finally
            {
                WaitForData();
            }
        }