Client LuaCsForBarotrauma
LidgrenEndpoint.cs
1 #nullable enable
2 using System.Linq;
3 using System.Net;
4 
5 namespace Barotrauma.Networking
6 {
7  sealed class LidgrenEndpoint : Endpoint
8  {
9  public readonly IPEndPoint NetEndpoint;
10 
11  public int Port => NetEndpoint.Port;
12 
13  public override string StringRepresentation
14  => NetEndpoint.ToString();
15 
16  public override LocalizedString ServerTypeString { get; } = TextManager.Get("DedicatedServer");
17 
18  public LidgrenEndpoint(IPAddress address, int port) : this(new IPEndPoint(address, port)) { }
19 
20  public LidgrenEndpoint(IPEndPoint netEndpoint) : base(new LidgrenAddress(netEndpoint.Address))
21  {
22  NetEndpoint = new IPEndPoint((Address as LidgrenAddress)!.NetAddress, netEndpoint.Port);
23  }
24 
25  public new static Option<LidgrenEndpoint> Parse(string endpointStr)
26  {
27  return ParseFromWithHostNameCheck(endpointStr, tryParseHostName: false);
28  }
29 
30  public static Option<LidgrenEndpoint> ParseFromWithHostNameCheck(string endpointStr, bool tryParseHostName)
31  {
32  string hostName = endpointStr;
33  int port = NetConfig.DefaultPort;
34  if (endpointStr.Count(c => c == ':') == 1)
35  {
36  string[] split = endpointStr.Split(':');
37  hostName = split[0];
38  port = int.TryParse(split[1], out var tmpPort) ? tmpPort : port;
39  }
40 
41  if (LidgrenAddress.Parse(hostName).TryUnwrap(out var adr) ||
42  (tryParseHostName && LidgrenAddress.ParseHostName(hostName).TryUnwrap(out adr)))
43  {
44  return Option<LidgrenEndpoint>.Some(new LidgrenEndpoint(adr.NetAddress, port));
45  }
46 
47  return IPEndPoint.TryParse(endpointStr, out IPEndPoint? netEndpoint)
48  ? Option<LidgrenEndpoint>.Some(new LidgrenEndpoint(netEndpoint))
49  : Option<LidgrenEndpoint>.None();
50  }
51 
52  public override bool Equals(object? obj)
53  => obj switch
54  {
55  LidgrenEndpoint otherEndpoint => this == otherEndpoint,
56  _ => false
57  };
58 
59  public override int GetHashCode()
60  => NetEndpoint.GetHashCode();
61 
62  public static bool operator ==(LidgrenEndpoint a, LidgrenEndpoint b)
63  => a.NetEndpoint.EquivalentTo(b.NetEndpoint);
64 
65  public static bool operator !=(LidgrenEndpoint a, LidgrenEndpoint b)
66  => !(a == b);
67  }
68 }
readonly Address Address
Definition: Endpoint.cs:11
LidgrenEndpoint(IPAddress address, int port)
static bool operator!=(LidgrenEndpoint a, LidgrenEndpoint b)
static bool operator==(LidgrenEndpoint a, LidgrenEndpoint b)
static new Option< LidgrenEndpoint > Parse(string endpointStr)
LidgrenEndpoint(IPEndPoint netEndpoint)
override bool Equals(object? obj)
override LocalizedString ServerTypeString
static Option< LidgrenEndpoint > ParseFromWithHostNameCheck(string endpointStr, bool tryParseHostName)