Skip to content

Commit 1c0ee7f

Browse files
authored
Add Client class. (jo3bingham#3)
* Add Client class. Mark Network classes as internal as they should only be accessed by the Client class, or other internal classes. Removed unused variable from RSA class. * Add code analyzers and fix warnings
1 parent 9ecaa8a commit 1c0ee7f

File tree

8 files changed

+113
-40
lines changed

8 files changed

+113
-40
lines changed

TibiaAPI/Client.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
3+
namespace OXGaming.TibiaAPI
4+
{
5+
public class Client : IDisposable
6+
{
7+
private Network.Connection _connection;
8+
9+
public bool StartProxy()
10+
{
11+
if (_connection == null)
12+
{
13+
_connection = new Network.Connection();
14+
}
15+
16+
return _connection.Start();
17+
}
18+
19+
public void StopProxy()
20+
{
21+
if (_connection != null)
22+
{
23+
_connection.Stop();
24+
}
25+
}
26+
27+
#region IDisposable Support
28+
private bool disposedValue = false;
29+
30+
protected virtual void Dispose(bool disposing)
31+
{
32+
if (!disposedValue)
33+
{
34+
if (disposing)
35+
{
36+
if (_connection != null)
37+
{
38+
_connection.Dispose();
39+
}
40+
}
41+
42+
disposedValue = true;
43+
}
44+
}
45+
46+
~Client()
47+
{
48+
Dispose(false);
49+
}
50+
51+
/// <summary>
52+
/// Releases all the managed resources used by the <see cref="Client"/>.
53+
/// </summary>
54+
public void Dispose()
55+
{
56+
Dispose(true);
57+
GC.SuppressFinalize(this);
58+
}
59+
#endregion
60+
}
61+
}

TibiaAPI/GlobalSuppressions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
// This file is used by Code Analysis to maintain SuppressMessage
3+
// attributes that are applied to this project.
4+
// Project-level suppressions either have no target or are given
5+
// a specific target and scoped to a namespace, type, member, etc.
6+
7+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "<Pending>", Scope = "type", Target = "~T:OXGaming.TibiaAPI.Network.LoginData")]
8+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "<Pending>", Scope = "type", Target = "~T:OXGaming.TibiaAPI.Network.Session")]
9+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "<Pending>", Scope = "type", Target = "~T:OXGaming.TibiaAPI.Network.PlayData")]
10+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "<Pending>", Scope = "type", Target = "~T:OXGaming.TibiaAPI.Network.World")]
11+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "<Pending>", Scope = "type", Target = "~T:OXGaming.TibiaAPI.Network.Character")]
12+

TibiaAPI/Network/Connection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace OXGaming.TibiaAPI.Network
1616
/// <summary>
1717
/// The <see cref="Connection"/> class is used to create a proxy between the Tibia client and the game server.
1818
/// </summary>
19-
public class Connection : IDisposable
19+
internal class Connection : IDisposable
2020
{
2121
private readonly object _clientSendLock = new object();
2222
private readonly object _serverSendLock = new object();
@@ -153,7 +153,7 @@ public void SendToClient(NetworkMessage message)
153153
{
154154
lock (_clientSendLock)
155155
{
156-
_clientSendQueue.Enqueue(message.Data);
156+
_clientSendQueue.Enqueue(message.GetData());
157157

158158
if (!_isSendingToClient)
159159
{
@@ -186,7 +186,7 @@ public void SendToServer(NetworkMessage message)
186186
{
187187
lock (_serverSendLock)
188188
{
189-
_serverSendQueue.Enqueue(message.Data);
189+
_serverSendQueue.Enqueue(message.GetData());
190190

191191
if (!_isSendingToServer)
192192
{

TibiaAPI/Network/LoginData.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OXGaming.TibiaAPI.Network
88
/// The <see cref="LoginData"/> class is used when deserializing the JSON response
99
/// from CipSoft's web service when the client POSTs a login request.
1010
/// </summary>
11-
public class LoginData
11+
internal class LoginData
1212
{
1313
[JsonProperty("session")]
1414
public Session Session { get; set; }
@@ -19,7 +19,7 @@ public class LoginData
1919
/// <summary>
2020
/// The <see cref="Session"/> class is used to hold relevant data for a given login session.
2121
/// </summary>
22-
public class Session
22+
internal class Session
2323
{
2424
[JsonProperty("sessionkey")]
2525
public string SessionKey { get; set; }
@@ -47,7 +47,7 @@ public class Session
4747
/// The <see cref="PlayData"/> class is used to store the characters, and their game worlds, of the
4848
/// account for a given login session.
4949
/// </summary>
50-
public class PlayData
50+
internal class PlayData
5151
{
5252
[JsonProperty("worlds")]
5353
public List<World> Worlds { get; set; }
@@ -58,7 +58,7 @@ public class PlayData
5858
/// <summary>
5959
/// The <see cref="World"/> class is used to store data (i.e., name, ip, port, etc.) for a game world.
6060
/// </summary>
61-
public class World
61+
internal class World
6262
{
6363
[JsonProperty("id")]
6464
public int Id { get; set; }
@@ -83,7 +83,7 @@ public class World
8383
/// <summary>
8484
/// The <see cref="Character"/> class is used to store data (e.g., name) for a character.
8585
/// </summary>
86-
public class Character
86+
internal class Character
8787
{
8888
[JsonProperty("worldid")]
8989
public int WorldId { get; set; }

TibiaAPI/Network/NetworkMessage.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OXGaming.TibiaAPI.Network
1010
/// <remarks>
1111
/// This is useful for parsing, and creating, Tibia packets.
1212
/// </remarks>
13-
public class NetworkMessage
13+
internal class NetworkMessage
1414
{
1515
/// <summary>
1616
/// The full length of a Tibia packet is stored in two bytes at the beginning of the packet.
@@ -23,23 +23,6 @@ public class NetworkMessage
2323

2424
private readonly byte[] _buffer = new byte[MaxMessageSize];
2525

26-
/// <value>
27-
/// Gets the message data from the underlying buffer.
28-
/// </value>
29-
/// <remarks>
30-
/// Get this only when necessary as it creates a new byte array, copies the valid data from the
31-
/// underlying buffer into it, then returns it.
32-
/// </remarks>
33-
public byte[] Data
34-
{
35-
get
36-
{
37-
var data = new byte[Size];
38-
Array.Copy(_buffer, data, Size);
39-
return data;
40-
}
41-
}
42-
4326
/// <value>
4427
/// Gets the current position in the buffer.
4528
/// </value>
@@ -59,6 +42,20 @@ public byte[] GetBuffer()
5942
return _buffer;
6043
}
6144

45+
/// <value>
46+
/// Gets the message data from the underlying buffer.
47+
/// </value>
48+
/// <remarks>
49+
/// Get this only when necessary as it creates a new byte array, copies the valid data from the
50+
/// underlying buffer into it, then returns it.
51+
/// </remarks>
52+
public byte[] GetData()
53+
{
54+
var data = new byte[Size];
55+
Array.Copy(_buffer, data, Size);
56+
return data;
57+
}
58+
6259
/// <summary>
6360
/// Reads the specified number of bytes from the buffer into an array of bytes
6461
/// and advances the position by that number of bytes.
@@ -80,8 +77,8 @@ public byte[] ReadBytes(uint count)
8077
{
8178
if (count == 0)
8279
{
83-
throw new ArgumentOutOfRangeException("[NetworkMessage.ReadBytes] " +
84-
"'count' must be greater than 0.");
80+
throw new ArgumentOutOfRangeException(nameof(count),
81+
"[NetworkMessage.ReadBytes] 'count' must be greater than 0.");
8582
}
8683

8784
if (Position + count > _buffer.Length)

TibiaAPI/Network/Rsa.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ namespace OXGaming.TibiaAPI.Network
1313
/// Decryption can only be done on data encrypted with the Open-Tibia private key.
1414
/// Encryption can be done on data with both the Open-Tibia and Tibia public keys.
1515
/// </remarks>
16-
public class Rsa
16+
internal class Rsa
1717
{
1818
private const string Exponent = "65537";
1919
private const string OpenTibiaModulus = "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413";
20-
private const string OpenTibiaD = "46730330223584118622160180015036832148732986808519344675210555262940258739805766860224610646919605860206328024326703361630109888417839241959507572247284807035235569619173792292786907845791904955103601652822519121908367187885509270025388641700821735345222087940578381210879116823013776808975766851829020659073";
2120
private const string OpenTibiaP = "14299623962416399520070177382898895550795403345466153217470516082934737582776038882967213386204600674145392845853859217990626450972452084065728686565928113";
2221
private const string OpenTibiaQ = "7630979195970404721891201847792002125535401292779123937207447574596692788513647179235335529307251350570728407373705564708871762033017096809910315212884101";
2322
private const string OpenTibiaDP = "11141736698610418925078406669215087697114858422461871124661098818361832856659225315773346115219673296375487744032858798960485665997181641221483584094519937";
@@ -82,21 +81,21 @@ public Rsa()
8281
/// <param name="message">The <see cref="NetworkMessage"/> containing the block of data to be processed.</param>
8382
/// <param name="index">Index of the underlying <see cref="NetworkMessage"/> buffer to start from.</param>
8483
/// <param name="engine">The RSA engine to use.</param>
85-
private void ProcessBlock(NetworkMessage message, int index, RsaEngine engine)
84+
private static void ProcessBlock(NetworkMessage message, int index, RsaEngine engine)
8685
{
8786
if (message == null)
8887
{
89-
throw new ArgumentNullException("[Rsa.ProcessBlock] 'message' must not be null.");
88+
throw new ArgumentNullException(nameof(message), "[Rsa.ProcessBlock] 'message' must not be null.");
9089
}
9190

9291
if (index < 0)
9392
{
94-
throw new ArgumentOutOfRangeException("[Rsa.ProcessBlock] Index cannot be less than 0.");
93+
throw new ArgumentOutOfRangeException(nameof(index), "[Rsa.ProcessBlock] Index cannot be less than 0.");
9594
}
9695

9796
if (engine == null)
9897
{
99-
throw new ArgumentNullException("[Rsa.ProcessBlock] 'engine' must not be null.");
98+
throw new ArgumentNullException(nameof(engine), "[Rsa.ProcessBlock] 'engine' must not be null.");
10099
}
101100

102101
try

TibiaAPI/Network/Xtea.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace OXGaming.TibiaAPI.Network
55
/// <summary>
66
/// The <see cref="Xtea"/> class contains methods for encrypting and decrypting Tibia packets using the XTEA algorithm.
77
/// </summary>
8-
public class Xtea
8+
internal class Xtea
99
{
1010
private const uint Delta = 0x9E3779B9;
1111
private const uint BlockSize = 8;
@@ -42,10 +42,10 @@ public unsafe bool Decrypt(NetworkMessage message, uint index = 6)
4242
{
4343
if (message == null)
4444
{
45-
throw new ArgumentNullException("[Xtea.Decrypt] 'message' must not be null.");
45+
throw new ArgumentNullException(nameof(message), "[Xtea.Decrypt] 'message' must not be null.");
4646
}
4747

48-
if (index >= message.Size || ((message.Size - index) % 8) > 0)
48+
if (index >= message.Size || ((message.Size - index) % BlockSize) > 0)
4949
{
5050
return false;
5151
}
@@ -83,7 +83,7 @@ public unsafe bool Encrypt(NetworkMessage message, uint index = 6)
8383
{
8484
if (message == null)
8585
{
86-
throw new ArgumentNullException("[Xtea.Encrypt] 'message' must not be null.");
86+
throw new ArgumentNullException(nameof(message), "[Xtea.Encrypt] 'message' must not be null.");
8787
}
8888

8989
if (index >= message.Size)
@@ -92,10 +92,10 @@ public unsafe bool Encrypt(NetworkMessage message, uint index = 6)
9292
}
9393

9494
var encryptSize = message.Size - index;
95-
var padding = encryptSize % 8;
95+
var padding = encryptSize % BlockSize;
9696
if (padding > 0)
9797
{
98-
encryptSize += 8 - padding;
98+
encryptSize += BlockSize - padding;
9999
var newSize = index + encryptSize;
100100
if (newSize > NetworkMessage.MaxMessageSize)
101101
{

TibiaAPI/TibiaAPI.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.1">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
22+
</PackageReference>
1923
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
2024
<PackageReference Include="Portable.BouncyCastle" Version="1.8.2" />
2125
</ItemGroup>

0 commit comments

Comments
 (0)