TcpPullServer.cs 3.5 KB

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