Skip to content

Instantly share code, notes, and snippets.

@oinant
Created July 22, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oinant/f16849566b18a85dfef9 to your computer and use it in GitHub Desktop.
Save oinant/f16849566b18a85dfef9 to your computer and use it in GitHub Desktop.
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