WebSocketReceiveFilterBase.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.Common;
  6. using SuperSocket.SocketBase;
  7. using SuperSocket.SocketBase.Command;
  8. using SuperSocket.SocketBase.Protocol;
  9. namespace SuperWebSocket.Protocol
  10. {
  11. /// <summary>
  12. /// WebSocketReceiveFilter basis
  13. /// </summary>
  14. public abstract class WebSocketReceiveFilterBase : ReceiveFilterBase<IWebSocketFragment>
  15. {
  16. /// <summary>
  17. /// The length of Sec3Key
  18. /// </summary>
  19. protected const int SecKey3Len = 8;
  20. private readonly IWebSocketSession m_Session;
  21. internal IWebSocketSession Session
  22. {
  23. get { return m_Session; }
  24. }
  25. static WebSocketReceiveFilterBase()
  26. {
  27. HandshakeRequestInfo = new HandshakeRequest();
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="WebSocketReceiveFilterBase" /> class.
  31. /// </summary>
  32. /// <param name="session">The session.</param>
  33. protected WebSocketReceiveFilterBase(IWebSocketSession session)
  34. {
  35. m_Session = session;
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="WebSocketReceiveFilterBase" /> class.
  39. /// </summary>
  40. /// <param name="previousReceiveFilter">The previous receive filter.</param>
  41. protected WebSocketReceiveFilterBase(WebSocketReceiveFilterBase previousReceiveFilter)
  42. : base(previousReceiveFilter)
  43. {
  44. m_Session = previousReceiveFilter.Session;
  45. }
  46. /// <summary>
  47. /// Handshakes the specified protocol processor.
  48. /// </summary>
  49. /// <param name="protocolProcessor">The protocol processor.</param>
  50. /// <param name="session">The session.</param>
  51. /// <returns></returns>
  52. protected bool Handshake(IProtocolProcessor protocolProcessor, IWebSocketSession session)
  53. {
  54. IReceiveFilter<IWebSocketFragment> dataFrameReader;
  55. if (!protocolProcessor.Handshake(session, this, out dataFrameReader))
  56. {
  57. session.Close(CloseReason.ServerClosing);
  58. return false;
  59. }
  60. //Processor handshake sucessfully, but output datareader is null, so the multiple protocol switch handled the handshake
  61. //In this case, the handshake is not completed
  62. if (dataFrameReader == null)
  63. {
  64. NextReceiveFilter = this;
  65. return false;
  66. }
  67. NextReceiveFilter = dataFrameReader;
  68. return true;
  69. }
  70. /// <summary>
  71. /// Gets the handshake request info.
  72. /// </summary>
  73. protected static IWebSocketFragment HandshakeRequestInfo { get; private set; }
  74. }
  75. }