Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions Assets/Scripts/EOSPeer2PeerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public class EOSPeer2PeerManager : IEOSSubManager
private P2PInterface P2PHandle;

private ulong ConnectionNotificationId;
private ulong ConnectionEstablishedNotificationId;
private ulong ConnectionInterruptedNotificationId;
private Dictionary<ProductUserId, ChatWithFriendData> ChatDataCache;
private bool ChatDataCacheDirty;

Expand Down Expand Up @@ -144,12 +146,20 @@ public void Initialize()
{
SubscribeToConnectionRequest();

var options = new AddNotifyPeerConnectionEstablishedOptions
var localUserId = EOSManager.Instance.GetProductUserId();

var establishedOptions = new AddNotifyPeerConnectionEstablishedOptions
{
LocalUserId = EOSManager.Instance.GetProductUserId()
LocalUserId = localUserId
};
ConnectionEstablishedNotificationId = P2PHandle.AddNotifyPeerConnectionEstablished(ref establishedOptions, null, OnPeerConnectionEstablished);

P2PHandle.AddNotifyPeerConnectionEstablished(ref options, null, OnPeerConnectionEstablished);
var interruptedOptions = new AddNotifyPeerConnectionInterruptedOptions
{
LocalUserId = localUserId,
SocketId = null
};
ConnectionInterruptedNotificationId = P2PHandle.AddNotifyPeerConnectionInterrupted(ref interruptedOptions, null, OnPeerConnectionInterrupted);

Debug.Log("EOSPeer2PeerManager initialized: connection listeners registered.");
}
Expand Down Expand Up @@ -188,6 +198,18 @@ public void OnLoggedIn()
public void OnLoggedOut()
{
UnsubscribeFromConnectionRequests();

if (ConnectionEstablishedNotificationId != 0)
{
P2PHandle.RemoveNotifyPeerConnectionEstablished(ConnectionEstablishedNotificationId);
ConnectionEstablishedNotificationId = 0;
}

if (ConnectionInterruptedNotificationId != 0)
{
P2PHandle.RemoveNotifyPeerConnectionInterrupted(ConnectionInterruptedNotificationId);
ConnectionInterruptedNotificationId = 0;
}
}

private void OnRefreshNATTypeFinished(ref OnQueryNATTypeCompleteInfo data)
Expand Down Expand Up @@ -533,7 +555,7 @@ private void SendRaw(ProductUserId remoteUserId, string rawMessage)
}
private void OnPeerConnectionEstablished(ref OnPeerConnectionEstablishedInfo info)
{
Debug.Log($"[P2P] Connection established with {LoggingUtils.Redact(info.RemoteUserId)}");
Debug.Log($"[P2P] Connection established with {LoggingUtils.Redact(info.RemoteUserId)} | type={info.ConnectionType} network={info.NetworkType}");

if (!connectionStates.ContainsKey(info.RemoteUserId))
{
Expand All @@ -542,6 +564,11 @@ private void OnPeerConnectionEstablished(ref OnPeerConnectionEstablishedInfo inf
connectionStates[info.RemoteUserId] = PeerConnectionAppState.HandshakePending;
}
}

private void OnPeerConnectionInterrupted(ref OnPeerConnectionInterruptedInfo info)
{
Debug.LogWarning($"[P2P] Connection interrupted with {LoggingUtils.Redact(info.RemoteUserId)} on socket '{info.SocketId?.SocketName}' - EOS will attempt auto-recovery");
}
public void SendTrigger(ProductUserId peerId)
{
if (!peerId.IsValid()) return;
Expand Down
62 changes: 62 additions & 0 deletions Assets/Scripts/P2PNetcodeSample/Networking/EOSTransportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ public string DebugStringJSON()

private const byte ConnectionConfirmationChannel = byte.MaxValue;

private ulong ConnectionEstablishedNotificationsId = 0;

private ulong ConnectionInterruptedNotificationsId = 0;

[System.Diagnostics.Conditional("EOS_TRANSPORTMANAGER_DEBUG")]
private void Log(string msg)
{
Expand Down Expand Up @@ -424,6 +428,8 @@ public bool Initialize()
IsInitialized = true;
SubscribeToConnectionRequestNotifications();
SubscribeToConnectionClosedNotifications();
SubscribeToConnectionEstablishedNotifications();
SubscribeToConnectionInterruptedNotifications();
QueryNATType();
}

Expand All @@ -443,6 +449,8 @@ public void Shutdown()

Log($"EOSTransportManager.Shutdown: Shutting down EOSTransportManager... | EOSTransportManager={GetDebugString()}");
CloseAllConnections();
UnsubscribeFromConnectionInterruptedNotifications();
UnsubscribeFromConnectionEstablishedNotifications();
UnsubscribeFromConnectionClosedNotifications();
UnsubscribeFromConnectionRequestNotifications();
Clear();
Expand Down Expand Up @@ -1350,6 +1358,60 @@ private void OnConnectionClosedNotification(ref OnRemoteConnectionClosedInfo dat
CloseConnection(remoteUserId, socketName, true);
}

private void SubscribeToConnectionEstablishedNotifications()
{
var options = new AddNotifyPeerConnectionEstablishedOptions()
{
LocalUserId = LocalUserId,
SocketId = null,
};

ConnectionEstablishedNotificationsId = P2PHandle.AddNotifyPeerConnectionEstablished(ref options, null, OnConnectionEstablishedNotification);
}

private void UnsubscribeFromConnectionEstablishedNotifications()
{
if (ConnectionEstablishedNotificationsId != 0)
{
P2PHandle?.RemoveNotifyPeerConnectionEstablished(ConnectionEstablishedNotificationsId);
ConnectionEstablishedNotificationsId = 0;
}
}

private void OnConnectionEstablishedNotification(ref OnPeerConnectionEstablishedInfo data)
{
Debug.Assert(data.LocalUserId == LocalUserId);
Log($"EOSTransportManager.OnConnectionEstablishedNotification: Connection established with remote peer '{LoggingUtils.Redact(data.RemoteUserId)}' " +
$"on socket '{data.SocketId?.SocketName}' | type={data.ConnectionType} network={data.NetworkType}");
}

private void SubscribeToConnectionInterruptedNotifications()
{
var options = new AddNotifyPeerConnectionInterruptedOptions()
{
LocalUserId = LocalUserId,
SocketId = null,
};

ConnectionInterruptedNotificationsId = P2PHandle.AddNotifyPeerConnectionInterrupted(ref options, null, OnConnectionInterruptedNotification);
}

private void UnsubscribeFromConnectionInterruptedNotifications()
{
if (ConnectionInterruptedNotificationsId != 0)
{
P2PHandle?.RemoveNotifyPeerConnectionInterrupted(ConnectionInterruptedNotificationsId);
ConnectionInterruptedNotificationsId = 0;
}
}

private void OnConnectionInterruptedNotification(ref OnPeerConnectionInterruptedInfo data)
{
Debug.Assert(data.LocalUserId == LocalUserId);
LogWarning($"EOSTransportManager.OnConnectionInterruptedNotification: Connection interrupted with remote peer '{LoggingUtils.Redact(data.RemoteUserId)}' " +
$"on socket '{data.SocketId?.SocketName}' - EOS will attempt auto-recovery");
}

public bool StartHost()
{
#if !COM_UNITY_MODULE_NETCODE
Expand Down
12 changes: 12 additions & 0 deletions com.playeveryware.eos/Runtime/Core/EOSManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,18 @@ private void ConfigureConnectExpirationCallback(Epic.OnlineServices.Connect.Logi
ulong callbackHandle = EOSConnectInterface.AddNotifyAuthExpiration(
ref addNotifyAuthExpirationOptions, null, (ref AuthExpirationCallbackInfo callbackInfo) =>
{
if (connectLoginOptions.Credentials.HasValue &&
connectLoginOptions.Credentials.Value.Type == ExternalCredentialType.EpicIdToken)
{
var epicAccountId = GetLocalUserId();
if (epicAccountId != null && epicAccountId.IsValid())
{
// connectLoginOptions captures the JWT string from initial login; by expiration
// time that JWT is also stale. Fetch a fresh one from the Auth interface instead.
StartConnectLoginWithEpicAccount(epicAccountId, null);
return;
}
}
StartConnectLoginWithOptions(connectLoginOptions, null);
});

Expand Down