Transmission.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Net;
  3. namespace Team.Communicate.Data
  4. {
  5. public class Transmission : IEquatable<Transmission>, IComparable<Transmission>
  6. {
  7. private static ulong _counter;
  8. private static readonly object Lock;
  9. public enum EType { Sent, Received };
  10. private ulong SequenceNr { get; }
  11. public byte[] Data { get; }
  12. public EType Type { get; }
  13. public IPEndPoint Origin { get; set; }
  14. public IPEndPoint Destination { get; set; }
  15. public DateTime Timestamp { get; }
  16. public bool IsSent => Type == EType.Sent;
  17. public Transmission(byte[] data, EType type)
  18. {
  19. lock (Lock)
  20. {
  21. SequenceNr = _counter++;
  22. }
  23. Data = data;
  24. Type = type;
  25. Timestamp = DateTime.Now;
  26. }
  27. static Transmission()
  28. {
  29. Lock = new object();
  30. }
  31. public bool Equals(Transmission other)
  32. {
  33. if (other != null)
  34. {
  35. return SequenceNr == other.SequenceNr;
  36. }
  37. return false;
  38. }
  39. public override bool Equals(object obj)
  40. {
  41. return Equals(obj as Transmission);
  42. }
  43. public override int GetHashCode()
  44. {
  45. return (int)SequenceNr;
  46. }
  47. public int CompareTo(Transmission other)
  48. {
  49. if (SequenceNr > other.SequenceNr)
  50. {
  51. return 1;
  52. }
  53. if (SequenceNr < other.SequenceNr)
  54. {
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. public override string ToString()
  60. {
  61. return base.ToString() + string.Join(";", Data);
  62. }
  63. }
  64. }