-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of a connection timeout.
Fixes #84
- Loading branch information
1 parent
3832738
commit 318fb3c
Showing
10 changed files
with
310 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/FubarDev.FtpServer.Abstractions/IFtpConnectionKeepAlive.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// <copyright file="IFtpConnectionKeepAlive.cs" company="Fubar Development Junker"> | ||
// Copyright (c) Fubar Development Junker. All rights reserved. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
namespace FubarDev.FtpServer | ||
{ | ||
/// <summary> | ||
/// Interface to ensure that a connection keeps alive. | ||
/// </summary> | ||
public interface IFtpConnectionKeepAlive | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether the connection is still alive. | ||
/// </summary> | ||
bool IsAlive { get; } | ||
|
||
/// <summary> | ||
/// Gets the time of last activity (UTC). | ||
/// </summary> | ||
DateTime LastActivityUtc { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether a data transfer is active. | ||
/// </summary> | ||
bool IsInDataTransfer { get; set; } | ||
|
||
/// <summary> | ||
/// Ensure that the connection keeps alive. | ||
/// </summary> | ||
void KeepAlive(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// <copyright file="FtpConnectionKeepAlive.cs" company="Fubar Development Junker"> | ||
// Copyright (c) Fubar Development Junker. All rights reserved. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
namespace FubarDev.FtpServer | ||
{ | ||
internal class FtpConnectionKeepAlive : IFtpConnectionKeepAlive | ||
{ | ||
/// <summary> | ||
/// The lock to be acquired when the timeout information gets set or read. | ||
/// </summary> | ||
private readonly object _inactivityTimeoutLock = new object(); | ||
|
||
/// <summary> | ||
/// The timeout for the detection of inactivity. | ||
/// </summary> | ||
private readonly TimeSpan _inactivityTimeout; | ||
|
||
/// <summary> | ||
/// The timestamp of the last activity on the connection. | ||
/// </summary> | ||
private DateTime _utcLastActiveTime; | ||
|
||
/// <summary> | ||
/// The timestamp where the connection expires. | ||
/// </summary> | ||
private DateTime? _expirationTimeout; | ||
|
||
/// <summary> | ||
/// Indicator if a data transfer is ongoing. | ||
/// </summary> | ||
private bool _isInDataTransfer; | ||
|
||
public FtpConnectionKeepAlive(TimeSpan? inactivityTimeout) | ||
{ | ||
_inactivityTimeout = inactivityTimeout ?? TimeSpan.MaxValue; | ||
UpdateLastActiveTime(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsAlive | ||
{ | ||
get | ||
{ | ||
lock (_inactivityTimeoutLock) | ||
{ | ||
if (_expirationTimeout == null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (_isInDataTransfer) | ||
{ | ||
UpdateLastActiveTime(); | ||
return true; | ||
} | ||
|
||
return DateTime.UtcNow <= _expirationTimeout.Value; | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public DateTime LastActivityUtc | ||
{ | ||
get | ||
{ | ||
lock (_inactivityTimeoutLock) | ||
{ | ||
return _utcLastActiveTime; | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsInDataTransfer | ||
{ | ||
get | ||
{ | ||
lock (_inactivityTimeoutLock) | ||
{ | ||
// Reset the expiration timeout while a data transfer is ongoing. | ||
if (_isInDataTransfer) | ||
{ | ||
UpdateLastActiveTime(); | ||
} | ||
|
||
return _isInDataTransfer; | ||
} | ||
} | ||
set | ||
{ | ||
lock (_inactivityTimeoutLock) | ||
{ | ||
// Reset the expiration timeout when the data transfer status gets updated. | ||
UpdateLastActiveTime(); | ||
_isInDataTransfer = value; | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void KeepAlive() | ||
{ | ||
lock (_inactivityTimeoutLock) | ||
{ | ||
UpdateLastActiveTime(); | ||
} | ||
} | ||
|
||
private void UpdateLastActiveTime() | ||
{ | ||
_utcLastActiveTime = DateTime.UtcNow; | ||
_expirationTimeout = (_inactivityTimeout == TimeSpan.MaxValue) | ||
? (DateTime?)null | ||
: _utcLastActiveTime.Add(_inactivityTimeout); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.