TcpPullClient.cs 3.4 KB

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