IncomingPacket.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #if !BESTHTTP_DISABLE_SOCKETIO
  2. namespace BestHTTP.SocketIO3
  3. {
  4. using System.Collections.Generic;
  5. using BestHTTP.PlatformSupport.Memory;
  6. using BestHTTP.SocketIO3.Events;
  7. public struct OutgoingPacket
  8. {
  9. public bool IsBinary { get { return string.IsNullOrEmpty(this.Payload); } }
  10. public string Payload { get; set; }
  11. public List<byte[]> Attachements { get; set; }
  12. public BufferSegment PayloadData { get; set; }
  13. public bool IsVolatile { get; set; }
  14. public override string ToString()
  15. {
  16. if (!string.IsNullOrEmpty(this.Payload))
  17. return this.Payload;
  18. else
  19. return this.PayloadData.ToString();
  20. }
  21. }
  22. public struct IncomingPacket
  23. {
  24. public static readonly IncomingPacket Empty = new IncomingPacket(TransportEventTypes.Unknown, SocketIOEventTypes.Unknown, null, -1);
  25. /// <summary>
  26. /// Event type of this packet on the transport layer.
  27. /// </summary>
  28. public TransportEventTypes TransportEvent { get; private set; }
  29. /// <summary>
  30. /// The packet's type in the Socket.IO protocol.
  31. /// </summary>
  32. public SocketIOEventTypes SocketIOEvent { get; private set; }
  33. /// <summary>
  34. /// The internal ack-id of this packet.
  35. /// </summary>
  36. public int Id { get; private set; }
  37. /// <summary>
  38. /// The sender namespace's name.
  39. /// </summary>
  40. public string Namespace { get; private set; }
  41. /// <summary>
  42. /// Count of binary data expected after the current packet.
  43. /// </summary>
  44. public int AttachementCount { get; set; }
  45. /// <summary>
  46. /// list of binary data received.
  47. /// </summary>
  48. public List<BufferSegment> Attachements { get; set; }
  49. /// <summary>
  50. /// The decoded event name from the payload string.
  51. /// </summary>
  52. public string EventName { get; set; }
  53. /// <summary>
  54. /// The decoded arguments by the parser.
  55. /// </summary>
  56. public object[] DecodedArgs { get; set; }
  57. public object DecodedArg { get; set; }
  58. public IncomingPacket(TransportEventTypes transportEvent, SocketIOEventTypes packetType, string nsp, int id)
  59. {
  60. this.TransportEvent = transportEvent;
  61. this.SocketIOEvent = packetType;
  62. this.Namespace = nsp;
  63. this.Id = id;
  64. this.AttachementCount = 0;
  65. //this.ReceivedAttachements = 0;
  66. this.Attachements = null;
  67. if (this.SocketIOEvent != SocketIOEventTypes.Unknown)
  68. this.EventName = EventNames.GetNameFor(this.SocketIOEvent);
  69. else
  70. this.EventName = EventNames.GetNameFor(this.TransportEvent);
  71. this.DecodedArg = this.DecodedArgs = null;
  72. }
  73. /// <summary>
  74. /// Returns with the Payload of this packet.
  75. /// </summary>
  76. public override string ToString()
  77. {
  78. return string.Format("[Packet {0}{1}/{2},{3}[{4}]]", this.TransportEvent, this.SocketIOEvent, this.Namespace, this.Id, this.EventName);
  79. }
  80. public override bool Equals(object obj)
  81. {
  82. if (obj is IncomingPacket)
  83. return Equals((IncomingPacket)obj);
  84. return false;
  85. }
  86. public bool Equals(IncomingPacket packet)
  87. {
  88. return this.TransportEvent == packet.TransportEvent &&
  89. this.SocketIOEvent == packet.SocketIOEvent &&
  90. this.Id == packet.Id &&
  91. this.Namespace == packet.Namespace &&
  92. this.EventName == packet.EventName &&
  93. this.DecodedArg == packet.DecodedArg &&
  94. this.DecodedArgs == packet.DecodedArgs;
  95. }
  96. public override int GetHashCode()
  97. {
  98. int hashCode = -1860921451;
  99. hashCode = hashCode * -1521134295 + TransportEvent.GetHashCode();
  100. hashCode = hashCode * -1521134295 + SocketIOEvent.GetHashCode();
  101. hashCode = hashCode * -1521134295 + Id.GetHashCode();
  102. if (Namespace != null)
  103. hashCode = hashCode * -1521134295 + Namespace.GetHashCode();
  104. if (EventName != null)
  105. hashCode = hashCode * -1521134295 + EventName.GetHashCode();
  106. if (DecodedArgs != null)
  107. hashCode = hashCode * -1521134295 + DecodedArgs.GetHashCode();
  108. if (DecodedArg != null)
  109. hashCode = hashCode * -1521134295 + DecodedArg.GetHashCode();
  110. return hashCode;
  111. }
  112. public static string GenerateAcknowledgementNameFromId(int id)
  113. {
  114. return string.Concat("Generated Callback Name for Id: ##", id.ToString(), "##");
  115. }
  116. }
  117. }
  118. #endif