Extensions.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.SocketBase.Metadata;
  6. namespace SuperSocket.SocketBase
  7. {
  8. /// <summary>
  9. /// Extensions class for SocketBase project
  10. /// </summary>
  11. public static class Extensions
  12. {
  13. /// <summary>
  14. /// Gets the app server instance in the bootstrap by name, ignore case
  15. /// </summary>
  16. /// <param name="bootstrap">The bootstrap.</param>
  17. /// <param name="name">The name of the appserver instance.</param>
  18. /// <returns></returns>
  19. /// <exception cref="System.ArgumentNullException"></exception>
  20. public static IWorkItem GetServerByName(this IBootstrap bootstrap, string name)
  21. {
  22. if (string.IsNullOrEmpty(name))
  23. throw new ArgumentNullException("name");
  24. return bootstrap.AppServers.FirstOrDefault(s => name.Equals(s.Name, StringComparison.OrdinalIgnoreCase));
  25. }
  26. /// <summary>
  27. /// Gets the status info metadata from the server type.
  28. /// </summary>
  29. /// <param name="serverType">Type of the server.</param>
  30. /// <returns></returns>
  31. /// <exception cref="System.ArgumentNullException"></exception>
  32. public static StatusInfoAttribute[] GetStatusInfoMetadata(this Type serverType)
  33. {
  34. if (serverType == null)
  35. throw new ArgumentNullException("serverType");
  36. var attType = typeof(AppServerMetadataTypeAttribute);
  37. while (true)
  38. {
  39. var atts = serverType.GetCustomAttributes(attType, false);
  40. if (atts != null && atts.Length > 0)
  41. {
  42. var serverMetadataTypeAtt = atts[0] as AppServerMetadataTypeAttribute;
  43. return serverMetadataTypeAtt
  44. .MetadataType
  45. .GetCustomAttributes(typeof(StatusInfoAttribute), true)
  46. .OfType<StatusInfoAttribute>().ToArray();
  47. }
  48. if (serverType.BaseType == null)
  49. return null;
  50. serverType = serverType.BaseType;
  51. }
  52. }
  53. }
  54. }