Client LuaCsForBarotrauma
P2PSocket.cs
1 #nullable enable
2 using System;
3 using System.Collections.Immutable;
4 using System.Linq;
6 
7 namespace Barotrauma.Networking;
8 
9 abstract class P2PSocket : IDisposable
10 {
11  public enum ErrorCode
12  {
13  EosNotInitialized,
14  EosNotLoggedIn,
15  FailedToCreateEosP2PSocket,
16 
17  SteamNotInitialized,
18  FailedToCreateSteamP2PSocket
19  }
20 
21  public readonly record struct Error(
22  ImmutableArray<(ErrorCode Code, string AdditionalInfo)> CodesAndInfo)
23  {
24  public Error(ErrorCode code, string? additionalInfo = "") : this((code, additionalInfo ?? "").ToEnumerable().ToImmutableArray()) { }
25  public Error(params Error[] innerErrors) : this(innerErrors.SelectMany(ie => ie.CodesAndInfo).ToImmutableArray()) { }
26 
27  public override string? ToString()
28  {
29  if (CodesAndInfo.IsDefault)
30  {
31  return "default(Error)";
32  }
33 
34  return $"Errors({string.Join("; ", CodesAndInfo)})";
35  }
36  }
37 
38  public readonly record struct Callbacks(
39  Predicate<P2PEndpoint> OnIncomingConnection,
40  Action<P2PEndpoint, PeerDisconnectPacket> OnConnectionClosed,
41  Action<P2PEndpoint, IReadMessage> OnData);
42  protected readonly Callbacks callbacks;
43 
45  {
46  this.callbacks = callbacks;
47  }
48 
49  public abstract void ProcessIncomingMessages();
50 
51  public abstract bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod);
52 
53  public abstract void CloseConnection(P2PEndpoint endpoint);
54 
55  public abstract void Dispose();
56 }
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)
abstract bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod)
abstract void ProcessIncomingMessages()
readonly Callbacks callbacks
Definition: P2PSocket.cs:42
P2PSocket(Callbacks callbacks)
Definition: P2PSocket.cs:44
abstract void CloseConnection(P2PEndpoint endpoint)
abstract void Dispose()