Skip to content

Commit

Permalink
Allowed IP/Port via rather than just IPEndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Walker committed May 18, 2015
1 parent 47c76c3 commit 312dfed
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 6 deletions.
31 changes: 31 additions & 0 deletions SSQLib/EndPointHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace SSQLib
{
public class EndPointHelpers
{
public static IPEndPoint GetIPEndPointFromHostName(string hostName, int port, bool throwIfMoreThanOneIP)
{
var addresses = System.Net.Dns.GetHostAddresses(hostName);
if (addresses.Length == 0)
{
throw new ArgumentException(
"Unable to retrieve address from specified host name.",
"hostName"
);
}
else if (throwIfMoreThanOneIP && addresses.Length > 1)
{
throw new ArgumentException(
"There is more that one IP address to the specified host.",
"hostName"
);
}
return new IPEndPoint(addresses[0], port); // Port gets validated here.
}
}
}
29 changes: 27 additions & 2 deletions SSQLib/SSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ public SSQL()
{

}

/// <summary>
/// Pings the specified Source server to retreive information about it such as the server name, max players, current number of players, etc.
/// </summary>
/// <param name="ip">The string containing the IP address or hostname of the server</param>
/// <param name="port">The port of the server</param>
/// <returns>Information about the server or throws an SSQLServerException if it could not be retreived</returns>
public ServerInfo Server(string ip, int port)
{
IPEndPoint endPoint = EndPointHelpers.GetIPEndPointFromHostName(ip, port, true);
return Server(endPoint);
}

/// <summary>
/// Pings the specified Source server to retreive information about it such as the server name, max players, current number of players, etc.
/// </summary>
/// <param name="ip_end">The IPEndPoint object containing the IP address and port of the server</param>
/// <returns>Information about the server or throws an SSQLServerException if it could not be retreived</returns>
public ServerInfo Server(IPEndPoint ip_end)
public ServerInfo Server(EndPoint ip_end)
{
//Create a new empty server info object
ServerInfo info = new ServerInfo();
Expand Down Expand Up @@ -188,11 +201,23 @@ public ServerInfo Server(IPEndPoint ip_end)
return info;
}

/// <summary>
/// Retreives information about the players on a Source server
/// </summary>
/// <param name="ip">The string containing the IP address or hostname of the server</param>
/// <param name="port">The port of the server</param>
/// <returns>A List<> of PlayerInfo or throws an SSQLServerException if the server could not be reached</returns>
public List<PlayerInfo> Players(string ip, int port)
{
IPEndPoint endPoint = EndPointHelpers.GetIPEndPointFromHostName(ip, port, true);
return Players(endPoint);
}

/// <summary>
/// Retreives information about the players on a Source server
/// </summary>
/// <param name="ip_end">The IPEndPoint object storing the IP address and port of the server</param>
/// <returns>An ArrayList of PlayerInfo or throws an SSQLServerException if the server could not be reached</returns>
/// <returns>A List<> of PlayerInfo or throws an SSQLServerException if the server could not be reached</returns>
public List<PlayerInfo> Players(IPEndPoint ip_end)
{
//Create a new array list to store the player array
Expand Down
1 change: 1 addition & 0 deletions SSQLib/SSQLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EndPointHelpers.cs" />
<Compile Include="Packet.cs" />
<Compile Include="PlayerInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
4 changes: 2 additions & 2 deletions SSQLib/SocketUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class SocketUtils
{
private SocketUtils() { }

internal static byte[] getInfo(IPEndPoint ipe, Packet packet)
internal static byte[] getInfo(EndPoint ipe, Packet packet)
{
//Create the socket
Socket srvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Expand All @@ -52,7 +52,7 @@ internal static byte[] getInfo(IPEndPoint ipe, Packet packet)

//Create a new receive buffer
byte[] rcvPacketInfo = new byte[packetSize];
EndPoint Remote = (EndPoint)ipe;
EndPoint Remote = ipe;

try
{
Expand Down
4 changes: 4 additions & 0 deletions Test.SSQLib/Test.SSQLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests.SSQLib.Player.cs" />
<Compile Include="Tests.SSQLib.Server.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -57,6 +58,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
31 changes: 31 additions & 0 deletions Test.SSQLib/Tests.SSQLib.Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using SSQLib;
using System.Net;
using Xunit;

namespace Test.SSQLib
{
public class PlayerTests
{
[Fact]
public void TestFakePlayerServerThrowsExceptionWithIpEndpoint()
{
Assert.Throws(typeof(SSQLServerException), () =>
{
SSQL query = new SSQL();
//127.0.0.2 set to stop loopback potentially resolving and failing test
query.Players(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 27015));
});
}

[Fact]
public void TestFakePlayerServerThrowsExceptionWithStringIp()
{
Assert.Throws(typeof(SSQLServerException), () =>
{
SSQL query = new SSQL();
//127.0.0.2 set to stop loopback potentially resolving and failing test
query.Players("127.0.0.2", 27015);
});
}
}
}
16 changes: 14 additions & 2 deletions Test.SSQLib/Tests.SSQLib.Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ namespace Test.SSQLib
public class ServerTests
{
[Fact]
public void TestFakeServerThrowsException()
public void TestFakeServerThrowsExceptionWithIpEndpoint()
{
Assert.Throws(typeof(SSQLServerException), () =>
{
SSQL query = new SSQL();
query.Server(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27015));
//127.0.0.2 set to stop loopback potentially resolving and failing test
query.Server(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 27015));
});
}

[Fact]
public void TestFakeServerThrowsExceptionWithStringIp()
{
Assert.Throws(typeof(SSQLServerException), () =>
{
SSQL query = new SSQL();
//127.0.0.2 set to stop loopback potentially resolving and failing test
query.Server("127.0.0.2", 27015);
});
}
}
}

0 comments on commit 312dfed

Please sign in to comment.