DeliveryClient.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Sers.CL.Socket.Iocp.Base;
  4. using Vit.Core.Module.Log;
  5. using Vit.Core.Util.Threading.Timer;
  6. namespace Sers.CL.Socket.Iocp.Mode.Timer
  7. {
  8. public class DeliveryClient : DeliveryClient_Base<DeliveryConnection>
  9. {
  10. #region NewConnection
  11. /// <summary>
  12. /// 发送缓冲区数据块的最小大小(单位:byte,默认 1000000)
  13. /// </summary>
  14. public int sendBufferSize = 1_000_000;
  15. /// <summary>
  16. /// 发送缓冲区个数(默认1024)
  17. /// </summary>
  18. public int sendBufferCount = 1024;
  19. public override DeliveryConnection NewConnection()
  20. {
  21. var conn = base.NewConnection();
  22. conn.SetConfig(sendBufferSize, sendBufferCount);
  23. return conn;
  24. }
  25. #endregion
  26. #region Connect Close
  27. public override bool Connect()
  28. {
  29. try
  30. {
  31. Logger.Info("[CL.DeliveryClient] Socket.Iocp,connecting", new { host, port });
  32. if (!base.Connect())
  33. {
  34. return false;
  35. }
  36. //(x.4)
  37. Send_timer.intervalMs = sendFlushInterval;
  38. Send_timer.timerCallback = Send_Flush;
  39. Send_timer.Start();
  40. Logger.Info("[CL.DeliveryClient] Socket.Iocp,connected");
  41. return true;
  42. }
  43. catch (Exception ex)
  44. {
  45. Logger.Error(ex);
  46. }
  47. return false;
  48. }
  49. public override void Close()
  50. {
  51. try
  52. {
  53. Send_timer.Stop();
  54. }
  55. catch (Exception ex)
  56. {
  57. Logger.Error(ex);
  58. }
  59. base.Close();
  60. }
  61. #endregion
  62. #region Send
  63. /// <summary>
  64. /// 发送缓冲区刷新间隔(单位:毫秒,默认:1)
  65. /// </summary>
  66. public int sendFlushInterval = 1;
  67. VitTimer_SingleThread Send_timer = new VitTimer_SingleThread();
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. void Send_Flush(object state)
  70. {
  71. try
  72. {
  73. _conn.FlushSendFrameQueue();
  74. }
  75. catch (Exception ex)
  76. {
  77. Logger.Error(ex);
  78. }
  79. }
  80. #endregion
  81. }
  82. }