Skip to content

Commit

Permalink
+ packetlogger
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 21, 2023
1 parent 50d129c commit c9a6ff7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 74 deletions.
3 changes: 1 addition & 2 deletions src/ClassicUO.Client/CUOEnviroment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ internal static class CUOEnviroment
public static uint CurrentRefreshRate;
public static bool SkipLoginScreen;
public static bool IsOutlands;
public static bool PacketLog;
public static bool NoServerPing;

public static readonly bool IsUnix = Environment.OSVersion.Platform != PlatformID.Win32NT && Environment.OSVersion.Platform != PlatformID.Win32Windows && Environment.OSVersion.Platform != PlatformID.Win32S && Environment.OSVersion.Platform != PlatformID.WinCE;

public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version;
public static readonly string ExecutablePath =
#if NETFRAMEWORK
Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
#else
Environment.CurrentDirectory;
#endif
Expand Down
26 changes: 13 additions & 13 deletions src/ClassicUO.Client/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@

#endregion

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using ClassicUO.Configuration;
using ClassicUO.Game;
using ClassicUO.Game.Managers;
using ClassicUO.IO;
using ClassicUO.Network;
using ClassicUO.Resources;
using ClassicUO.Utility;
using ClassicUO.Utility.Logging;
using ClassicUO.Utility.Platforms;
using SDL2;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace ClassicUO
{
Expand Down Expand Up @@ -164,7 +163,7 @@ public static void Main(string[] args)
if (string.IsNullOrWhiteSpace(Settings.GlobalSettings.Language))
{
Log.Warn("cannot read the OS language. Rolled back to ENU");

Settings.GlobalSettings.Language = "ENU";
}

Expand All @@ -173,7 +172,7 @@ public static void Main(string[] args)
catch
{
Log.Warn("cannot read the OS language. Rolled back to ENU");

Settings.GlobalSettings.Language = "ENU";
}
}
Expand Down Expand Up @@ -338,7 +337,7 @@ private static void ReadSettingsFromArgs(string[] args)
break;

case "lastcharactername":
case "lastcharname":
case "lastcharname":
LastCharacterManager.OverrideLastCharacter(value);

break;
Expand All @@ -348,7 +347,7 @@ private static void ReadSettingsFromArgs(string[] args)

break;

case "last_server_name":
case "last_server_name":
Settings.GlobalSettings.LastServerName = value;
break;

Expand Down Expand Up @@ -491,7 +490,8 @@ private static void ReadSettingsFromArgs(string[] args)

case "packetlog":

CUOEnviroment.PacketLog = true;
PacketLogger.Default.Enabled = true;
PacketLogger.Default.CreateFile();

break;

Expand All @@ -509,7 +509,7 @@ private static void ReadSettingsFromArgs(string[] args)
case "ITA": Settings.GlobalSettings.Language = "ITA"; break;
case "CHT": Settings.GlobalSettings.Language = "CHT"; break;
default:

Settings.GlobalSettings.Language = "ENU";
break;

Expand Down
88 changes: 39 additions & 49 deletions src/ClassicUO.Client/Network/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public void Connect(string ip, ushort port)
_socket.Connect(ip, port);
}


public void Disconnect()
{
_isCompressionEnabled = false;
Expand All @@ -230,52 +229,6 @@ public void EnableCompression()
}

public Span<byte> CollectAvailableData()
=> ProcessRecv();

public void Flush()
{
ProcessSend();
Statistics.Update();
}

public void Send(Span<byte> message, bool ignorePlugin = false, bool skipEncryption = false)
{
if (!ignorePlugin && !Plugin.ProcessSendPacket(ref message))
{
return;
}

Send(message, skipEncryption);
}

private void Send(Span<byte> message, bool skipEncryption)
{
if (!IsConnected || message.IsEmpty)
{
return;
}

lock (_sendStream)
{
if (CUOEnviroment.PacketLog)
{
//LogPacket(message, message.Length, true);
}

if (!skipEncryption)
{
EncryptionHelper.Encrypt(!_isCompressionEnabled, message, message, message.Length);
}

//_socket.Send(data, 0, length);
_sendStream.Enqueue(message);

Statistics.TotalBytesSent += (uint)message.Length;
Statistics.TotalPacketsSent++;
}
}

private Span<byte> ProcessRecv()
{
try
{
Expand All @@ -297,7 +250,7 @@ private Span<byte> ProcessRecv()
catch (SocketException ex)
{
Log.Error("socket error when receving:\n" + ex);

Disconnect();
Disconnected?.Invoke(this, ex.SocketErrorCode);
}
Expand All @@ -307,7 +260,7 @@ private Span<byte> ProcessRecv()
{
Log.Error("main exception:\n" + ex);
Log.Error("socket error when receving:\n" + socketEx);

Disconnect();
Disconnected?.Invoke(this, socketEx.SocketErrorCode);
}
Expand All @@ -325,6 +278,43 @@ private Span<byte> ProcessRecv()
return Span<byte>.Empty;
}

public void Flush()
{
ProcessSend();
Statistics.Update();
}

public void Send(Span<byte> message, bool ignorePlugin = false, bool skipEncryption = false)
{
if (!IsConnected || message.IsEmpty)
{
return;
}

if (!ignorePlugin && !Plugin.ProcessSendPacket(ref message))
{
return;
}

if (message.IsEmpty) return;

PacketLogger.Default?.Log(message, true);

if (!skipEncryption)
{
EncryptionHelper.Encrypt(!_isCompressionEnabled, message, message, message.Length);
}

lock (_sendStream)
{
//_socket.Send(data, 0, length);
_sendStream.Enqueue(message);
}

Statistics.TotalBytesSent += (uint)message.Length;
Statistics.TotalPacketsSent++;
}

private void ProcessEncryption(Span<byte> buffer)
{
if (!_isCompressionEnabled) return;
Expand Down
5 changes: 1 addition & 4 deletions src/ClassicUO.Client/Network/PacketHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ private int ParsePackets(CircularBuffer stream, bool allowPlugins)

_ = stream.Dequeue(packetBuffer, 0, packetlength);

if (CUOEnviroment.PacketLog)
{
_packetLogger.Log(packetBuffer.AsSpan(0, packetlength), false);
}
PacketLogger.Default?.Log(packetBuffer.AsSpan(0, packetlength), false);

// TODO: the pluging function should allow Span<byte> or unsafe type only.
// The current one is a bad style decision.
Expand Down
22 changes: 16 additions & 6 deletions src/ClassicUO.Client/Network/PacketLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ namespace ClassicUO.Network
{
sealed class PacketLogger
{
public static PacketLogger Default { get; set; } = new PacketLogger();



private LogFile _logFile;

public bool Enabled { get; set; }



public LogFile CreateFile()
{
return new LogFile(FileSystemHelper.CreateFolderIfNotExists(CUOEnviroment.ExecutablePath, "Logs", "Network"), "packets.log");
_logFile?.Dispose();
return _logFile = new LogFile(FileSystemHelper.CreateFolderIfNotExists(CUOEnviroment.ExecutablePath, "Logs", "Network"), "packets.log");
}

public void Log(Span<byte> message, bool toServer)
=> Log(null, message, toServer);

public void Log(LogFile log, Span<byte> message, bool toServer)
{
if (!Enabled) return;

Span<char> span = stackalloc char[256];
var output = new ValueStringBuilder(span);
{
Expand Down Expand Up @@ -85,9 +95,9 @@ public void Log(LogFile log, Span<byte> message, bool toServer)

var s = output.ToString();

if (log != null)
if (_logFile != null)
{
log.Write(s);
_logFile.Write(s);
}
else
{
Expand Down

0 comments on commit c9a6ff7

Please sign in to comment.