/// <summary> /// Disconnects from remote host providing specified reason /// </summary> public void Disconnect(string reason) { if (m_status != NetConnectionStatus.Disconnected) { SendUnsentMessages(true, 1.0f); // flush NetHandshake.SendDisconnected(m_parent, reason, this); if (m_unsentMessages.Count < 1) { SetStatus(NetConnectionStatus.Disconnected, reason); } else { SetStatus(NetConnectionStatus.Disconnecting, reason); m_pendingDisconnectedReason = reason; } } }
/// <summary> /// Connection heartbeat, normally called from NetClient.Heartbeat() or NetServer.Heartbeat() /// Calling it manually may interfere with proper throttling /// </summary> public void Heartbeat(double now) { m_frameLength = now - m_lastHeartbeat; m_lastHeartbeat = now; // resend timed out saved packets for (int i = 0; i < m_savedReliableMessages.Length; i++) { if (m_savedReliableMessages[i] != null && m_savedReliableMessages[i].Count > 0) { foreach (NetMessage msg in m_savedReliableMessages[i]) { if (now > msg.m_nextResendTime) { // it might have been lost; resend it if (msg.m_numResends >= Configuration.NumResendsBeforeFailing) { m_parent.Log.Error("Dropping outgoing packet " + msg + "; maximum number of resends reached!"); } else { if (MessageResent != null) { MessageResent(this, new NetMessageEventArgs(msg)); } //m_parent.Log.Verbose("Resending " + msg + " @ " + now); m_unsentMessages.Enqueue(msg); float nextResend = (float)now + (msg.m_numResends < 1 ? Configuration.ResendFirstUnacknowledgedDelay : Configuration.ResendSubsequentUnacknowledgedDelay); msg.m_nextResendTime = nextResend; msg.m_numResends++; Statistics.MessagesResent++; } m_tmpRemoveList.Add(msg); // to be removed } } if (m_tmpRemoveList.Count > 0) { foreach (NetMessage rm in m_tmpRemoveList) { m_savedReliableMessages[i].Remove(rm); // remove it; it will be readded upon sending } m_tmpRemoveList.Clear(); } } } // send all messages (possibly forcing acks to be sent) SendUnsentMessages((m_forceExplicitAckTime != 0.0 && now > m_forceExplicitAckTime), (m_frameLength > 0.1 ? 0.1 : m_frameLength)); // throttling don't like unlikely long frames if (now - m_lastHeardFromRemote > Configuration.ConnectionTimeOut) { NetHandshake.SendDisconnected(m_parent, "Connection timed out", this); SetStatus(NetConnectionStatus.Disconnected, "Connection timed out"); return; } if (m_status == NetConnectionStatus.Connecting) { // keep sending connect packets if (now - m_lastSentHandshake > NetConstants.SendHandshakeFrequency) { if (now - m_firstSentHandshake > NetConstants.ConnectAttemptTimeout) { m_parent.Log.Info("Failed to connect; no answer from remote host!"); SetStatus(NetConnectionStatus.Disconnected, "Connect attempt timeout"); } else { NetClient client = m_parent as NetClient; if (client != null) { int bytesSent = NetHandshake.SendConnect(client, m_remoteEndpoint, m_savedConnectCustomData); client.ServerConnection.Statistics.PacketsSent++; client.ServerConnection.Statistics.MessagesSent++; client.ServerConnection.Statistics.BytesSent += bytesSent; m_lastSentHandshake = now; } else { NetHandshake.SendConnectResponse(m_parent, this, m_remoteEndpoint); m_lastSentHandshake = now; } } } return; } m_ping.Heartbeat(now); }