You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

4 years ago
4 years ago
4 years ago
  1. #if !NET20
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Apewer.WebSocket
  5. {
  6. internal class ComposableHandler
  7. {
  8. public Func<string, byte[]> Handshake = s => new byte[0];
  9. public Func<string, byte[]> TextFrame = x => new byte[0];
  10. public Func<byte[], byte[]> BytesFrame = x => new byte[0];
  11. public Action<List<byte>> ReceiveData = delegate { };
  12. public Func<byte[], byte[]> PingFrame = i => new byte[0];
  13. public Func<byte[], byte[]> PongFrame = i => new byte[0];
  14. public Func<int, byte[]> CloseFrame = i => new byte[0];
  15. private readonly List<byte> _data = new List<byte>();
  16. public byte[] CreateHandshake(string subProtocol = null)
  17. {
  18. return Handshake(subProtocol);
  19. }
  20. public void Receive(IEnumerable<byte> data)
  21. {
  22. _data.AddRange(data);
  23. ReceiveData(_data);
  24. }
  25. public byte[] FrameText(string text)
  26. {
  27. return TextFrame(text);
  28. }
  29. public byte[] FrameBytes(byte[] bytes)
  30. {
  31. return BytesFrame(bytes);
  32. }
  33. public byte[] FramePing(byte[] bytes)
  34. {
  35. return PingFrame(bytes);
  36. }
  37. public byte[] FramePong(byte[] bytes)
  38. {
  39. return PongFrame(bytes);
  40. }
  41. public byte[] FrameClose(int code)
  42. {
  43. return CloseFrame(code);
  44. }
  45. }
  46. }
  47. #endif