aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ExportBot/Commands/PacketLogCommand.cs
blob: 827b147d634398cd3adeba51e4d60ffd70fa696c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Xml;
using libsecondlife;
using libsecondlife.Packets;

namespace libsecondlife.TestClient
{
    public class PacketLogCommand : Command
    {
        List<Packet> Packets = new List<Packet>();
        bool Done = false;
        int Count = 0;
        int Total = 0;

        public PacketLogCommand(TestClient testClient)
        {
            Name = "packetlog";
            Description = "Logs a given number of packets to an xml file. Usage: packetlog 10 tenpackets.xml";
        }

        public override string Execute(string[] args, LLUUID fromAgentID)
        {
            if (args.Length != 2)
                return "Usage: packetlog 10 tenpackets.xml";

            XmlWriter writer;
            NetworkManager.PacketCallback callback = new NetworkManager.PacketCallback(OnPacket);

            Packets.Clear();
            Done = false;
            Count = 0;

            try
            {
                Total = Int32.Parse(args[0]);
                writer = XmlWriter.Create(args[1]);

                Client.Network.RegisterCallback(PacketType.Default, callback);
            }
            catch (Exception e)
            {
                return "Usage: packetlog 10 tenpackets.xml (" + e + ")";
            }

            while (!Done)
            {
                System.Threading.Thread.Sleep(100);
            }

            Client.Network.UnregisterCallback(PacketType.Default, callback);

            try
            {
                Helpers.PacketListToXml(Packets, writer);
            }
            catch (Exception e)
            {
                return "Serialization failed: " + e.ToString();
            }

            writer.Close();
            Packets.Clear();

            return "Exported " + Count + " packets to " + args[1];
        }

        private void OnPacket(Packet packet, Simulator simulator)
        {
            lock (Packets)
            {
                if (Count >= Total)
                {
                    Done = true;
                }
                else
                {
                    Packets.Add(packet);
                    Count++;
                }
            }
        }
    }
}