Lidgren.Network.NetConnection.UnconnectedHeartbeat C# (CSharp) Method

UnconnectedHeartbeat() private method

private UnconnectedHeartbeat ( float now ) : void
now float
return void
        internal void UnconnectedHeartbeat(float now)
        {
            m_peer.VerifyNetworkThread();

            if (m_disconnectRequested)
                ExecuteDisconnect(m_disconnectMessage, true);

            if (m_connectRequested)
            {
                switch (m_status)
                {
                    case NetConnectionStatus.Connected:
                    case NetConnectionStatus.RespondedConnect:
                        // reconnect
                        ExecuteDisconnect("Reconnecting", true);
                        break;

                    case NetConnectionStatus.InitiatedConnect:
                        // send another connect attempt
                        SendConnect(now);
                        break;

                    case NetConnectionStatus.Disconnected:
                        m_peer.ThrowOrLog("This connection is Disconnected; spent. A new one should have been created");
                        break;

                    case NetConnectionStatus.Disconnecting:
                        // let disconnect finish first
                        break;

                    case NetConnectionStatus.None:
                    default:
                        SendConnect(now);
                        break;
                }
                return;
            }

            if (now - m_lastHandshakeSendTime > m_peerConfiguration.m_resendHandshakeInterval)
            {
                if (m_handshakeAttempts >= m_peerConfiguration.m_maximumHandshakeAttempts)
                {
                    // failed to connect
                    ExecuteDisconnect("Failed to establish connection - no response from remote host", true);
                    return;
                }

                // resend handshake
                switch (m_status)
                {
                    case NetConnectionStatus.InitiatedConnect:
                        SendConnect(now);
                        break;
                    case NetConnectionStatus.RespondedConnect:
                        SendConnectResponse(now, true);
                        break;
                    case NetConnectionStatus.RespondedAwaitingApproval:
                        // awaiting approval
                        m_lastHandshakeSendTime = now; // postpone handshake resend
                        break;
                    case NetConnectionStatus.None:
                    case NetConnectionStatus.ReceivedInitiation:
                    default:
                        m_peer.LogWarning("Time to resend handshake, but status is " + m_status);
                        break;
                }
            }
        }

Usage Example

        /// <summary>
        /// Process handshake messages / client connected, client disconnected
        /// </summary>
        /// <param name="delta"></param>
        /// <param name="now"></param>
        /// <param name="maxConnectionHeartbeatsPerSecond"></param>
        private void ProcessHandshakes(double delta, double now, double maxConnectionHeartbeatsPerSecond)
        {
            // do handshake heartbeats
            if ((m_frameCounter % 3) == 0)
            {
                foreach (var kvp in _handshakeManager.Handshakes)
                {
                    NetConnection conn = kvp.Value as NetConnection;

                    /*#if DEBUG
                     *                      // sanity check
                     *                      if (kvp.Key != kvp.Key)
                     *                          LogWarning("Sanity fail! Connection in handshake list under wrong key!");
                     #endif*/
                    conn.UnconnectedHeartbeat(now);
                    if (conn.m_status == NetConnectionStatus.Connected ||
                        conn.m_status == NetConnectionStatus.Disconnected)
                    {
                        /*#if DEBUG
                         *                          // sanity check
                         *                          if (conn.m_status == NetConnectionStatus.Disconnected && m_handshakes.ContainsKey(conn.RemoteEndPoint))
                         *                          {
                         *                              LogWarning("Sanity fail! Handshakes list contained disconnected connection!");
                         *                              m_handshakes.Remove(conn.RemoteEndPoint);
                         *                          }
                         #endif*/
                        break; // collection has been modified
                    }
                }
            }
        }
All Usage Examples Of Lidgren.Network.NetConnection::UnconnectedHeartbeat