-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProtocolBase.cs
62 lines (56 loc) · 1.84 KB
/
ProtocolBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Net;
using System.Threading.Tasks;
namespace OpenGSQ
{
/// <summary>
/// Abstract base class for protocols.
/// </summary>
public abstract class ProtocolBase
{
/// <summary>
/// Gets the full name of the protocol.
/// </summary>
public abstract string FullName { get; }
/// <summary>
/// Gets the host to connect to.
/// </summary>
public string Host { get; private set; }
/// <summary>
/// Gets the port to connect to.
/// </summary>
public int Port { get; private set; }
/// <summary>
/// The timeout for the connection in seconds.
/// </summary>
public int Timeout { get; private set; }
/// <summary>
/// Initializes a new instance of the ProtocolBase class.
/// </summary>
/// <param name="host">The host to connect to.</param>
/// <param name="port">The port to connect to.</param>
/// <param name="timeout">The connection timeout in milliseconds. Default is 5 seconds.</param>
public ProtocolBase(string host, int port, int timeout = 5000)
{
Host = host;
Port = port;
Timeout = timeout;
}
/// <summary>
/// Retrieves the IP address of the host.
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the IP address of the host.
/// </returns>
protected async Task<string> GetIPAddress()
{
if (IPAddress.TryParse(Host, out var address))
{
return await Task.FromResult(address.ToString());
}
else
{
return (await Dns.GetHostAddressesAsync(Host))[0].ToString();
}
}
}
}