JsonSubCommandBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using SuperSocket.SocketBase.Protocol;
  7. namespace SuperWebSocket.SubProtocol
  8. {
  9. /// <summary>
  10. /// Json SubCommand base
  11. /// </summary>
  12. /// <typeparam name="TWebSocketSession">The type of the web socket session.</typeparam>
  13. /// <typeparam name="TJsonCommandInfo">The type of the json command info.</typeparam>
  14. public abstract class JsonSubCommandBase<TWebSocketSession, TJsonCommandInfo> : SubCommandBase<TWebSocketSession>
  15. where TWebSocketSession : WebSocketSession<TWebSocketSession>, new()
  16. {
  17. private const string m_QueryTemplateA = "{0}-{1} {2}";
  18. private const string m_QueryTemplateB = "{0} {1}";
  19. private bool m_IsSimpleType = false;
  20. private Type m_CommandInfoType;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="JsonSubCommandBase&lt;TWebSocketSession, TJsonCommandInfo&gt;"/> class.
  23. /// </summary>
  24. public JsonSubCommandBase()
  25. {
  26. m_CommandInfoType = typeof(TJsonCommandInfo);
  27. if (m_CommandInfoType.IsSimpleType())
  28. m_IsSimpleType = true;
  29. }
  30. /// <summary>
  31. /// Executes the command.
  32. /// </summary>
  33. /// <param name="session">The session.</param>
  34. /// <param name="requestInfo">The request info.</param>
  35. public override void ExecuteCommand(TWebSocketSession session, SubRequestInfo requestInfo)
  36. {
  37. if (string.IsNullOrEmpty(requestInfo.Body))
  38. {
  39. ExecuteJsonCommand(session, default(TJsonCommandInfo));
  40. return;
  41. }
  42. TJsonCommandInfo jsonCommandInfo;
  43. LocalDataStoreSlot tokenSlot = null;
  44. if (!string.IsNullOrEmpty(requestInfo.Token))
  45. tokenSlot = session.SetCurrentToken(requestInfo.Token);
  46. try
  47. {
  48. if (!m_IsSimpleType)
  49. jsonCommandInfo = (TJsonCommandInfo)session.AppServer.JsonDeserialize(requestInfo.Body, m_CommandInfoType);
  50. else
  51. jsonCommandInfo = (TJsonCommandInfo)Convert.ChangeType(requestInfo.Body, m_CommandInfoType);
  52. ExecuteJsonCommand(session, jsonCommandInfo);
  53. }
  54. finally
  55. {
  56. if (tokenSlot != null)
  57. Thread.SetData(tokenSlot, null);
  58. }
  59. }
  60. /// <summary>
  61. /// Executes the json command.
  62. /// </summary>
  63. /// <param name="session">The session.</param>
  64. /// <param name="commandInfo">The command info.</param>
  65. protected abstract void ExecuteJsonCommand(TWebSocketSession session, TJsonCommandInfo commandInfo);
  66. /// <summary>
  67. /// Gets the json message.
  68. /// </summary>
  69. /// <param name="session">The session.</param>
  70. /// <param name="name">The name.</param>
  71. /// <param name="token">The token.</param>
  72. /// <param name="content">The content.</param>
  73. /// <returns></returns>
  74. protected string GetJsonMessage(TWebSocketSession session, string name, string token, object content)
  75. {
  76. string strOutput;
  77. //Needn't serialize primitive type object
  78. if (content.GetType().IsSimpleType())
  79. strOutput = content.ToString();
  80. else
  81. strOutput = session.AppServer.JsonSerialize(content);
  82. if (string.IsNullOrEmpty(token))
  83. return string.Format(m_QueryTemplateB, name, strOutput);
  84. else
  85. return string.Format(m_QueryTemplateA, name, token, strOutput);
  86. }
  87. }
  88. }