BasicSubCommandParser.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.SocketBase.Command;
  6. using SuperSocket.SocketBase.Protocol;
  7. namespace SuperWebSocket.SubProtocol
  8. {
  9. /// <summary>
  10. /// Basic sub command parser
  11. /// </summary>
  12. public class BasicSubCommandParser : IRequestInfoParser<SubRequestInfo>
  13. {
  14. #region ISubProtocolCommandParser Members
  15. /// <summary>
  16. /// Parses the request info.
  17. /// </summary>
  18. /// <param name="source">The source.</param>
  19. /// <returns></returns>
  20. public SubRequestInfo ParseRequestInfo(string source)
  21. {
  22. var cmd = source.Trim();
  23. int pos = cmd.IndexOf(' ');
  24. string name;
  25. string param;
  26. if (pos > 0)
  27. {
  28. name = cmd.Substring(0, pos);
  29. param = cmd.Substring(pos + 1);
  30. }
  31. else
  32. {
  33. name = cmd;
  34. param = string.Empty;
  35. }
  36. pos = name.IndexOf('-');
  37. string token = string.Empty;
  38. if (pos > 0)
  39. {
  40. token = name.Substring(pos + 1);
  41. name = name.Substring(0, pos);
  42. }
  43. return new SubRequestInfo(name, token, param);
  44. }
  45. #endregion
  46. }
  47. }