TcpPullAgent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class TcpPullAgentEvent
  8. {
  9. public delegate HandleResult OnReceiveEventHandler(IntPtr connId, int length);
  10. }
  11. public class TcpPullAgent<T> : TcpPullAgent
  12. {
  13. public new T GetExtra(IntPtr connId)
  14. {
  15. return base.GetExtra<T>(connId);
  16. }
  17. public bool SetExtra(IntPtr connId, T obj)
  18. {
  19. return base.SetExtra(connId, obj);
  20. }
  21. }
  22. public class TcpPullAgent : TcpAgent
  23. {
  24. public new TcpPullAgentEvent.OnReceiveEventHandler OnReceive;
  25. /// <summary>
  26. /// 创建socket监听&服务组件
  27. /// </summary>
  28. /// <returns></returns>
  29. protected override bool CreateListener()
  30. {
  31. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  32. {
  33. return false;
  34. }
  35. pListener = Sdk.Create_HP_TcpPullAgentListener();
  36. if (pListener == IntPtr.Zero)
  37. {
  38. return false;
  39. }
  40. pAgent = Sdk.Create_HP_TcpPullAgent(pListener);
  41. if (pAgent == IntPtr.Zero)
  42. {
  43. return false;
  44. }
  45. IsCreate = true;
  46. return true;
  47. }
  48. /// <summary>
  49. /// 抓取数据
  50. /// 用户通过该方法从 Socket 组件中抓取数据
  51. /// </summary>
  52. /// <param name="connId"></param>
  53. /// <param name="pBuffer"></param>
  54. /// <param name="size"></param>
  55. /// <returns></returns>
  56. public FetchResult Fetch(IntPtr connId, IntPtr pBuffer, int size)
  57. {
  58. return Sdk.HP_TcpPullAgent_Fetch(pAgent, connId, pBuffer, size);
  59. }
  60. /// <summary>
  61. /// 抓取数据
  62. /// 用户通过该方法从 Socket 组件中抓取数据
  63. /// </summary>
  64. /// <param name="connId"></param>
  65. /// <param name="pBuffer"></param>
  66. /// <param name="size"></param>
  67. /// <returns></returns>
  68. public FetchResult Peek(IntPtr connId, IntPtr pBuffer, int size)
  69. {
  70. return Sdk.HP_TcpPullAgent_Peek(pAgent, connId, pBuffer, size);
  71. }
  72. new Sdk.OnPullReceive _OnReceive = null;
  73. /// <summary>
  74. /// 设置回调函数
  75. /// </summary>
  76. /// <param name="prepareConnect"></param>
  77. /// <param name="connect"></param>
  78. /// <param name="send"></param>
  79. /// <param name="recv"></param>
  80. /// <param name="close"></param>
  81. /// <param name="error"></param>
  82. /// <param name="agentShutdown"></param>
  83. protected override void SetCallback()
  84. {
  85. _OnReceive = new Sdk.OnPullReceive(SDK_OnReceive);
  86. // 设置 Socket 监听器回调函数
  87. Sdk.HP_Set_FN_Agent_OnPullReceive(pListener, _OnReceive);
  88. base.SetCallback();
  89. }
  90. protected virtual HandleResult SDK_OnReceive(IntPtr pSender, IntPtr connId, int length)
  91. {
  92. if (OnReceive != null)
  93. {
  94. return OnReceive(connId, length);
  95. }
  96. return HandleResult.Ignore;
  97. }
  98. /// <summary>
  99. /// 终止服务并释放资源
  100. /// </summary>
  101. public override void Destroy()
  102. {
  103. Stop();
  104. if (pAgent != IntPtr.Zero)
  105. {
  106. Sdk.Destroy_HP_TcpPullAgent(pAgent);
  107. pAgent = IntPtr.Zero;
  108. }
  109. if (pListener != IntPtr.Zero)
  110. {
  111. Sdk.Destroy_HP_TcpPullAgentListener(pListener);
  112. pListener = IntPtr.Zero;
  113. }
  114. IsCreate = false;
  115. }
  116. }
  117. }