Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit d39005d

Browse files
committed
Added help command example
Added DateTimeExtension tests Made Commands a public get only property Remove unused exception ctors
1 parent aa891cf commit d39005d

File tree

6 files changed

+133
-108
lines changed

6 files changed

+133
-108
lines changed

Examples/ServerExample/Program.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net;
3+
using System.Text;
34
using RCONServerLib;
45

56
namespace ServerExample
@@ -13,7 +14,39 @@ public static void Main(string[] args)
1314
SendAuthImmediately = true,
1415
Debug = true,
1516
};
16-
server.CommandManager.Add("hello", "", (command, arguments) => { return "world"; });
17+
server.CommandManager.Add("hello", "Echos back world", (command, arguments) => { return "world"; });
18+
server.CommandManager.Add("help", "(command)", "Shows this help", (cmd, arguments) =>
19+
{
20+
if (arguments.Count == 1)
21+
{
22+
var helpCmdStr = arguments[0];
23+
var helpCmd = server.CommandManager.GetCommand(helpCmdStr);
24+
if (helpCmd == null)
25+
return "Command not found.";
26+
27+
return string.Format("{0} - {1}", helpCmd.Name, helpCmd.Description);
28+
}
29+
else
30+
{
31+
var sb = new StringBuilder();
32+
33+
var all = server.CommandManager.Commands.Count;
34+
var i = 0;
35+
foreach (var command in server.CommandManager.Commands)
36+
{
37+
if (command.Value.Usage == "")
38+
sb.AppendFormat("{0}", command.Value.Name);
39+
else
40+
sb.AppendFormat("{0} {1}", command.Value.Name, command.Value.Usage);
41+
if (i < all)
42+
sb.Append(", ");
43+
44+
i++;
45+
}
46+
47+
return sb.ToString();
48+
}
49+
});
1750

1851
server.StartListening();
1952

RCONServerLib.Tests/Tests.cs

Lines changed: 92 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -53,79 +53,100 @@ public void RconPacketTypeTest()
5353
0xFF, 0xFF, 0x00, 0x00 // Type
5454
}));
5555
}
56-
57-
[Fact]
58-
public void IpPatternMatchTest()
59-
{
60-
Assert.True(IpExtension.Match("127.0.0.1", "127.0.0.1"));
61-
Assert.True(IpExtension.Match("192.*.*.*", "192.168.178.1"));
62-
Assert.True(IpExtension.Match("192.*.*.*", "192.255.255.255"));
63-
64-
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.2"));
65-
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.9"));
66-
Assert.False(IpExtension.Match("192.*.*.*", "178.75.68.49"));
67-
}
6856

69-
[Fact]
70-
public void RconPacketSuccessTest()
71-
{
72-
var packet = new RemoteConPacket(new byte[]
73-
{
74-
0x0A, 0x00, 0x00, 0x00, // Size
75-
0x02, 0x00, 0x00, 0x00, // Id
76-
0x03, 0x00, 0x00, 0x00, // Type
77-
0x00,
78-
0x00,
79-
});
80-
81-
Assert.Equal(2, packet.Id);
82-
Assert.Equal(10, packet.Size);
83-
Assert.Equal(RemoteConPacket.PacketType.Auth, packet.Type);
84-
}
57+
[Fact]
58+
public void IpPatternMatchTest()
59+
{
60+
Assert.True(IpExtension.Match("127.0.0.1", "127.0.0.1"));
61+
Assert.True(IpExtension.Match("192.*.*.*", "192.168.178.1"));
62+
Assert.True(IpExtension.Match("192.*.*.*", "192.255.255.255"));
63+
64+
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.2"));
65+
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.9"));
66+
Assert.False(IpExtension.Match("192.*.*.*", "178.75.68.49"));
67+
}
68+
69+
[Fact]
70+
public void RconPacketSuccessTest()
71+
{
72+
var packet = new RemoteConPacket(new byte[]
73+
{
74+
0x0A, 0x00, 0x00, 0x00, // Size
75+
0x02, 0x00, 0x00, 0x00, // Id
76+
0x03, 0x00, 0x00, 0x00, // Type
77+
0x00,
78+
0x00,
79+
});
80+
81+
Assert.Equal(2, packet.Id);
82+
Assert.Equal(10, packet.Size);
83+
Assert.Equal(RemoteConPacket.PacketType.Auth, packet.Type);
84+
}
85+
86+
[Fact]
87+
public void RconPacketGetBytesTest()
88+
{
89+
var testBytes = new byte[]
90+
{
91+
0x0A, 0x00, 0x00, 0x00, // Size
92+
0x02, 0x00, 0x00, 0x00, // Id
93+
0x03, 0x00, 0x00, 0x00, // Type
94+
0x00,
95+
0x00,
96+
};
97+
var packet = new RemoteConPacket(2, RemoteConPacket.PacketType.Auth, "");
8598

86-
[Fact]
87-
public void RconPacketGetBytesTest()
88-
{
89-
var testBytes = new byte[]
90-
{
91-
0x0A, 0x00, 0x00, 0x00, // Size
92-
0x02, 0x00, 0x00, 0x00, // Id
93-
0x03, 0x00, 0x00, 0x00, // Type
94-
0x00,
95-
0x00,
96-
};
97-
var packet = new RemoteConPacket(2, RemoteConPacket.PacketType.Auth, "");
99+
var packetBytes = packet.GetBytes();
100+
for (var index = 0; index < testBytes.Length; index++)
101+
Assert.Equal(testBytes[index], packetBytes[index]);
102+
}
98103

99-
var packetBytes = packet.GetBytes();
100-
for (var index = 0; index < testBytes.Length; index++)
101-
Assert.Equal(testBytes[index], packetBytes[index]);
102-
}
103-
104-
[Fact]
105-
public void ExtendedBinaryReaderTest()
106-
{
107-
var testBytes = new byte[]
108-
{
109-
0x15, 0x00, 0x00, 0x00, // Size
110-
0x02, 0x00, 0x00, 0x00, // Id
111-
0x03, 0x00, 0x00, 0x00, // Type
112-
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00,
113-
0x00,
114-
};
115-
using(var ms = new MemoryStream(testBytes))
116-
{
117-
using(var reader = new BinaryReaderExt(ms))
118-
{
119-
var sizeTestValue = reader.ReadInt32LittleEndian();
120-
Assert.Equal(21, sizeTestValue);
121-
var idTestValue = reader.ReadInt32LittleEndian();
122-
Assert.Equal(2, idTestValue);
123-
var typeTestValue = reader.ReadInt32LittleEndian();
124-
Assert.Equal(3, typeTestValue);
125-
var payloadTestValue = reader.ReadAscii();
126-
Assert.Equal("Hello World", payloadTestValue);
127-
}
128-
}
129-
}
104+
[Fact]
105+
public void ExtendedBinaryReaderTest()
106+
{
107+
var testBytes = new byte[]
108+
{
109+
0x15, 0x00, 0x00, 0x00, // Size
110+
0x02, 0x00, 0x00, 0x00, // Id
111+
0x03, 0x00, 0x00, 0x00, // Type
112+
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00,
113+
0x00,
114+
};
115+
using (var ms = new MemoryStream(testBytes))
116+
{
117+
using (var reader = new BinaryReaderExt(ms))
118+
{
119+
var sizeTestValue = reader.ReadInt32LittleEndian();
120+
Assert.Equal(21, sizeTestValue);
121+
var idTestValue = reader.ReadInt32LittleEndian();
122+
Assert.Equal(2, idTestValue);
123+
var typeTestValue = reader.ReadInt32LittleEndian();
124+
Assert.Equal(3, typeTestValue);
125+
var payloadTestValue = reader.ReadAscii();
126+
Assert.Equal("Hello World", payloadTestValue);
127+
}
128+
}
129+
}
130+
131+
[Fact]
132+
public void DateTimeTest()
133+
{
134+
var nowTime = DateTime.Now;
135+
var unix = nowTime.ToUnixTimestamp();
136+
Assert.True(nowTime.Day == DateTimeExtensions.FromUnixTimestamp(unix).Day, "Day doesn't match");
137+
Assert.True(nowTime.Month == DateTimeExtensions.FromUnixTimestamp(unix).Month, "Month doesn't match");
138+
Assert.True(nowTime.Year == DateTimeExtensions.FromUnixTimestamp(unix).Year, "Year doesn't match");
139+
Assert.True(nowTime.Hour == DateTimeExtensions.FromUnixTimestamp(unix).Hour, "Hour doesn't match");
140+
Assert.True(nowTime.Minute == DateTimeExtensions.FromUnixTimestamp(unix).Minute, "Minute doesn't match");
141+
Assert.True(nowTime.Second == DateTimeExtensions.FromUnixTimestamp(unix).Second, "Second doesn't match");
142+
143+
var unix2 = DateTime.Now.UnixTimestamp();
144+
Assert.True(nowTime.Day == DateTimeExtensions.FromUnixTimestamp(unix2).Day, "Day doesn't match");
145+
Assert.True(nowTime.Month == DateTimeExtensions.FromUnixTimestamp(unix2).Month, "Month doesn't match");
146+
Assert.True(nowTime.Year == DateTimeExtensions.FromUnixTimestamp(unix2).Year, "Year doesn't match");
147+
Assert.True(nowTime.Hour == DateTimeExtensions.FromUnixTimestamp(unix2).Hour, "Hour doesn't match");
148+
Assert.True(nowTime.Minute == DateTimeExtensions.FromUnixTimestamp(unix2).Minute, "Minute doesn't match");
149+
Assert.True(nowTime.Second == DateTimeExtensions.FromUnixTimestamp(unix2).Second, "Second doesn't match");
150+
}
130151
}
131152
}

RCONServerLib/RCONServerLib.nuspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>source-rcon-server</id>
5-
<version>1.2.1</version>
5+
<version>1.3.0</version>
66
<title>Source RCON Server Lib</title>
77
<authors>Julien H. (Subtixx)</authors>
88
<owners>Julien H. (Subtixx)</owners>
@@ -12,11 +12,11 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>
1414
Easy to use, single-class solution to create a Valve source RCON server which supports authentication,
15-
IP Whitelisting and a command manager
15+
IP Whitelisting, a command manager and much more!
1616
</description>
1717
<summary>Single class solution to create a source compatible RCON interface</summary>
18-
<releaseNotes>Initial Release.</releaseNotes>
19-
<copyright>Copyright Subtixx 2018</copyright>
18+
<releaseNotes>Please see https://github.com/Subtixx/source-rcon-library/releases for release notes!</releaseNotes>
19+
<copyright>Copyright Julien H. (Subtixx) 2018</copyright>
2020
<tags>source-rcon-server rconserver remoteconsole rcon-server</tags>
2121
</metadata>
2222
<files>

RCONServerLib/Utils/CommandManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace RCONServerLib.Utils
77
/// </summary>
88
public class CommandManager
99
{
10-
protected Dictionary<string, Command> Commands;
10+
public Dictionary<string, Command> Commands { get; private set; }
1111

1212
public CommandManager()
1313
{

RCONServerLib/Utils/Exceptions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,25 @@ protected RconServerException(string message) : base(message)
1515

1616
public class NotAuthenticatedException : RconServerException
1717
{
18-
public NotAuthenticatedException()
19-
{
20-
}
21-
22-
public NotAuthenticatedException(string message) : base(message)
23-
{
24-
}
2518
}
2619

2720
public class EmptyPacketPayloadException : RconServerException
2821
{
29-
public EmptyPacketPayloadException()
30-
{
31-
}
32-
33-
public EmptyPacketPayloadException(string message) : base(message)
34-
{
35-
}
3622
}
3723

3824
public class PacketTooLongException : RconServerException
3925
{
40-
public PacketTooLongException()
41-
{
42-
}
43-
44-
public PacketTooLongException(string message) : base(message)
45-
{
46-
}
4726
}
4827

4928
public class NullTerminatorMissingException : RconServerException
5029
{
51-
public NullTerminatorMissingException()
52-
{
53-
}
54-
5530
public NullTerminatorMissingException(string message) : base(message)
5631
{
5732
}
5833
}
5934

6035
public class LengthMismatchException : RconServerException
6136
{
62-
public LengthMismatchException()
63-
{
64-
}
65-
6637
public LengthMismatchException(string message) : base(message)
6738
{
6839
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ![Logo](https://user-images.githubusercontent.com/20743379/35411973-0227102e-021b-11e8-9a1b-023e08c33c4e.png) Source RCON Library [![Maintenance](https://img.shields.io/maintenance/yes/2018.svg)]()
22

3-
Source RCON Library is an easy to use, single-class solution to create a [Valve source RCON](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) server which supports authentication,
4-
IP Whitelisting and a command manager
3+
Source RCON Library is an easy to use, single-class solution to create a [Valve source RCON](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) server
4+
which supports authentication, IP Whitelisting, a command manager and much more!
55

66
* [NuGet](#nuget)
77
* [Examples](#examples)

0 commit comments

Comments
 (0)