DeliveryServer.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using Sers.Core.CL.MessageDelivery;
  4. using Vit.Core.Module.Log;
  5. namespace Sers.CL.Ipc.SharedMemory
  6. {
  7. public class DeliveryServer : IDeliveryServer
  8. {
  9. public Sers.Core.Util.StreamSecurity.SecurityManager securityManager { set => _conn.securityManager = value; }
  10. public DeliveryServer()
  11. {
  12. _conn = new DeliveryConnection();
  13. _conn.OnDisconnected = (conn) =>
  14. {
  15. try
  16. {
  17. Conn_OnDisconnected?.Invoke(conn);
  18. }
  19. catch (Exception ex)
  20. {
  21. Logger.Error(ex);
  22. }
  23. try
  24. {
  25. Stop();
  26. }
  27. catch (Exception ex)
  28. {
  29. Logger.Error(ex);
  30. }
  31. try
  32. {
  33. Start();
  34. }
  35. catch (Exception ex)
  36. {
  37. Logger.Error(ex);
  38. }
  39. };
  40. }
  41. public Action<IDeliveryConnection > Conn_OnDisconnected { private get; set; }
  42. public Action<IDeliveryConnection> Conn_OnConnected { private get; set; }
  43. readonly DeliveryConnection _conn;
  44. public IEnumerable<IDeliveryConnection> ConnectedList =>new[]{ _conn };
  45. /// <summary>
  46. /// 共享内存名称
  47. /// </summary>
  48. public string name { get; set; }
  49. /// <summary>
  50. /// 共享内存节点个数
  51. /// </summary>
  52. public int nodeCount { get; set; } = 64;
  53. /// <summary>
  54. /// 共享内存节点大小
  55. /// </summary>
  56. public int nodeBufferSize { get; set; } = 10240;
  57. public void Stop()
  58. {
  59. _conn.Close();
  60. }
  61. public bool Start()
  62. {
  63. Logger.Info("[CL.Ipc] Ipc.SharedMemory,starting... name:" + name);
  64. if (!_conn.InitAsServer(name, nodeCount, nodeBufferSize))
  65. {
  66. Stop();
  67. return false;
  68. }
  69. Conn_OnConnected?.Invoke(_conn);
  70. if (!_conn.Start())
  71. {
  72. Stop();
  73. return false;
  74. }
  75. Logger.Info("[CL.Ipc] Ipc.SharedMemory,started.");
  76. return true;
  77. }
  78. }
  79. }