From 69ef8154c0a8326dddd573474dbdcb0d7fa5c445 Mon Sep 17 00:00:00 2001 From: Dominic Hock Date: Sat, 15 Jan 2022 23:28:30 +0100 Subject: [PATCH] Refactoring of code --- Examples/ClientExample/ClientExample.csproj | 112 +++---- Examples/ClientExample/Program.cs | 16 +- Examples/ServerExample/Program.cs | 38 ++- Examples/ServerExample/ServerExample.csproj | 112 +++---- RCONServerLib.Tests/ConnectionTests.cs | 158 +++++----- .../RCONServerLib.Tests.csproj | 148 ++++----- RCONServerLib.Tests/RemoteConTests.cs | 285 +++++++++--------- RCONServerLib.Tests/Tests.cs | 14 +- RCONServerLib.Tests/packages.config | 16 +- RCONServerLib/Properties/AssemblyInfo.cs | 2 +- RCONServerLib/RCONServerLib.csproj | 4 +- RCONServerLib/RCONServerLib.nuspec | 10 +- RCONServerLib/RemoteConClient.cs | 64 ++-- RCONServerLib/RemoteConPacket.cs | 30 +- RCONServerLib/RemoteConServer.cs | 15 +- RCONServerLib/RemoteConTcpClient.cs | 47 +-- ...ryReaderExt.cs => BinaryReaderExtended.cs} | 2 +- ...ryWriterExt.cs => BinaryWriterExtended.cs} | 2 +- RCONServerLib/Utils/CommandManager.cs | 25 +- RCONServerLib/Utils/DateTimeExtensions.cs | 4 +- README.md | 2 +- 21 files changed, 558 insertions(+), 548 deletions(-) rename RCONServerLib/Utils/{BinaryReaderExt.cs => BinaryReaderExtended.cs} (96%) rename RCONServerLib/Utils/{BinaryWriterExt.cs => BinaryWriterExtended.cs} (87%) diff --git a/Examples/ClientExample/ClientExample.csproj b/Examples/ClientExample/ClientExample.csproj index afe6aab..93ff626 100644 --- a/Examples/ClientExample/ClientExample.csproj +++ b/Examples/ClientExample/ClientExample.csproj @@ -1,59 +1,59 @@  - - - Debug - AnyCPU - {F394E5AB-E3C0-4BF9-9BE7-BAE048B61B40} - Exe - Properties - ClientExample - ClientExample - v3.5 - 512 - 6 - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} - RCONServerLib - - - - + + + Debug + AnyCPU + {F394E5AB-E3C0-4BF9-9BE7-BAE048B61B40} + Exe + Properties + ClientExample + ClientExample + v3.5 + 512 + 6 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} + RCONServerLib + + + + \ No newline at end of file diff --git a/Examples/ClientExample/Program.cs b/Examples/ClientExample/Program.cs index f106422..39a25a5 100644 --- a/Examples/ClientExample/Program.cs +++ b/Examples/ClientExample/Program.cs @@ -19,18 +19,18 @@ public static void Main(string[] args) password = args[2]; } - var client = new RemoteConClient(); - client.OnLog += message => { Console.WriteLine(string.Format("Client Log: {0}", message)); }; + var client = new RemoteConClient() + { + UseUtf8 = true, + }; + client.OnLog += message => { Console.WriteLine("Client Log: {0}", message); }; client.OnAuthResult += result => { _authProcessed = true; }; client.OnConnectionStateChange += state => { Console.WriteLine("Connection changed: " + state); - if (state == 0) - { - client.Authenticate(password); - } + if (state == RemoteConClient.ConnectionStateChange.Connected) client.Authenticate(password); }; - + client.Connect(ip, port); while (true) { @@ -57,7 +57,7 @@ public static void Main(string[] args) if (cmd == "exit" || cmd == "quit") { client.Disconnect(); - return; + break; } client.SendCommand(cmd, result => { Console.WriteLine("CMD Result: " + result); }); diff --git a/Examples/ServerExample/Program.cs b/Examples/ServerExample/Program.cs index 3575527..7d9b274 100644 --- a/Examples/ServerExample/Program.cs +++ b/Examples/ServerExample/Program.cs @@ -12,7 +12,7 @@ public static void Main(string[] args) var server = new RemoteConServer(IPAddress.Any, 27015) { SendAuthImmediately = true, - Debug = true, + Debug = true }; server.CommandManager.Add("hello", "Echos back world", (command, arguments) => { return "world"; }); server.CommandManager.Add("help", "(command)", "Shows this help", (cmd, arguments) => @@ -26,30 +26,28 @@ public static void Main(string[] args) return string.Format("{0} - {1}", helpCmd.Name, helpCmd.Description); } - else + + var sb = new StringBuilder(); + + var all = server.CommandManager.Commands.Count; + var i = 0; + foreach (var command in server.CommandManager.Commands) { - var sb = new StringBuilder(); - - var all = server.CommandManager.Commands.Count; - var i = 0; - foreach (var command in server.CommandManager.Commands) - { - if (command.Value.Usage == "") - sb.AppendFormat("{0}", command.Value.Name); - else - sb.AppendFormat("{0} {1}", command.Value.Name, command.Value.Usage); - if (i < all) - sb.Append(", "); - - i++; - } - - return sb.ToString(); + if (command.Value.Usage == "") + sb.AppendFormat("{0}", command.Value.Name); + else + sb.AppendFormat("{0} {1}", command.Value.Name, command.Value.Usage); + if (i < all) + sb.Append(", "); + + i++; } + + return sb.ToString(); }); server.StartListening(); - + Console.WriteLine("Server started. Press any key to stop."); Console.ReadKey(); } diff --git a/Examples/ServerExample/ServerExample.csproj b/Examples/ServerExample/ServerExample.csproj index 9a8e4a0..782577b 100644 --- a/Examples/ServerExample/ServerExample.csproj +++ b/Examples/ServerExample/ServerExample.csproj @@ -1,59 +1,59 @@  - - - Debug - AnyCPU - {EDB656EE-634B-4080-8293-73205470FCCF} - Exe - Properties - ServerExample - ServerExample - v3.5 - 512 - 6 - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} - RCONServerLib - - - - + + + Debug + AnyCPU + {EDB656EE-634B-4080-8293-73205470FCCF} + Exe + Properties + ServerExample + ServerExample + v3.5 + 512 + 6 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} + RCONServerLib + + + + \ No newline at end of file diff --git a/RCONServerLib.Tests/ConnectionTests.cs b/RCONServerLib.Tests/ConnectionTests.cs index 9dc1d81..88b7187 100644 --- a/RCONServerLib.Tests/ConnectionTests.cs +++ b/RCONServerLib.Tests/ConnectionTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Net; +using System.Net; using System.Threading; using Xunit; @@ -10,30 +9,33 @@ public class ConnectionTests [Theory] [InlineData(false)] [InlineData(true)] - public void TestAuthFail(bool useUTF8) + public void TestAuthFail(bool useUtf8) { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.UseUTF8 = useUTF8; + var server = new RemoteConServer(IPAddress.Any, 27015) + { + UseUtf8 = useUtf8 + }; server.StartListening(); - bool authResult = false; - using (var waitEvent = new AutoResetEvent(false)) + var authResult = true; + var waitEvent = new AutoResetEvent(false); + var client = new RemoteConClient { - var client = new RemoteConClient(); - client.UseUTF8 = useUTF8; - client.OnAuthResult += success => - { - authResult = success; + UseUtf8 = useUtf8 + }; - client.Disconnect(); - server.StopListening(); - waitEvent.Set(); - }; + client.OnAuthResult += success => + { + authResult = success; - client.Connect("127.0.0.1", 27015); - client.Authenticate("unitfail"); - waitEvent.WaitOne(); - } + client.Disconnect(); + server.StopListening(); + waitEvent.Set(); + }; + + client.Connect("127.0.0.1", 27015); + client.Authenticate("unitfail"); + waitEvent.WaitOne(); Assert.False(authResult); } @@ -41,30 +43,32 @@ public void TestAuthFail(bool useUTF8) [Theory] [InlineData(false)] [InlineData(true)] - public void TestAuthSuccess(bool useUTF8) + public void TestAuthSuccess(bool useUtf8) { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.UseUTF8 = useUTF8; + var server = new RemoteConServer(IPAddress.Any, 27015) + { + UseUtf8 = useUtf8 + }; server.StartListening(); - bool authResult = false; - using (var waitEvent = new AutoResetEvent(false)) + var authResult = false; + var waitEvent = new AutoResetEvent(false); + var client = new RemoteConClient { - var client = new RemoteConClient(); - client.UseUTF8 = useUTF8; - client.OnAuthResult += success => - { - authResult = success; + UseUtf8 = useUtf8 + }; + client.OnAuthResult += success => + { + authResult = success; - client.Disconnect(); - server.StopListening(); - waitEvent.Set(); - }; + client.Disconnect(); + server.StopListening(); + waitEvent.Set(); + }; - client.Connect("127.0.0.1", 27015); - client.Authenticate("changeme"); - waitEvent.WaitOne(); - } + client.Connect("127.0.0.1", 27015); + client.Authenticate("changeme"); + waitEvent.WaitOne(); Assert.True(authResult); } @@ -72,33 +76,33 @@ public void TestAuthSuccess(bool useUTF8) [Theory] [InlineData(false)] [InlineData(true)] - public void TestCommandFail(bool useUTF8) + public void TestCommandFail(bool useUtf8) { var server = new RemoteConServer(IPAddress.Any, 27015); server.StartListening(); - server.UseUTF8 = useUTF8; + server.UseUtf8 = useUtf8; string commandResult = null; - using (var waitEvent = new AutoResetEvent(false)) + var waitEvent = new AutoResetEvent(false); + var client = new RemoteConClient + { + UseUtf8 = useUtf8 + }; + client.OnAuthResult += success => { - var client = new RemoteConClient(); - client.UseUTF8 = useUTF8; - client.OnAuthResult += success => + client.SendCommand("testing", result => { - client.SendCommand("testing", result => - { - commandResult = result; + commandResult = result; - client.Disconnect(); - server.StopListening(); - waitEvent.Set(); - }); - }; + client.Disconnect(); + server.StopListening(); + waitEvent.Set(); + }); + }; - client.Connect("127.0.0.1", 27015); - client.Authenticate("changeme"); - waitEvent.WaitOne(); - } + client.Connect("127.0.0.1", 27015); + client.Authenticate("changeme"); + waitEvent.WaitOne(); Assert.Contains("Invalid command", commandResult); } @@ -106,34 +110,34 @@ public void TestCommandFail(bool useUTF8) [Theory] [InlineData(false)] [InlineData(true)] - public void TestCommandSuccess(bool useUTF8) + public void TestCommandSuccess(bool useUtf8) { var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("hello", "Replies with world", (command, args) => "world"); - server.UseUTF8 = true; + server.CommandManager.Add("hello", "Replies with world", (command, args) => "world"); + server.UseUtf8 = true; server.StartListening(); string commandResult = null; - using (var waitEvent = new AutoResetEvent(false)) + var waitEvent = new AutoResetEvent(false); + var client = new RemoteConClient { - var client = new RemoteConClient(); - client.UseUTF8 = useUTF8; - client.OnAuthResult += success => + UseUtf8 = useUtf8 + }; + client.OnAuthResult += success => + { + client.SendCommand("hello", result => { - client.SendCommand("hello", result => - { - commandResult = result; - - client.Disconnect(); - server.StopListening(); - waitEvent.Set(); - }); - }; - - client.Connect("127.0.0.1", 27015); - client.Authenticate("changeme"); - waitEvent.WaitOne(); - } + commandResult = result; + + client.Disconnect(); + server.StopListening(); + waitEvent.Set(); + }); + }; + + client.Connect("127.0.0.1", 27015); + client.Authenticate("changeme"); + waitEvent.WaitOne(); Assert.Contains("world", commandResult); } diff --git a/RCONServerLib.Tests/RCONServerLib.Tests.csproj b/RCONServerLib.Tests/RCONServerLib.Tests.csproj index a16664f..4f42a5f 100644 --- a/RCONServerLib.Tests/RCONServerLib.Tests.csproj +++ b/RCONServerLib.Tests/RCONServerLib.Tests.csproj @@ -1,77 +1,77 @@  - - - Debug - AnyCPU - {A1FDCE93-4C35-4A04-AD3E-F67949607BA9} - {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Library - Properties - RCONServerLib.Tests - RCONServerLib.Tests - v4.5.2 - 512 - 6 - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - ..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll - - - ..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll - - - ..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll - - - ..\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll - - - - - - - - - - - {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} - RCONServerLib - - - - - - - + + + Debug + AnyCPU + {A1FDCE93-4C35-4A04-AD3E-F67949607BA9} + {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + Properties + RCONServerLib.Tests + RCONServerLib.Tests + v4.5.2 + 512 + 6 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + ..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll + + + ..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll + + + ..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll + + + ..\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll + + + + + + + + + + + {2efa0311-14ce-414b-9867-fc0d1ff6f6c4} + RCONServerLib + + + + + + + \ No newline at end of file diff --git a/RCONServerLib.Tests/RemoteConTests.cs b/RCONServerLib.Tests/RemoteConTests.cs index 55e2614..4477fbd 100644 --- a/RCONServerLib.Tests/RemoteConTests.cs +++ b/RCONServerLib.Tests/RemoteConTests.cs @@ -9,171 +9,172 @@ public class RemoteConTests [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConInvalidAuthPacketTypeTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - - // Wrong Auth packet type test - Assert.Throws(() => - { - client.ParsePacket(new byte[] - { - 0x15, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x02, 0x00, 0x00, 0x00, // Type - 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, - 0x00, - }); - }); - } + public void RemoteConInvalidAuthPacketTypeTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + + // Wrong Auth packet type test + Assert.Throws(() => + { + client.ParsePacket(new byte[] + { + 0x15, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x02, 0x00, 0x00, 0x00, // Type + 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, + 0x00 + }); + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConAuthFailureTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - - // Auth wrong test - client.ParsePacket(new byte[] - { - 0x15, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x03, 0x00, 0x00, 0x00, // Type - 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, - 0x00, - }); - } + public void RemoteConAuthFailureTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + + // Auth wrong test + client.ParsePacket(new byte[] + { + 0x15, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x03, 0x00, 0x00, 0x00, // Type + 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, + 0x00 + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConAuthSuccessTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015) - { - Password = "supersecretpassword" - }; - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - - // Auth correct test - client.ParsePacket(new byte[] - { - 0x1D, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x03, 0x00, 0x00, 0x00, // Type - 0x73, 0x75, 0x70, 0x65, 0x72, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64, 0x00, - 0x00, - }); - } + public void RemoteConAuthSuccessTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015) + { + Password = "supersecretpassword" + }; + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + + // Auth correct test + client.ParsePacket(new byte[] + { + 0x1D, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x03, 0x00, 0x00, 0x00, // Type + 0x73, 0x75, 0x70, 0x65, 0x72, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, + 0x72, 0x64, 0x00, + 0x00 + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConInvalidCommandTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - client.Authenticated = true; - - // No command found test - client.ParsePacket(new byte[] - { - 0x15, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x02, 0x00, 0x00, 0x00, // Type - 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, - 0x00, - }); - } + public void RemoteConInvalidCommandTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + client.Authenticated = true; + + // No command found test + client.ParsePacket(new byte[] + { + 0x15, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x02, 0x00, 0x00, 0x00, // Type + 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, + 0x00 + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConCommandTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - client.Authenticated = true; - - // Command test - client.ParsePacket(new byte[] - { - 0x0E, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x02, 0x00, 0x00, 0x00, // Type - 0x74, 0x65, 0x73, 0x74, 0x00, - 0x00, - }); - } + public void RemoteConCommandTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + client.Authenticated = true; + + // Command test + client.ParsePacket(new byte[] + { + 0x0E, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x02, 0x00, 0x00, 0x00, // Type + 0x74, 0x65, 0x73, 0x74, 0x00, + 0x00 + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConEmptyPayloadTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - client.Authenticated = true; - - // Empty payload test - Assert.Throws(() => - { - client.ParsePacket(new byte[] - { - 0x0A, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x02, 0x00, 0x00, 0x00, // Type - 0x00, - 0x00, - }); - }); - } + public void RemoteConEmptyPayloadTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + client.Authenticated = true; + + // Empty payload test + Assert.Throws(() => + { + client.ParsePacket(new byte[] + { + 0x0A, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x02, 0x00, 0x00, 0x00, // Type + 0x00, + 0x00 + }); + }); + } [Theory] [InlineData(false)] [InlineData(true)] - public void RemoteConInvalidPacketTypeTest(bool useUTF8) - { - var server = new RemoteConServer(IPAddress.Any, 27015); - server.CommandManager.Add("test", "Test", (command, args) => "test"); - server.UseUTF8 = useUTF8; - - var client = new RemoteConTcpClient(server); - client.Authenticated = true; - - // Type other than execcommand - Assert.Throws(() => - { - client.ParsePacket(new byte[] - { - 0x0E, 0x00, 0x00, 0x00, // Size - 0x02, 0x00, 0x00, 0x00, // Id - 0x00, 0x00, 0x00, 0x00, // Type - 0x74, 0x65, 0x73, 0x74, 0x00, - 0x00, - }); - }); - } + public void RemoteConInvalidPacketTypeTest(bool useUtf8) + { + var server = new RemoteConServer(IPAddress.Any, 27015); + server.CommandManager.Add("test", "Test", (command, args) => "test"); + server.UseUtf8 = useUtf8; + + var client = new RemoteConTcpClient(server); + client.Authenticated = true; + + // Type other than execcommand + Assert.Throws(() => + { + client.ParsePacket(new byte[] + { + 0x0E, 0x00, 0x00, 0x00, // Size + 0x02, 0x00, 0x00, 0x00, // Id + 0x00, 0x00, 0x00, 0x00, // Type + 0x74, 0x65, 0x73, 0x74, 0x00, + 0x00 + }); + }); + } } } \ No newline at end of file diff --git a/RCONServerLib.Tests/Tests.cs b/RCONServerLib.Tests/Tests.cs index a53f5a1..bd5153d 100644 --- a/RCONServerLib.Tests/Tests.cs +++ b/RCONServerLib.Tests/Tests.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Net; using RCONServerLib.Utils; using Xunit; @@ -13,7 +12,8 @@ public class Tests [InlineData(true)] public void RconPacketLengthTest(bool useUTF8) { - Assert.Throws(() => new RemoteConPacket(new byte[] {0xFF, 0xFF, 0x00, 0x00}, useUTF8)); + Assert.Throws(() => + new RemoteConPacket(new byte[] { 0xFF, 0xFF, 0x00, 0x00 }, useUTF8)); } [Theory] @@ -46,7 +46,7 @@ public void RconPacketTooLongTest() for (var i = 13; i < 4094; i++) bytes[i] = 0xFF; Assert.Throws(() => new RemoteConPacket(bytes)); } - + [Theory] [InlineData(false)] [InlineData(true)] @@ -83,7 +83,7 @@ public void RconPacketSuccessTest(bool useUTF8) 0x02, 0x00, 0x00, 0x00, // Id 0x03, 0x00, 0x00, 0x00, // Type 0x00, - 0x00, + 0x00 }, useUTF8); Assert.Equal(2, packet.Id); @@ -102,7 +102,7 @@ public void RconPacketGetBytesTest(bool useUTF8) 0x02, 0x00, 0x00, 0x00, // Id 0x03, 0x00, 0x00, 0x00, // Type 0x00, - 0x00, + 0x00 }; var packet = new RemoteConPacket(2, RemoteConPacket.PacketType.Auth, "", useUTF8); @@ -120,7 +120,7 @@ public void ExtendedBinaryReaderTest() 0x02, 0x00, 0x00, 0x00, // Id 0x03, 0x00, 0x00, 0x00, // Type 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00, - 0x00, + 0x00 }; using (var ms = new MemoryStream(testBytes)) { @@ -149,7 +149,7 @@ public void DateTimeTest() Assert.True(nowTime.Hour == DateTimeExtensions.FromUnixTimestamp(unix).Hour, "Hour doesn't match"); Assert.True(nowTime.Minute == DateTimeExtensions.FromUnixTimestamp(unix).Minute, "Minute doesn't match"); Assert.True(nowTime.Second == DateTimeExtensions.FromUnixTimestamp(unix).Second, "Second doesn't match"); - + var unix2 = DateTime.Now.UnixTimestamp(); Assert.True(nowTime.Day == DateTimeExtensions.FromUnixTimestamp(unix2).Day, "Day doesn't match"); Assert.True(nowTime.Month == DateTimeExtensions.FromUnixTimestamp(unix2).Month, "Month doesn't match"); diff --git a/RCONServerLib.Tests/packages.config b/RCONServerLib.Tests/packages.config index 023d420..bf07c14 100644 --- a/RCONServerLib.Tests/packages.config +++ b/RCONServerLib.Tests/packages.config @@ -1,11 +1,11 @@  - - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/RCONServerLib/Properties/AssemblyInfo.cs b/RCONServerLib/Properties/AssemblyInfo.cs index 201343a..7cc9d24 100644 --- a/RCONServerLib/Properties/AssemblyInfo.cs +++ b/RCONServerLib/Properties/AssemblyInfo.cs @@ -22,7 +22,7 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("2EFA0311-14CE-414B-9867-FC0D1FF6F6C4")] -[assembly:InternalsVisibleTo("RCONServerLib.Tests")] +[assembly: InternalsVisibleTo("RCONServerLib.Tests")] // Version information for an assembly consists of the following four values: // diff --git a/RCONServerLib/RCONServerLib.csproj b/RCONServerLib/RCONServerLib.csproj index b2960c2..323caf3 100644 --- a/RCONServerLib/RCONServerLib.csproj +++ b/RCONServerLib/RCONServerLib.csproj @@ -42,8 +42,8 @@ - - + + diff --git a/RCONServerLib/RCONServerLib.nuspec b/RCONServerLib/RCONServerLib.nuspec index 1775058..a359da9 100644 --- a/RCONServerLib/RCONServerLib.nuspec +++ b/RCONServerLib/RCONServerLib.nuspec @@ -8,14 +8,16 @@ Julien H. (Subtixx) https://github.com/Subtixx/source-rcon-library/blob/master/LICENSE https://github.com/Subtixx/source-rcon-library/ - https://user-images.githubusercontent.com/20743379/35411973-0227102e-021b-11e8-9a1b-023e08c33c4e.png + https://user-images.githubusercontent.com/20743379/35411973-0227102e-021b-11e8-9a1b-023e08c33c4e.png + false -Easy to use, single-class solution to create a Valve source RCON server which supports authentication, -IP Whitelisting, a command manager and much more! + Easy to use, single-class solution to create a Valve source RCON server which supports authentication, + IP Whitelisting, a command manager and much more! Single class solution to create a source compatible RCON interface - Please see https://github.com/Subtixx/source-rcon-library/releases for release notes! + Please see https://github.com/Subtixx/source-rcon-library/releases for release notes! + Copyright Julien H. (Subtixx) 2018 source-rcon-server rconserver remoteconsole rcon-server diff --git a/RCONServerLib/RemoteConClient.cs b/RCONServerLib/RemoteConClient.cs index 02f870c..adfe198 100644 --- a/RCONServerLib/RemoteConClient.cs +++ b/RCONServerLib/RemoteConClient.cs @@ -40,11 +40,6 @@ public enum ConnectionStateChange private const int MaxAllowedPacketSize = 4096; - /// - /// The TCP Client - /// - private TcpClient _client; - /// /// A list containing all requested commands for event handling /// @@ -55,6 +50,11 @@ public enum ConnectionStateChange /// private byte[] _buffer; + /// + /// The TCP Client + /// + private TcpClient _client; + /// /// Underlaying NetworkStream /// @@ -77,7 +77,7 @@ public RemoteConClient() _packetId = 0; _requestedCommands = new Dictionary(); - UseUTF8 = false; + UseUtf8 = false; } /// @@ -91,7 +91,7 @@ public bool Connected /// /// Whether to use UTF8 to encode the packet payload /// - public bool UseUTF8 { get; set; } + public bool UseUtf8 { get; set; } /// /// An event handler when the result of the authentication is received @@ -120,30 +120,34 @@ public void Connect(string hostname, int port) Log(string.Format("Connecting to {0}:{1}", hostname, port)); try { - IAsyncResult ar = null; + IAsyncResult asyncResult = null; try { - ar = _client.BeginConnect(hostname, port, null, null); + asyncResult = _client.BeginConnect(hostname, port, null, null); } catch (ObjectDisposedException) { _client = new TcpClient(); try { - ar = _client.BeginConnect(hostname, port, null, null); + asyncResult = _client.BeginConnect(hostname, port, null, null); } - catch (Exception e) + catch (Exception) { Log("Unknown Exception"); } } - ar.AsyncWaitHandle.WaitOne(2000); // wait 2 seconds - if (!ar.IsCompleted) + + if (asyncResult == null) { - if (OnConnectionStateChange != null) - { - OnConnectionStateChange(ConnectionStateChange.NoConnection); - } + Log("Async connect failed!"); + return; + } + + asyncResult.AsyncWaitHandle.WaitOne(2000); // wait 2 seconds + if (!asyncResult.IsCompleted) + { + if (OnConnectionStateChange != null) OnConnectionStateChange(ConnectionStateChange.NoConnection); _client.Client.Close(); } } @@ -154,6 +158,7 @@ public void Connect(string hostname, int port) OnConnectionStateChange(ConnectionStateChange.ConnectionTimeout); _client.Client.Close(); } + return; } @@ -202,7 +207,7 @@ public void Disconnect() public void Authenticate(string password) { _packetId++; - var packet = new RemoteConPacket(_packetId, RemoteConPacket.PacketType.Auth, password, UseUTF8); + var packet = new RemoteConPacket(_packetId, RemoteConPacket.PacketType.Auth, password, UseUtf8); SendPacket(packet); } @@ -223,7 +228,7 @@ public void SendCommand(string command, CommandResult resultFunc) _packetId++; _requestedCommands.Add(_packetId, resultFunc); - var packet = new RemoteConPacket(_packetId, RemoteConPacket.PacketType.ExecCommand, command, UseUTF8); + var packet = new RemoteConPacket(_packetId, RemoteConPacket.PacketType.ExecCommand, command, UseUtf8); SendPacket(packet); } @@ -243,8 +248,12 @@ private void SendPacket(RemoteConPacket packet) { _ns.BeginWrite(packetBytes, 0, packetBytes.Length - 1, ar => { _ns.EndWrite(ar); }, null); } - catch (ObjectDisposedException) { } // Do not write to NetworkStream when it's closed. - catch (IOException) { } // Do not write to Socket when it's closed. + catch (ObjectDisposedException) + { + } // Do not write to NetworkStream when it's closed. + catch (IOException) + { + } // Do not write to Socket when it's closed. } /// @@ -300,11 +309,11 @@ private void OnPacket(IAsyncResult result) /// Parses raw bytes to RemoteConPacket /// /// - internal void ParsePacket(byte[] rawPacket) + private void ParsePacket(byte[] rawPacket) { try { - var packet = new RemoteConPacket(rawPacket, UseUTF8); + var packet = new RemoteConPacket(rawPacket, UseUtf8); if (!Authenticated) { // ExecCommand is AuthResponse too. @@ -328,14 +337,11 @@ internal void ParsePacket(byte[] rawPacket) return; } - if (_requestedCommands.ContainsKey(packet.Id) && packet.Type == RemoteConPacket.PacketType.ResponseValue) - { + if (_requestedCommands.ContainsKey(packet.Id) && + packet.Type == RemoteConPacket.PacketType.ResponseValue) _requestedCommands[packet.Id](packet.Payload); - } else - { Log("Got packet with invalid id " + packet.Id); - } } catch (Exception e) { @@ -344,4 +350,4 @@ internal void ParsePacket(byte[] rawPacket) } } } -} +} \ No newline at end of file diff --git a/RCONServerLib/RemoteConPacket.cs b/RCONServerLib/RemoteConPacket.cs index 5b4f293..bbe0d4b 100644 --- a/RCONServerLib/RemoteConPacket.cs +++ b/RCONServerLib/RemoteConPacket.cs @@ -20,6 +20,12 @@ public enum PacketType private const int MaxAllowedPacketSize = 4096; + /// + /// Indicates the encoding of the packet payload. + /// Is either ASCII or UTF8 + /// + private readonly Encoding _packetEncoding = Encoding.ASCII; + /// /// The identifier of the packet /// (It need not be unique, but if a unique packet id is assigned, @@ -43,15 +49,9 @@ public enum PacketType /// public readonly PacketType Type; - /// - /// Indicates the encoding of the packet payload. - /// Is either ASCII or UTF8 - /// - private readonly Encoding _packetEncoding = Encoding.ASCII; - - public RemoteConPacket(byte[] packetBytes, bool useUTF8 = false) + public RemoteConPacket(byte[] packetBytes, bool useUtf8 = false) { - if (useUTF8) + if (useUtf8) _packetEncoding = Encoding.UTF8; using (var ms = new MemoryStream(packetBytes)) @@ -69,9 +69,9 @@ public RemoteConPacket(byte[] packetBytes, bool useUTF8 = false) var packetType = reader.ReadInt32LittleEndian(); if (!Enum.IsDefined(typeof(PacketType), packetType)) throw new InvalidPacketTypeException("Invalid packet type"); - Type = (PacketType) Enum.ToObject(typeof(PacketType), packetType); + Type = (PacketType)Enum.ToObject(typeof(PacketType), packetType); - if (!useUTF8) + if (!useUtf8) { Payload = reader.ReadAscii(); @@ -95,9 +95,9 @@ public RemoteConPacket(byte[] packetBytes, bool useUTF8 = false) } } - public RemoteConPacket(int id, PacketType type, string payload, bool useUTF8 = false) + public RemoteConPacket(int id, PacketType type, string payload, bool useUtf8 = false) { - if (useUTF8) + if (useUtf8) _packetEncoding = Encoding.UTF8; Payload = payload; @@ -110,7 +110,7 @@ public RemoteConPacket(int id, PacketType type, string payload, bool useUTF8 = f /// /// The total size of the packet /// - public int Length + private int Length { get { return _packetEncoding.GetBytes(Payload + '\0').Length + 13; } } @@ -128,7 +128,7 @@ public byte[] GetBytes() var bodyBytes = _packetEncoding.GetBytes(Payload + '\0'); writer.WriteLittleEndian(bodyBytes.Length + 9); writer.WriteLittleEndian(Id); - writer.WriteLittleEndian((int) Type); + writer.WriteLittleEndian((int)Type); writer.Write(bodyBytes); writer.Write('\0'); @@ -137,4 +137,4 @@ public byte[] GetBytes() } } } -} +} \ No newline at end of file diff --git a/RCONServerLib/RemoteConServer.cs b/RCONServerLib/RemoteConServer.cs index dec4b28..d565110 100644 --- a/RCONServerLib/RemoteConServer.cs +++ b/RCONServerLib/RemoteConServer.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Net; using System.Net.Sockets; -using System.Threading; using RCONServerLib.Utils; namespace RCONServerLib @@ -20,7 +19,7 @@ public class RemoteConServer public RemoteConServer(IPAddress bindAddress, int port) { - UseUTF8 = false; + UseUtf8 = false; EmptyPayloadKick = true; EnableIpWhitelist = true; InvalidPacketKick = true; @@ -55,7 +54,7 @@ public RemoteConServer(IPAddress bindAddress, int port) /// Whether to use UTF8 to encode packet payloads. /// Default: False /// - public bool UseUTF8 { get; set; } + public bool UseUtf8 { get; set; } /// /// When true closes the connection if the payload of the packet is empty. @@ -150,7 +149,7 @@ public void StartListening() { _listener.Start(); _listener.BeginAcceptTcpClient(OnAccept, _listener); - LogDebug("Started listening on " + ((IPEndPoint) _listener.LocalEndpoint).Address + ", Password is: \"" + + LogDebug("Started listening on " + ((IPEndPoint)_listener.LocalEndpoint).Address + ", Password is: \"" + Password + "\""); } @@ -175,7 +174,7 @@ private void OnAccept(IAsyncResult result) return; } - var ip = ((IPEndPoint) tcpClient.Client.RemoteEndPoint).Address; + var ip = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address; if (MaxConnections > 0) if (_clients.Count >= MaxConnections) @@ -189,7 +188,7 @@ private void OnAccept(IAsyncResult result) { var count = 0; foreach (var tcpClient1 in _clients) - if (((IPEndPoint) tcpClient1.Client.RemoteEndPoint).Address.ToString() == ip.ToString()) + if (((IPEndPoint)tcpClient1.Client.RemoteEndPoint).Address.ToString() == ip.ToString()) count++; if (count >= MaxConnectionsPerIp) @@ -202,7 +201,7 @@ private void OnAccept(IAsyncResult result) if (EnableIpWhitelist) if (!IpWhitelist.Any(p => - IpExtension.Match(p, ip.ToString()))) + IpExtension.Match(p, ip.ToString()))) { LogDebug("Rejected new connection from " + ip + " (Not in whitelist)"); tcpClient.Close(); @@ -223,7 +222,7 @@ private void OnAccept(IAsyncResult result) } LogDebug("Accepted new connection from " + ip); - var client = new RemoteConTcpClient(tcpClient, this); + new RemoteConTcpClient(tcpClient, this); _clients.Add(tcpClient); diff --git a/RCONServerLib/RemoteConTcpClient.cs b/RCONServerLib/RemoteConTcpClient.cs index 4123329..fe0fd5b 100644 --- a/RCONServerLib/RemoteConTcpClient.cs +++ b/RCONServerLib/RemoteConTcpClient.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.IO; using System.Net; using System.Net.Sockets; @@ -101,7 +100,7 @@ private void CloseConnection() if (_isUnitTest) return; _remoteConServer.RemoveClient(_tcp); - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " connection closed."); + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " connection closed."); _connected = false; @@ -140,7 +139,7 @@ private void OnPacket(IAsyncResult result) var bytesRead = _ns.EndRead(result); if (!_tcp.Connected || !_connected) { - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection."); + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " lost connection."); CloseConnection(); return; } @@ -171,7 +170,7 @@ private void OnPacket(IAsyncResult result) if (!_connected || !_tcp.Connected) { - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection."); + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " lost connection."); CloseConnection(); return; } @@ -181,12 +180,12 @@ private void OnPacket(IAsyncResult result) } catch (IOException) { - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection."); + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " lost connection."); CloseConnection(); } catch (ThreadAbortException) { - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " socket closed."); + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " socket closed."); CloseConnection(); } catch (RconServerException) @@ -207,10 +206,10 @@ internal void ParsePacket(byte[] rawPacket) { try { - var packet = new RemoteConPacket(rawPacket, _remoteConServer.UseUTF8); + var packet = new RemoteConPacket(rawPacket, _remoteConServer.UseUtf8); if (!_isUnitTest) - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " sent packet " + + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " sent packet " + packet.Type + "!"); // Do not allow any other packets than auth to be sent when client is not authenticated @@ -228,37 +227,39 @@ internal void ParsePacket(byte[] rawPacket) if (packet.Payload == _remoteConServer.Password) { if (!_isUnitTest) - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " successfully authenticated!"); Authenticated = true; if (!_remoteConServer.SendAuthImmediately) - SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, "", _remoteConServer.UseUTF8)); + SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, "", + _remoteConServer.UseUtf8)); - SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ExecCommand, "", _remoteConServer.UseUTF8)); + SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ExecCommand, "", + _remoteConServer.UseUtf8)); return; } if (_authTries >= _remoteConServer.MaxPasswordTries) { if (_remoteConServer.BanMinutes > 0) - { - _remoteConServer.IpBanList.Add(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address.ToString(), + _remoteConServer.IpBanList.Add(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address.ToString(), DateTime.Now.AddMinutes(_remoteConServer.BanMinutes).ToUnixTimestamp()); - } CloseConnection(); return; } if (!_isUnitTest) - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " entered wrong password!"); if (!_remoteConServer.SendAuthImmediately) - SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, "", _remoteConServer.UseUTF8)); + SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, "", + _remoteConServer.UseUtf8)); - SendPacket(new RemoteConPacket(-1, RemoteConPacket.PacketType.ExecCommand, "", _remoteConServer.UseUTF8)); + SendPacket(new RemoteConPacket(-1, RemoteConPacket.PacketType.ExecCommand, "", + _remoteConServer.UseUtf8)); return; } @@ -269,7 +270,7 @@ internal void ParsePacket(byte[] rawPacket) if (_isUnitTest) throw new InvalidPacketTypeException(); - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " sent a packet with invalid type!"); if (_remoteConServer.InvalidPacketKick) @@ -281,7 +282,7 @@ internal void ParsePacket(byte[] rawPacket) { if (_isUnitTest) throw new EmptyPacketPayloadException(); - _remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + + _remoteConServer.LogDebug(((IPEndPoint)_tcp.Client.RemoteEndPoint).Address + " sent a packet with empty payload!"); if (_remoteConServer.EmptyPayloadKick) @@ -297,7 +298,7 @@ internal void ParsePacket(byte[] rawPacket) { var result = _remoteConServer.ExecuteCustomCommandHandler(cmd, args); SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, - result, _remoteConServer.UseUTF8)); + result, _remoteConServer.UseUtf8)); return; } @@ -305,14 +306,14 @@ internal void ParsePacket(byte[] rawPacket) if (command == null) { SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, - "Invalid command \"" + packet.Payload + "\"", _remoteConServer.UseUTF8)); + "Invalid command \"" + packet.Payload + "\"", _remoteConServer.UseUtf8)); } else { var commandResult = command.Handler(cmd, args); // TODO: Split packets? SendPacket(new RemoteConPacket(packet.Id, RemoteConPacket.PacketType.ResponseValue, - commandResult, _remoteConServer.UseUTF8)); + commandResult, _remoteConServer.UseUtf8)); } } catch (RconServerException) @@ -326,7 +327,7 @@ internal void ParsePacket(byte[] rawPacket) if (!_isUnitTest) _remoteConServer.LogDebug(string.Format("Client {0} caused an exception: {1} and was killed.", - ((IPEndPoint) _tcp.Client.RemoteEndPoint).Address, e.Message)); + ((IPEndPoint)_tcp.Client.RemoteEndPoint).Address, e.Message)); CloseConnection(); } diff --git a/RCONServerLib/Utils/BinaryReaderExt.cs b/RCONServerLib/Utils/BinaryReaderExtended.cs similarity index 96% rename from RCONServerLib/Utils/BinaryReaderExt.cs rename to RCONServerLib/Utils/BinaryReaderExtended.cs index c965952..4598f08 100644 --- a/RCONServerLib/Utils/BinaryReaderExt.cs +++ b/RCONServerLib/Utils/BinaryReaderExtended.cs @@ -38,7 +38,7 @@ public string ReadAscii() { val = ReadByte(); if (val > 0) - sb.Append((char) val); + sb.Append((char)val); } while (val > 0); return sb.ToString(); diff --git a/RCONServerLib/Utils/BinaryWriterExt.cs b/RCONServerLib/Utils/BinaryWriterExtended.cs similarity index 87% rename from RCONServerLib/Utils/BinaryWriterExt.cs rename to RCONServerLib/Utils/BinaryWriterExtended.cs index 2ba29f8..4169af0 100644 --- a/RCONServerLib/Utils/BinaryWriterExt.cs +++ b/RCONServerLib/Utils/BinaryWriterExtended.cs @@ -12,7 +12,7 @@ public BinaryWriterExt(Stream stream) } /// - /// Writes the int as little endian if we're in a big-endian environment + /// Writes the int as little endian if we're in a big-endian environment /// /// The value to write public void WriteLittleEndian(int val) diff --git a/RCONServerLib/Utils/CommandManager.cs b/RCONServerLib/Utils/CommandManager.cs index 42bdc45..67d5e79 100644 --- a/RCONServerLib/Utils/CommandManager.cs +++ b/RCONServerLib/Utils/CommandManager.cs @@ -7,13 +7,13 @@ namespace RCONServerLib.Utils /// public class CommandManager { - public Dictionary Commands { get; private set; } - public CommandManager() { Commands = new Dictionary(); } + public Dictionary Commands { get; private set; } + /// /// Adds command to list of command handlers. /// @@ -82,16 +82,15 @@ public Command(string name, string usage, string description, CommandHandler han Handler = handler; } - public string Name { get; protected set; } - public string Usage { get; protected set; } - public string Description { get; protected set; } - public CommandHandler Handler { get; protected set; } + public string Name { get; private set; } + public string Usage { get; private set; } + public string Description { get; private set; } + public CommandHandler Handler { get; private set; } } - - /// - /// - /// - /// The command that was entered - /// List containing all arguments, does not contain command - public delegate string CommandHandler(string command, IList args); + + /// + /// + /// The command that was entered + /// List containing all arguments, does not contain command + public delegate string CommandHandler(string command, IList args); } \ No newline at end of file diff --git a/RCONServerLib/Utils/DateTimeExtensions.cs b/RCONServerLib/Utils/DateTimeExtensions.cs index 7450170..3b96a9b 100644 --- a/RCONServerLib/Utils/DateTimeExtensions.cs +++ b/RCONServerLib/Utils/DateTimeExtensions.cs @@ -11,7 +11,7 @@ public static class DateTimeExtensions /// The given DateTime in Unix timestamp format public static int ToUnixTimestamp(this DateTime value) { - return (int) Math.Truncate(value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds); + return (int)Math.Truncate(value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds); } /// @@ -21,7 +21,7 @@ public static int ToUnixTimestamp(this DateTime value) /// Now expressed as a Unix timestamp public static int UnixTimestamp(this DateTime ignored) { - return (int) Math.Truncate(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds); + return (int)Math.Truncate(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds); } public static DateTime FromUnixTimestamp(int unixTimeStamp) diff --git a/README.md b/README.md index 9390dfb..a2bd90c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- +