Client LuaCsForBarotrauma
DualStackP2PSocket.cs
1 #nullable enable
2 
3 namespace Barotrauma.Networking;
4 
6 {
7  private readonly Option<EosP2PSocket> eosSocket;
8  private readonly Option<SteamListenSocket> steamSocket;
9 
10  private DualStackP2PSocket(
12  Option<EosP2PSocket> eosSocket,
13  Option<SteamListenSocket> steamSocket) :
14  base(callbacks)
15  {
16  this.eosSocket = eosSocket;
17  this.steamSocket = steamSocket;
18  }
19 
20  public static Result<P2PSocket, Error> Create(Callbacks callbacks)
21  {
22  var eosP2PSocketResult = EosP2PSocket.Create(callbacks);
23  var steamP2PSocketResult = SteamListenSocket.Create(callbacks);
24  if (eosP2PSocketResult.TryUnwrapFailure(out var eosError)
25  && steamP2PSocketResult.TryUnwrapFailure(out var steamError))
26  {
27  return Result.Failure(new Error(eosError, steamError));
28  }
29  return Result.Success((P2PSocket)new DualStackP2PSocket(
30  callbacks,
31  eosP2PSocketResult.TryUnwrapSuccess(out var eosP2PSocket)
32  ? Option.Some((EosP2PSocket)eosP2PSocket)
33  : Option.None,
34  steamP2PSocketResult.TryUnwrapSuccess(out var steamP2PSocket)
35  ? Option.Some((SteamListenSocket)steamP2PSocket)
36  : Option.None));
37  }
38 
39  public override void ProcessIncomingMessages()
40  {
41  if (eosSocket.TryUnwrap(out var eosP2PSocket)) { eosP2PSocket.ProcessIncomingMessages(); }
42  if (steamSocket.TryUnwrap(out var steamP2PSocket)) { steamP2PSocket.ProcessIncomingMessages(); }
43  }
44 
45  public override bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod)
46  {
47  return endpoint switch
48  {
49  EosP2PEndpoint eosP2PEndpoint when eosSocket.TryUnwrap(out var eosP2PSocket)
50  => eosP2PSocket.SendMessage(eosP2PEndpoint, outMsg, deliveryMethod),
51  SteamP2PEndpoint steamP2PEndpoint when steamSocket.TryUnwrap(out var steamP2PSocket)
52  => steamP2PSocket.SendMessage(steamP2PEndpoint, outMsg, deliveryMethod),
53  _
54  => false
55  };
56  }
57 
58  public override void CloseConnection(P2PEndpoint endpoint)
59  {
60  switch (endpoint)
61  {
62  case EosP2PEndpoint eosP2PEndpoint:
63  if (eosSocket.TryUnwrap(out var eosP2PSocket))
64  {
65  eosP2PSocket.CloseConnection(eosP2PEndpoint);
66  }
67  break;
68  case SteamP2PEndpoint steamP2PEndpoint:
69  if (steamSocket.TryUnwrap(out var steamP2PSocket))
70  {
71  steamP2PSocket.CloseConnection(steamP2PEndpoint);
72  }
73  break;
74  }
75  }
76 
77  public override void Dispose()
78  {
79  if (eosSocket.TryUnwrap(out var eosP2PSocket)) { eosP2PSocket.Dispose(); }
80  if (steamSocket.TryUnwrap(out var steamP2PSocket)) { steamP2PSocket.Dispose(); }
81  }
82 }
override void ProcessIncomingMessages()
static Result< P2PSocket, Error > Create(Callbacks callbacks)
override void CloseConnection(P2PEndpoint endpoint)
override void Dispose()
override bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod)
static Result< P2PSocket, Error > Create(Callbacks callbacks)
Definition: EosP2PSocket.cs:17
readonly record struct Error(ImmutableArray<(ErrorCode Code, string AdditionalInfo)> CodesAndInfo)
Definition: P2PSocket.cs:21
readonly record struct Callbacks(Predicate< P2PEndpoint > OnIncomingConnection, Action< P2PEndpoint, PeerDisconnectPacket > OnConnectionClosed, Action< P2PEndpoint, IReadMessage > OnData)
readonly Callbacks callbacks
Definition: P2PSocket.cs:42
static Result< P2PSocket, Error > Create(Callbacks callbacks)