12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Net;
- namespace Team.Communicate.Data
- {
- public class Transmission : IEquatable<Transmission>, IComparable<Transmission>
- {
- 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);
- }
- }
- }
|