MultipleProtocolSwitchProcessor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.SocketBase.Protocol;
  6. namespace SuperWebSocket.Protocol
  7. {
  8. /// <summary>
  9. /// http://tools.ietf.org/html/rfc6455#section-4.4
  10. /// </summary>
  11. class MultipleProtocolSwitchProcessor : IProtocolProcessor
  12. {
  13. private byte[] m_SwitchResponse;
  14. public MultipleProtocolSwitchProcessor(int[] availableVersions)
  15. {
  16. var responseBuilder = new StringBuilder();
  17. responseBuilder.AppendWithCrCf("HTTP/1.1 400 Bad Request");
  18. responseBuilder.AppendWithCrCf("Upgrade: WebSocket");
  19. responseBuilder.AppendWithCrCf("Connection: Upgrade");
  20. responseBuilder.AppendWithCrCf("Sec-WebSocket-Version: " + string.Join(", ", availableVersions.Select(i => i.ToString()).ToArray()));
  21. responseBuilder.AppendWithCrCf();
  22. m_SwitchResponse = Encoding.UTF8.GetBytes(responseBuilder.ToString());
  23. }
  24. public bool CanSendBinaryData { get { return false; } }
  25. public ICloseStatusCode CloseStatusClode { get; set; }
  26. public IProtocolProcessor NextProcessor { get; set; }
  27. public bool Handshake(IWebSocketSession session, WebSocketReceiveFilterBase previousReader, out IReceiveFilter<IWebSocketFragment> dataFrameReader)
  28. {
  29. dataFrameReader = null;
  30. session.SendRawData(m_SwitchResponse, 0, m_SwitchResponse.Length);
  31. return true;
  32. }
  33. public void SendMessage(IWebSocketSession session, string message)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. public void SendData(IWebSocketSession session, byte[] data, int offset, int length)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. public void SendCloseHandshake(IWebSocketSession session, int statusCode, string closeReason)
  42. {
  43. throw new NotImplementedException();
  44. }
  45. public void SendPong(IWebSocketSession session, byte[] pong)
  46. {
  47. throw new NotImplementedException();
  48. }
  49. public void SendPing(IWebSocketSession session, byte[] ping)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. public int Version
  54. {
  55. get { return 0; }
  56. }
  57. public bool IsValidCloseCode(int code)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. public bool TrySendMessage(IWebSocketSession session, string message)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. public bool TrySendData(IWebSocketSession session, byte[] data, int offset, int length)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. public IList<ArraySegment<byte>> GetEncodedPackage(int opCode, byte[] data, int offset, int length)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. public IList<ArraySegment<byte>> GetEncodedPackage(int opCode, string message)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. }
  78. }