Skip to content

Commit

Permalink
Merge branch 'fix-swiftlint-warnings-in-xcode-ios-TunnelMonitor-296'
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Sep 13, 2023
2 parents 6e7e58c + a3d4328 commit 92bd76e
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions ios/PacketTunnelCore/TunnelMonitor/TunnelMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,65 +101,12 @@ public final class TunnelMonitor: TunnelMonitorProtocol {
func evaluateConnection(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
switch connectionState {
case .connecting:
if now.timeIntervalSince(timeoutReference) >= pingTimeout {
return .pingTimeout
}

guard let lastRequestDate = pingStats.lastRequestDate else {
return .sendInitialPing
}

if now.timeIntervalSince(lastRequestDate) >= pingDelay {
return .sendNextPing
}

return handleConnectingState(now: now, pingTimeout: pingTimeout)
case .connected:
if now.timeIntervalSince(timeoutReference) >= pingTimeout, !isHeartbeatSuspended {
return .pingTimeout
}

guard let lastRequestDate = pingStats.lastRequestDate else {
return .sendInitialPing
}

let timeSinceLastPing = now.timeIntervalSince(lastRequestDate)
if let lastReplyDate = pingStats.lastReplyDate,
lastRequestDate.timeIntervalSince(lastReplyDate) >= heartbeatReplyTimeout,
timeSinceLastPing >= pingDelay, !isHeartbeatSuspended {
return .retryHeartbeatPing
}

guard let lastSeenRx, let lastSeenTx else { return .ok }

let rxTimeElapsed = now.timeIntervalSince(lastSeenRx)
let txTimeElapsed = now.timeIntervalSince(lastSeenTx)

if timeSinceLastPing >= heartbeatPingInterval {
// Send heartbeat if traffic is flowing.
if rxTimeElapsed <= trafficFlowTimeout || txTimeElapsed <= trafficFlowTimeout {
return .sendHeartbeatPing
}

if !isHeartbeatSuspended {
return .suspendHeartbeat
}
}

if timeSinceLastPing >= pingDelay {
if txTimeElapsed >= trafficTimeout || rxTimeElapsed >= trafficTimeout {
return .trafficTimeout
}

if lastSeenTx > lastSeenRx, rxTimeElapsed >= inboundTrafficTimeout {
return .inboundTrafficTimeout
}
}

return handleConnectedState(now: now, pingTimeout: pingTimeout)
default:
break
return .ok
}

return .ok
}

func getPingTimeout() -> Duration {
Expand Down Expand Up @@ -206,6 +153,67 @@ public final class TunnelMonitor: TunnelMonitorProtocol {

return pingTimestamp
}

private func handleConnectingState(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
if now.timeIntervalSince(timeoutReference) >= pingTimeout {
return .pingTimeout
}

guard let lastRequestDate = pingStats.lastRequestDate else {
return .sendInitialPing
}

if now.timeIntervalSince(lastRequestDate) >= pingDelay {
return .sendNextPing
}

return .ok
}

private func handleConnectedState(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
if now.timeIntervalSince(timeoutReference) >= pingTimeout, !isHeartbeatSuspended {
return .pingTimeout
}

guard let lastRequestDate = pingStats.lastRequestDate else {
return .sendInitialPing
}

let timeSinceLastPing = now.timeIntervalSince(lastRequestDate)
if let lastReplyDate = pingStats.lastReplyDate,
lastRequestDate.timeIntervalSince(lastReplyDate) >= heartbeatReplyTimeout,
timeSinceLastPing >= pingDelay, !isHeartbeatSuspended {
return .retryHeartbeatPing
}

guard let lastSeenRx, let lastSeenTx else { return .ok }

let rxTimeElapsed = now.timeIntervalSince(lastSeenRx)
let txTimeElapsed = now.timeIntervalSince(lastSeenTx)

if timeSinceLastPing >= heartbeatPingInterval {
// Send heartbeat if traffic is flowing.
if rxTimeElapsed <= trafficFlowTimeout || txTimeElapsed <= trafficFlowTimeout {
return .sendHeartbeatPing
}

if !isHeartbeatSuspended {
return .suspendHeartbeat
}
}

if timeSinceLastPing >= pingDelay {
if txTimeElapsed >= trafficTimeout || rxTimeElapsed >= trafficTimeout {
return .trafficTimeout
}

if lastSeenTx > lastSeenRx, rxTimeElapsed >= inboundTrafficTimeout {
return .inboundTrafficTimeout
}
}

return .ok
}
}

/// Ping statistics.
Expand Down

0 comments on commit 92bd76e

Please sign in to comment.