2014-06-19 22:49:03 +01:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using OpenMetaverse.Packets;
|
|
|
|
|
|
|
|
|
|
namespace OpenMetaverse.TestClient
|
|
|
|
|
{
|
|
|
|
|
public class PacketLogCommand : Command
|
|
|
|
|
{
|
|
|
|
|
private TestClient m_client;
|
|
|
|
|
private bool m_isLogging;
|
|
|
|
|
private int m_packetsToLogRemaining;
|
|
|
|
|
private StreamWriter m_logStreamWriter;
|
|
|
|
|
|
|
|
|
|
public PacketLogCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "logpacket";
|
|
|
|
|
Description = "Logs a given number of packets to a file. For example, packetlog 10 tenpackets.xml";
|
|
|
|
|
Category = CommandCategory.TestClient;
|
|
|
|
|
|
|
|
|
|
m_client = testClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Execute(string[] args, UUID fromAgentID)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length != 2)
|
2022-02-25 19:38:11 -06:00
|
|
|
return $"Usage: {Name} no-of-packets filename";
|
2014-06-19 22:49:03 +01:00
|
|
|
|
|
|
|
|
string rawNumberOfPackets = args[0];
|
|
|
|
|
string path = args[1];
|
|
|
|
|
int numberOfPackets;
|
|
|
|
|
|
|
|
|
|
if (!int.TryParse(args[0], out numberOfPackets) || numberOfPackets <= 0)
|
2022-02-25 19:38:11 -06:00
|
|
|
return $"{rawNumberOfPackets} is not a valid number of packets for {m_client.Self.Name}";
|
2014-06-19 22:49:03 +01:00
|
|
|
|
2014-06-19 23:23:16 +01:00
|
|
|
lock (this)
|
2014-06-19 22:49:03 +01:00
|
|
|
{
|
2014-06-19 23:23:16 +01:00
|
|
|
if (m_isLogging)
|
2022-02-25 19:38:11 -06:00
|
|
|
return
|
|
|
|
|
$"Still waiting to finish logging {m_packetsToLogRemaining} packets for {m_client.Self.Name}";
|
2014-06-19 23:23:16 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-19 22:49:03 +01:00
|
|
|
m_logStreamWriter = new StreamWriter(path);
|
2014-06-19 23:23:16 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-25 19:38:11 -06:00
|
|
|
return $"Could not open file with path [{path}], exception {e}";
|
2014-06-19 23:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_isLogging = true;
|
2014-06-19 22:49:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_packetsToLogRemaining = numberOfPackets;
|
|
|
|
|
m_client.Network.RegisterCallback(PacketType.Default, HandlePacket);
|
2022-02-25 19:38:11 -06:00
|
|
|
return $"Now logging {m_packetsToLogRemaining} packets for {m_client.Self.Name}";
|
2014-06-19 22:49:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logs the packet received for this client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This handler assumes that packets are processed one at a time.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="sender">Sender.</param>
|
|
|
|
|
/// <param name="args">Arguments.</param>
|
|
|
|
|
private void HandlePacket(object sender, PacketReceivedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
// Console.WriteLine(
|
|
|
|
|
// "Received packet {0} from {1} for {2}", args.Packet.Type, args.Simulator.Name, m_client.Self.Name);
|
2014-06-19 23:03:01 +01:00
|
|
|
|
2014-06-19 23:23:16 +01:00
|
|
|
lock (this)
|
2014-06-19 22:49:03 +01:00
|
|
|
{
|
2014-06-19 23:23:16 +01:00
|
|
|
if (!m_isLogging)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-06-08 17:58:54 -05:00
|
|
|
m_logStreamWriter.WriteLine("Received: {0:yyyy-MM-dd hh:mm:ss.fff}", DateTime.Now);
|
2014-06-19 23:23:16 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_logStreamWriter.WriteLine(PacketDecoder.PacketToString(args.Packet));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
m_logStreamWriter.WriteLine("Failed to write decode of {0}, exception {1}", args.Packet.Type, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (--m_packetsToLogRemaining <= 0)
|
|
|
|
|
{
|
|
|
|
|
m_client.Network.UnregisterCallback(PacketType.Default, HandlePacket);
|
|
|
|
|
m_logStreamWriter.Close();
|
|
|
|
|
Console.WriteLine("Finished logging packets for {0}", m_client.Self.Name);
|
|
|
|
|
m_isLogging = false;
|
|
|
|
|
}
|
2014-06-19 22:49:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|