BasicSubProtocol.cs 11 KB

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