using System; using System.Net; namespace Team.Communicate.Data { public class Transmission : IEquatable, IComparable { private static ulong _counter; private static readonly object Lock; public enum EType { Sent, Received }; private ulong SequenceNr { get; } public byte[] Data { get; } public EType Type { get; } public IPEndPoint Origin { get; set; } public IPEndPoint Destination { get; set; } public DateTime Timestamp { get; } public bool IsSent => Type == EType.Sent; public Transmission(byte[] data, EType type) { lock (Lock) { SequenceNr = _counter++; } Data = data; Type = type; Timestamp = DateTime.Now; } static Transmission() { Lock = new object(); } public bool Equals(Transmission other) { if (other != null) { return SequenceNr == other.SequenceNr; } return false; } public override bool Equals(object obj) { return Equals(obj as Transmission); } public override int GetHashCode() { return (int)SequenceNr; } public int CompareTo(Transmission other) { if (SequenceNr > other.SequenceNr) { return 1; } if (SequenceNr < other.SequenceNr) { return -1; } return 0; } public override string ToString() { return base.ToString() + string.Join(";", Data); } } }