Created
July 22, 2015 16:59
-
-
Save oinant/f16849566b18a85dfef9 to your computer and use it in GitHub Desktop.
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
using System; | |
namespace ExternalLibrary | |
{ | |
public class NetworkMessage | |
{ | |
static readonly TimeSpan TimeToLive = TimeSpan.FromSeconds(10); | |
private readonly DateTime _creation; | |
private Status _status; | |
private readonly object _content; | |
public NetworkMessage(Status status, object content) | |
{ | |
_creation = DateTime.UtcNow; | |
_status = status; | |
_content = content; | |
} | |
public bool IsSuccess() | |
{ | |
if (HasTimedOut()) | |
_status = Status.Failed; | |
return _status == Status.Succeeded; | |
} | |
private bool HasTimedOut() | |
{ | |
return (_creation.Add(TimeToLive)) < DateTime.UtcNow; | |
} | |
public T GetContentAs<T>() where T : class | |
{ | |
var castedContent = _content as T; | |
if (castedContent == null) | |
throw new InvalidCastException("contet couldn't be casted into " + typeof(T)); | |
return _content as T; | |
} | |
} | |
public enum Status | |
{ | |
Pending, | |
Succeeded, | |
Failed | |
} | |
public class NetworkService | |
{ | |
public Tuple<int, object> SendMessage() | |
{ | |
// for ske of simplicity, some static data... | |
return new Tuple<int, object>(1, new object()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment