Cannot Disconnect / Incorrectly flagged as Connected

Hey All,

I've run into a bit of a confusing situation. I can connect to the Photon Cloud without problem; however, after I have disconnected it will still show me as being connected, causing a disconnect call to hang indefinitely. The below function is used to disconnect when needed.

[code2=csharp]IEnumerator resetNetwork()
{
if (PhotonNetwork.connected) // First off, make sure we are connected before bothering to disconnect
{
try
{
PhotonNetwork.Disconnect();
}
catch (Exception err)
{
Debug.LogError("Failed to Disconnect with error: " + err.ToString());
yield break; // If the Disconnect call failed, cancel the function to make sure we get the Error message
}
while (PhotonNetwork.connectionState != ConnectionState.Disconnected) // Wait here till we verify that we disconnected
{
if (PhotonNetwork.connectionState != ConnectionState.Disconnecting) // If we are not attempting to disconnect for some reason, retry the D/C call
{
try
{
PhotonNetwork.Disconnect();
}
catch (Exception err)
{
Debug.LogError("Failed to Disconnect with error: " + err.ToString());
break;
}
}
yield return 0;
//Hangs in this loop
}
yield return 0;
}
Debug.Log("Disconnect Complete");
PhotonNetwork.offlineMode = true;
}[/code2]

I call this function Before starting either Single or Multiplayer (In the case of Single Player, to make sure I'm in Offline mode and not sending useless RPCs, and in Multiplayer, to get a clean reconnect)

If I choose Multiplayer first, the flow goes like this:
(Check to see if I'm connected => I'm not => skip disconnect call => attempt connection => success => Go to Multiplayer Game)
If I back out, and go back into Multiplayer:
(Check to see if I'm Connected => I am => attempt disconnect => success => attempt reconnect => success=> Go to Multiplayer Game)
If I then Back out and go to single Player:
(Check to see if I'm Connected => I am => attempt disconnect => success => Go to Single Player Game)
I then Back out and go to Either Single or Multiplayer:
(Check to see if I'm Connected => I am :?: => attempt disconnect => infinite hang with no errors)

Choosing Single player as first selection:
(Check to see if I'm Connected => I'm not => skip disconnect call => Go to Single Player Game)
I then Back out and go to Either Single or Multiplayer
(Check to see if I'm Connected => I am :?: => attempt disconnect => infinite hang with no errors)

Is there anything I'm missing here, or is this a bug?

Thank you for any insight,

Comments

  • I did just try altering my connection check slightly, and that seems to have fixed it.

    [code2=csharp]if (PhotonNetwork.connected && PhotonNetwork.offlineMode == false)[/code2]

    since I manually toggle the offline mode on and off, it appears to be a reliable way to verify if I'm connected or not. So this work around has taken the pressure off my question, but I'd still be curious to know if there is an issue with the PhotonNetwork.connected boolean

    Thanks again,
  • There are multiple states of connected and it can be very tricky to interpret it correct.
    The reason is the multiserver backend nature.

    When you connect you connect to master and are connected. when you then join a room you are temporally no longer connected as you disconnect from master and connect to game server. When you leave the room the whole thing happens the other way around again.


    I personally went the way to use the detailed status and own definitions of connected basing on those data.

    Naturally in case of disconnected it should even there show a disconnected and used to do so, but it will take some time as it will tell the server about the disconnect and wait for it to happen. if you cap the line before that you will not get a disconnected until the timeout happened
  • Any thoughts on the second case? I never called connect at all in the second case play through(verified with console prints), but it seemed to think it was connected when I attempted the reset.
  • If you go into single player you should not touch the network commands unless you decide to set it into offlineMode. The reason is that the state is not needfully defined as it is never properly setup.
  • Correct me, if this summary is wrong somewhere:
    You are in singleplayer (using offline mode) and the next Disconnect() call will never turn your client into state "disconnected"?

    This is a problem with the current design of the offline mode.
    As long as OfflineMode is true, the client pretends to be connected.
    We could use Disconnect() to set offline mode to false and call the callbacks you'd expect.

    For your case, it should be ok if you just modify PhotonNetwork.Disconnect() by adding this:
    [code2=csharp]if (offlineMode)
    {
    offlineMode = false;
    }[/code2]

    This will set the state to Disconnected and actually disconnect when you are in multiplayer mode.
    If the client is actually connected, the disconnect is a "workflow", as the server gets a message you are disconnecting. This takes up to a few seconds but you don't need to call disconnect multiple times.

    I'm not 100% sure if OnDisconnectedFromPhoton() is called in all cases but it seems at least you are not using it at the moment.
    I will try to check this for the next PUN release (this month).
  • I followed up on this and think that the following is the most elegant and complete solution. Modify PhotonNetwork.Disconnect accordingly, please:

    [code2=csharp]public static void Disconnect()
    {
    if (offlineMode)
    {
    offlineMode = false;
    networkingPeer.State = PeerState.Disconnecting;
    networkingPeer.OnStatusChanged(StatusCode.Disconnect);
    return;
    }

    // and on code existing in PUN 1.17...[/code2]

    Please note the offlineMode = false in the code. This ends offline mode, too.