7 private readonly EosInterface.
P2PSocket eosSocket;
11 EosInterface.P2PSocket eosSocket)
14 this.eosSocket = eosSocket;
19 if (!EosInterface.Core.IsInitialized) {
return Result.Failure(
new Error(
ErrorCode.EosNotInitialized)); }
22 if (EosInterface.IdQueries.GetLoggedInPuids() is not { Length: > 0 } puids)
26 var socketCreateResult = EosInterface.P2PSocket.Create(puids[0], eosSocketId);
28 if (!socketCreateResult.TryUnwrapSuccess(out var eosSocket)) {
return Result.Failure(
new Error(
ErrorCode.FailedToCreateEosP2PSocket, socketCreateResult.ToString())); }
31 eosSocket.HandleIncomingConnection.Register(
"Event".ToIdentifier(), retVal.OnIncomingConnection);
32 eosSocket.HandleClosedConnection.Register(
"Event".ToIdentifier(), retVal.OnConnectionClosed);
39 foreach (var msg
in eosSocket.GetMessageBatch())
41 callbacks.OnData(
new EosP2PEndpoint(msg.Sender),
new ReadWriteMessage(msg.Buffer, 0, msg.ByteLength * 8,
false));
47 if (endpoint is not
EosP2PEndpoint { ProductUserId: var puid }) {
return false; }
48 var sendResult = eosSocket.
SendMessage(
new EosInterface.P2PSocket.OutgoingMessage(
49 Buffer: outMsg.Buffer,
50 ByteLength: outMsg.LengthBytes,
52 DeliveryMethod: deliveryMethod));
53 return sendResult.IsSuccess;
56 private void OnIncomingConnection(EosInterface.P2PSocket.IncomingConnectionRequest request)
60 if (
callbacks.OnIncomingConnection(remoteEndpoint))
66 private void OnConnectionClosed(EosInterface.P2PSocket.RemoteConnectionClosed data)
70 var peerDisconnectPacket = PeerDisconnectPacket.WithReason(data.Reason
switch
72 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.Unknown => DisconnectReason.Unknown,
73 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.ClosedByLocalUser => DisconnectReason.Disconnected,
74 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.ClosedByPeer => DisconnectReason.Disconnected,
75 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.TimedOut => DisconnectReason.Timeout,
76 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.TooManyConnections => DisconnectReason.ServerFull,
77 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.InvalidMessage => DisconnectReason.Unknown,
78 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.InvalidData => DisconnectReason.Unknown,
79 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.ConnectionFailed => DisconnectReason.AuthenticationFailed,
80 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.ConnectionClosed => DisconnectReason.Disconnected,
81 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.NegotiationFailed => DisconnectReason.AuthenticationFailed,
82 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.UnexpectedError => DisconnectReason.Unknown,
83 EosInterface.P2PSocket.RemoteConnectionClosed.ConnectionClosedReason.Unhandled => DisconnectReason.Unknown,
84 _ => DisconnectReason.Unknown
86 callbacks.OnConnectionClosed(remoteEndpoint, peerDisconnectPacket);
91 if (endpoint is not
EosP2PEndpoint { ProductUserId: var puid }) {
return; }
override void ProcessIncomingMessages()
override void CloseConnection(P2PEndpoint endpoint)
override bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod)
static Result< P2PSocket, Error > Create(Callbacks callbacks)
readonly record struct Error(ImmutableArray<(ErrorCode Code, string AdditionalInfo)> CodesAndInfo)
readonly record struct Callbacks(Predicate< P2PEndpoint > OnIncomingConnection, Action< P2PEndpoint, PeerDisconnectPacket > OnConnectionClosed, Action< P2PEndpoint, IReadMessage > OnData)
abstract bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod)
readonly Callbacks callbacks
P2PSocket(Callbacks callbacks)
abstract void CloseConnection(P2PEndpoint endpoint)