Skip to content

Commit 6365ae1

Browse files
authored
Add Track app to record server packets (jo3bingham#5)
Properly handle object disposed exceptions when stopping the proxy.
1 parent 764bd68 commit 6365ae1

5 files changed

Lines changed: 124 additions & 1 deletion

File tree

Apps/Track/Program.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.IO;
3+
4+
using OXGaming.TibiaAPI;
5+
6+
namespace Track
7+
{
8+
class Program
9+
{
10+
static BinaryWriter _binaryWriter;
11+
12+
static void Main(string[] args)
13+
{
14+
try
15+
{
16+
// Use a default .dat file called Tibia11.dat in the same directory as the Track application,
17+
// otherwise, the user can supply the path to the .dat file on the command line.
18+
var tibiaDatFile = "Tibia11.dat";
19+
if (args.Length == 1)
20+
{
21+
tibiaDatFile = args[0];
22+
}
23+
24+
using (var client = new Client(tibiaDatFile))
25+
{
26+
var dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
27+
if (!Directory.Exists(dataDirectory))
28+
{
29+
Directory.CreateDirectory(dataDirectory);
30+
}
31+
32+
var currentDate = DateTime.UtcNow;
33+
var filename = $"{currentDate.Day}_{currentDate.Month}_{currentDate.Year}__{currentDate.Hour}_{currentDate.Minute}_{currentDate.Second}.dat";
34+
_binaryWriter = new BinaryWriter(File.OpenWrite(Path.Combine(dataDirectory, filename)));
35+
36+
client.Proxy.OnReceivedServerMessage += Proxy_OnReceivedServerMessage;
37+
// Disable packet parsing as we only care about the raw, decrypted packets and speed.
38+
client.StartProxy(enablePacketParsing: false);
39+
40+
while (Console.ReadLine() != "quit")
41+
{
42+
System.Threading.Thread.Sleep(100);
43+
}
44+
45+
client.Proxy.OnReceivedServerMessage -= Proxy_OnReceivedServerMessage;
46+
client.StopProxy();
47+
48+
// Give the proxy time to quit, and any pending packets to be consumed.
49+
System.Threading.Thread.Sleep(1000);
50+
51+
_binaryWriter.Flush();
52+
_binaryWriter.Close();
53+
_binaryWriter.Dispose();
54+
}
55+
}
56+
catch (Exception ex)
57+
{
58+
Console.WriteLine(ex);
59+
Console.Read();
60+
}
61+
finally
62+
{
63+
Console.Read();
64+
}
65+
}
66+
67+
private static void Proxy_OnReceivedServerMessage(byte[] data)
68+
{
69+
try
70+
{
71+
_binaryWriter.Write(data.Length);
72+
_binaryWriter.Write(data);
73+
_binaryWriter.Flush();
74+
}
75+
catch (Exception ex)
76+
{
77+
Console.WriteLine(ex);
78+
}
79+
}
80+
}
81+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"profiles": {
3+
"Track": {
4+
"commandName": "Project"
5+
}
6+
}
7+
}

Apps/Track/Track.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\TibiaAPI\TibiaAPI.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

TibiaAPI.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.28010.2003
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TibiaAPI", "TibiaAPI\TibiaAPI.csproj", "{FEDE2A7B-8203-443E-9CF7-97A562AABB92}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{C086BFA7-CE77-4DDF-9474-C65709DBA8A8}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Track", "Apps\Track\Track.csproj", "{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -15,10 +19,17 @@ Global
1519
{FEDE2A7B-8203-443E-9CF7-97A562AABB92}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{FEDE2A7B-8203-443E-9CF7-97A562AABB92}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{FEDE2A7B-8203-443E-9CF7-97A562AABB92}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6}.Release|Any CPU.Build.0 = Release|Any CPU
1826
EndGlobalSection
1927
GlobalSection(SolutionProperties) = preSolution
2028
HideSolutionNode = FALSE
2129
EndGlobalSection
30+
GlobalSection(NestedProjects) = preSolution
31+
{CFE7894A-D5A4-4C16-B2B4-51915BE0D4F6} = {C086BFA7-CE77-4DDF-9474-C65709DBA8A8}
32+
EndGlobalSection
2233
GlobalSection(ExtensibilityGlobals) = postSolution
2334
SolutionGuid = {779085CF-5BE0-48BA-A204-511A8914A67C}
2435
EndGlobalSection

TibiaAPI/Network/Connection.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class Connection : Communication, IDisposable
6060
private bool _isSendingToServer = false;
6161
private bool _isStarted;
6262

63-
public delegate bool ReceivedMessageEventHandler(byte[] data);
63+
public delegate void ReceivedMessageEventHandler(byte[] data);
6464

6565
public event ReceivedMessageEventHandler OnReceivedClientMessage;
6666
public event ReceivedMessageEventHandler OnReceivedServerMessage;
@@ -555,6 +555,10 @@ private void BeginGetContextCallback(IAsyncResult ar)
555555

556556
_httpListener.BeginGetContext(new AsyncCallback(BeginGetContextCallback), _httpListener);
557557
}
558+
catch (ObjectDisposedException)
559+
{
560+
// This exception can occur if Stop() is called.
561+
}
558562
catch (Exception ex)
559563
{
560564
Console.WriteLine(ex.ToString());
@@ -584,6 +588,10 @@ private void BeginAcceptTcpClientCallback(IAsyncResult ar)
584588

585589
_tcpListener.BeginAcceptSocket(new AsyncCallback(BeginAcceptTcpClientCallback), _tcpListener);
586590
}
591+
catch (ObjectDisposedException)
592+
{
593+
// This exception can occur if Stop() is called.
594+
}
587595
catch (Exception ex)
588596
{
589597
Console.WriteLine(ex.ToString());
@@ -808,6 +816,10 @@ private void BeginReceiveServerCallback(IAsyncResult ar)
808816

809817
_serverSocket.BeginReceive(_serverInMessage.GetBuffer(), 0, 2, SocketFlags.None, new AsyncCallback(BeginReceiveServerCallback), 1);
810818
}
819+
catch (ObjectDisposedException)
820+
{
821+
// This exception can occur if Stop() is called.
822+
}
811823
catch (SocketException)
812824
{
813825
// This exception can happen if the server, forcefully, closes the connection.

0 commit comments

Comments
 (0)