BasicSubProtocol.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using SuperSocket.Common;
  7. using SuperSocket.SocketBase.Protocol;
  8. using SuperWebSocket.Config;
  9. using SuperSocket.SocketBase;
  10. using Microsoft.Extensions.Logging;
  11. namespace SuperWebSocket.SubProtocol
  12. {
  13. /// <summary>
  14. /// Default basic sub protocol implementation
  15. /// </summary>
  16. public class BasicSubProtocol : BasicSubProtocol<WebSocketSession>
  17. {
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  20. /// </summary>
  21. public BasicSubProtocol()
  22. : base(Assembly.GetCallingAssembly())
  23. {
  24. }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  27. /// </summary>
  28. /// <param name="name">The sub protocol name.</param>
  29. public BasicSubProtocol(string name)
  30. : base(name, Assembly.GetCallingAssembly())
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  35. /// </summary>
  36. /// <param name="commandAssembly">The command assembly.</param>
  37. public BasicSubProtocol(Assembly commandAssembly)
  38. : base(commandAssembly)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  43. /// </summary>
  44. /// <param name="commandAssemblies">The command assemblies.</param>
  45. public BasicSubProtocol(IEnumerable<Assembly> commandAssemblies)
  46. : base(commandAssemblies)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  51. /// </summary>
  52. /// <param name="name">The sub protocol name.</param>
  53. /// <param name="commandAssembly">The command assembly.</param>
  54. public BasicSubProtocol(string name, Assembly commandAssembly)
  55. : base(name, commandAssembly)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  60. /// </summary>
  61. /// <param name="name">The sub protocol name.</param>
  62. /// <param name="commandAssemblies">The command assemblies.</param>
  63. public BasicSubProtocol(string name, IEnumerable<Assembly> commandAssemblies)
  64. : base(name, commandAssemblies)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  69. /// </summary>
  70. /// <param name="name">The name.</param>
  71. /// <param name="commandAssemblies">The command assemblies.</param>
  72. /// <param name="requestInfoParser">The request info parser.</param>
  73. public BasicSubProtocol(string name, IEnumerable<Assembly> commandAssemblies, IRequestInfoParser<SubRequestInfo> requestInfoParser)
  74. : base(name, commandAssemblies, requestInfoParser)
  75. {
  76. }
  77. }
  78. /// <summary>
  79. /// Default basic sub protocol implementation
  80. /// </summary>
  81. public class BasicSubProtocol<TWebSocketSession> : SubProtocolBase<TWebSocketSession>
  82. where TWebSocketSession : WebSocketSession<TWebSocketSession>, new()
  83. {
  84. /// <summary>
  85. /// Default basic sub protocol name
  86. /// </summary>
  87. public const string DefaultName = "Basic";
  88. private List<Assembly> m_CommandAssemblies = new List<Assembly>();
  89. private Dictionary<string, ISubCommand<TWebSocketSession>> m_CommandDict;
  90. private ILogger m_Logger;
  91. private SubCommandFilterAttribute[] m_GlobalFilters;
  92. internal static BasicSubProtocol<TWebSocketSession> CreateDefaultSubProtocol()
  93. {
  94. var commandAssembly = typeof(TWebSocketSession).Assembly;
  95. if (commandAssembly == Assembly.GetExecutingAssembly())
  96. commandAssembly = Assembly.GetEntryAssembly();
  97. return new BasicSubProtocol<TWebSocketSession>(DefaultName, commandAssembly);
  98. }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with the calling aseembly as command assembly
  101. /// </summary>
  102. public BasicSubProtocol()
  103. : this(DefaultName, Assembly.GetCallingAssembly())
  104. {
  105. }
  106. /// <summary>
  107. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with the calling aseembly as command assembly
  108. /// </summary>
  109. /// <param name="name">The sub protocol name.</param>
  110. public BasicSubProtocol(string name)
  111. : this(name, Assembly.GetCallingAssembly())
  112. {
  113. }
  114. /// <summary>
  115. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with command assemblies
  116. /// </summary>
  117. /// <param name="commandAssemblies">The command assemblies.</param>
  118. public BasicSubProtocol(IEnumerable<Assembly> commandAssemblies)
  119. : this(DefaultName, commandAssemblies, new BasicSubCommandParser())
  120. {
  121. }
  122. /// <summary>
  123. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with single command assembly.
  124. /// </summary>
  125. /// <param name="commandAssembly">The command assembly.</param>
  126. public BasicSubProtocol(Assembly commandAssembly)
  127. : this(DefaultName, new List<Assembly> { commandAssembly }, new BasicSubCommandParser())
  128. {
  129. }
  130. /// <summary>
  131. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with name and single command assembly.
  132. /// </summary>
  133. /// <param name="name">The sub protocol name.</param>
  134. /// <param name="commandAssembly">The command assembly.</param>
  135. public BasicSubProtocol(string name, Assembly commandAssembly)
  136. : this(name, new List<Assembly> { commandAssembly }, new BasicSubCommandParser())
  137. {
  138. }
  139. /// <summary>
  140. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with name and command assemblies.
  141. /// </summary>
  142. /// <param name="name">The sub protocol name.</param>
  143. /// <param name="commandAssemblies">The command assemblies.</param>
  144. public BasicSubProtocol(string name, IEnumerable<Assembly> commandAssemblies)
  145. : this(name, commandAssemblies, new BasicSubCommandParser())
  146. {
  147. }
  148. /// <summary>
  149. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class.
  150. /// </summary>
  151. /// <param name="name">The name.</param>
  152. /// <param name="commandAssemblies">The command assemblies.</param>
  153. /// <param name="requestInfoParser">The request info parser.</param>
  154. public BasicSubProtocol(string name, IEnumerable<Assembly> commandAssemblies, IRequestInfoParser<SubRequestInfo> requestInfoParser)
  155. : base(name)
  156. {
  157. //The items in commandAssemblies may be null, so filter here
  158. m_CommandAssemblies.AddRange(commandAssemblies.Where(a => a != null));
  159. SubRequestParser = requestInfoParser;
  160. }
  161. #region ISubProtocol Members
  162. private void DiscoverCommands()
  163. {
  164. var subCommands = new List<ISubCommand<TWebSocketSession>>();
  165. foreach (var assembly in m_CommandAssemblies)
  166. {
  167. subCommands.AddRange(assembly.GetImplementedObjectsByInterface<ISubCommand<TWebSocketSession>>());
  168. }
  169. #if DEBUG
  170. var cmdbuilder = new StringBuilder();
  171. cmdbuilder.AppendLine(string.Format("SubProtocol {0} found the commands below:", this.Name));
  172. foreach (var c in subCommands)
  173. {
  174. cmdbuilder.AppendLine(c.Name);
  175. }
  176. //m_logger.LogDebug(cmdbuilder.ToString());
  177. #endif
  178. m_CommandDict = new Dictionary<string, ISubCommand<TWebSocketSession>>(subCommands.Count, StringComparer.OrdinalIgnoreCase);
  179. subCommands.ForEach(c =>
  180. {
  181. var fc = c as ISubCommandFilterLoader;
  182. if (fc != null)
  183. fc.LoadSubCommandFilters(m_GlobalFilters);
  184. m_CommandDict.Add(c.Name, c);
  185. }
  186. );
  187. }
  188. /// <summary>
  189. /// Initializes with the specified config.
  190. /// </summary>
  191. /// <param name="appServer">The app server.</param>
  192. /// <param name="protocolConfig">The protocol config.</param>
  193. /// <param name="logger">The logger.</param>
  194. /// <returns></returns>
  195. public override bool Initialize(IAppServer appServer, SubProtocolConfig protocolConfig, ILogger logger)
  196. {
  197. m_Logger = logger;
  198. var config = appServer.Config;
  199. m_GlobalFilters = appServer.GetType()
  200. .GetCustomAttributes(true)
  201. .OfType<SubCommandFilterAttribute>()
  202. .Where(a => string.IsNullOrEmpty(a.SubProtocol) || Name.Equals(a.SubProtocol, StringComparison.OrdinalIgnoreCase)).ToArray();
  203. if (Name.Equals(DefaultName, StringComparison.OrdinalIgnoreCase))
  204. {
  205. var commandAssembly = config.Options.GetValue("commandAssembly");
  206. if (!string.IsNullOrEmpty(commandAssembly))
  207. {
  208. if (!ResolveCommmandAssembly(commandAssembly))
  209. return false;
  210. }
  211. }
  212. if (protocolConfig != null && protocolConfig.Commands != null)
  213. {
  214. foreach (var commandConfig in protocolConfig.Commands)
  215. {
  216. var assembly = commandConfig.Options.GetValue("assembly");
  217. if (!string.IsNullOrEmpty(assembly))
  218. {
  219. if (!ResolveCommmandAssembly(assembly))
  220. return false;
  221. }
  222. }
  223. }
  224. //Always discover commands
  225. DiscoverCommands();
  226. return true;
  227. }
  228. private bool ResolveCommmandAssembly(string definition)
  229. {
  230. try
  231. {
  232. var assemblies = AssemblyUtil.GetAssembliesFromString(definition);
  233. if (assemblies.Any())
  234. m_CommandAssemblies.AddRange(assemblies);
  235. return true;
  236. }
  237. catch (Exception e)
  238. {
  239. m_Logger.LogError(e, e.Message);
  240. return false;
  241. }
  242. }
  243. /// <summary>
  244. /// Tries get command from the sub protocol's command inventory.
  245. /// </summary>
  246. /// <param name="name">The name.</param>
  247. /// <param name="command">The command.</param>
  248. /// <returns></returns>
  249. public override bool TryGetCommand(string name, out ISubCommand<TWebSocketSession> command)
  250. {
  251. return m_CommandDict.TryGetValue(name, out command);
  252. }
  253. #endregion
  254. }
  255. }