Skip to content

Commit e46002a

Browse files
committed
Add empty ctor and Connect function to make ServerQuery a bit more testable.
1 parent fe98040 commit e46002a

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

SteamQueryNet/SteamQueryNet/ServerQuery.cs

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public interface IServerQuery
2020
/// </summary>
2121
void RenewChallenge();
2222

23+
/// <summary>
24+
/// Configures and Connects the created instance of SteamQuery UDP socket for Steam Server Query Operations.
25+
/// </summary>
26+
/// <param name="serverAddress">IPAddress or HostName of the server that queries will be sent.</param>
27+
/// <param name="port">Port of the server that queries will be sent.</param>
28+
void Connect(string serverAddress, int port);
29+
2330
/// <summary>
2431
/// Requests and serializes the server information.
2532
/// </summary>
@@ -46,10 +53,10 @@ public class ServerQuery : IServerQuery, IDisposable
4653
private const int RESPONSE_HEADER_COUNT = 6;
4754
private const int RESPONSE_CODE_INDEX = 5;
4855

49-
private readonly int _port;
5056
private readonly UdpClient _client = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
5157
private IPEndPoint _ipEndpoint;
5258

59+
private int _port;
5360
private int _currentChallenge;
5461

5562
/// <summary>
@@ -73,51 +80,25 @@ public bool IsConnected
7380
/// </summary>
7481
public int ReceiveTimeout { get; set; }
7582

83+
/// <summary>
84+
/// Creates a new instance of ServerQuery without UDP socket connection.
85+
/// </summary>
86+
public ServerQuery() { }
87+
7688
/// <summary>
7789
/// Creates a new ServerQuery instance for Steam Server Query Operations.
7890
/// </summary>
7991
/// <param name="serverAddress">IPAddress or HostName of the server that queries will be sent.</param>
8092
/// <param name="port">Port of the server that queries will be sent.</param>
8193
public ServerQuery(string serverAddress, int port)
8294
{
83-
// Check the port range
84-
if (_port < IPEndPoint.MinPort || _port > IPEndPoint.MaxPort)
85-
{
86-
throw new ArgumentException($"Port should be between {IPEndPoint.MinPort} and {IPEndPoint.MaxPort}");
87-
}
88-
89-
_port = port;
90-
// Try to parse the serverAddress as IP first
91-
if (IPAddress.TryParse(serverAddress, out IPAddress parsedIpAddress))
92-
{
93-
// Yep its an IP.
94-
_ipEndpoint = new IPEndPoint(parsedIpAddress, _port);
95-
}
96-
else
97-
{
98-
// Nope it might be a hostname.
99-
try
100-
{
101-
IPAddress[] addresslist = Dns.GetHostAddresses(serverAddress);
102-
if (addresslist.Length > 0)
103-
{
104-
// We get the first address.
105-
_ipEndpoint = new IPEndPoint(addresslist[0], _port);
106-
}
107-
else
108-
{
109-
throw new ArgumentException($"Invalid host address {serverAddress}");
110-
}
111-
}
112-
catch (SocketException ex)
113-
{
114-
throw new ArgumentException("Could not reach the hostname.", ex);
115-
}
116-
}
95+
PrepareAndConnect(serverAddress, port);
96+
}
11797

118-
_client.Client.SendTimeout = SendTimeout;
119-
_client.Client.ReceiveTimeout = ReceiveTimeout;
120-
_client.Connect(_ipEndpoint);
98+
/// <inheritdoc/>
99+
public void Connect(string serverAddress, int port)
100+
{
101+
PrepareAndConnect(serverAddress, port);
121102
}
122103

123104
/// <inheritdoc/>
@@ -196,6 +177,49 @@ public void Dispose()
196177
_client.Dispose();
197178
}
198179

180+
private void PrepareAndConnect(string serverAddress, int port)
181+
{
182+
_port = port;
183+
184+
// Check the port range
185+
if (_port < IPEndPoint.MinPort || _port > IPEndPoint.MaxPort)
186+
{
187+
throw new ArgumentException($"Port should be between {IPEndPoint.MinPort} and {IPEndPoint.MaxPort}");
188+
}
189+
190+
// Try to parse the serverAddress as IP first
191+
if (IPAddress.TryParse(serverAddress, out IPAddress parsedIpAddress))
192+
{
193+
// Yep its an IP.
194+
_ipEndpoint = new IPEndPoint(parsedIpAddress, _port);
195+
}
196+
else
197+
{
198+
// Nope it might be a hostname.
199+
try
200+
{
201+
IPAddress[] addresslist = Dns.GetHostAddresses(serverAddress);
202+
if (addresslist.Length > 0)
203+
{
204+
// We get the first address.
205+
_ipEndpoint = new IPEndPoint(addresslist[0], _port);
206+
}
207+
else
208+
{
209+
throw new ArgumentException($"Invalid host address {serverAddress}");
210+
}
211+
}
212+
catch (SocketException ex)
213+
{
214+
throw new ArgumentException("Could not reach the hostname.", ex);
215+
}
216+
}
217+
218+
_client.Client.SendTimeout = SendTimeout;
219+
_client.Client.ReceiveTimeout = ReceiveTimeout;
220+
_client.Connect(_ipEndpoint);
221+
}
222+
199223
private List<TObject> ExtractListData<TObject>(byte[] rawSource)
200224
where TObject : class
201225
{

0 commit comments

Comments
 (0)