SSLPullClient.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class SSLPullClientEvent
  8. {
  9. public delegate HandleResult OnReceiveEventHandler(SSLPullClient sender, int length);
  10. }
  11. public class SSLPullClient : SSLClient
  12. {
  13. /// <summary>
  14. /// 数据到达事件
  15. /// </summary>
  16. public new event SSLPullClientEvent.OnReceiveEventHandler OnReceive;
  17. public SSLPullClient()
  18. : base()
  19. {
  20. }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="verifyModel">验证模式</param>
  25. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  26. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  27. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  28. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  29. public SSLPullClient(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  30. : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath)
  31. {
  32. }
  33. ~SSLPullClient()
  34. {
  35. Destroy();
  36. }
  37. /// <summary>
  38. /// 创建socket监听&服务组件
  39. /// </summary>
  40. /// <returns></returns>
  41. protected override bool CreateListener()
  42. {
  43. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  44. {
  45. return false;
  46. }
  47. pListener = Sdk.Create_HP_TcpPullClientListener();
  48. if (pListener == IntPtr.Zero)
  49. {
  50. return false;
  51. }
  52. pClient = SSLSdk.Create_HP_SSLPullClient(pListener);
  53. if (pClient == IntPtr.Zero)
  54. {
  55. return false;
  56. }
  57. IsCreate = true;
  58. return true;
  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 Fetch(IntPtr pBuffer, int size)
  69. {
  70. return Sdk.HP_TcpPullClient_Fetch(pClient, pBuffer, size);
  71. }
  72. /// <summary>
  73. /// 抓取数据
  74. /// 用户通过该方法从 Socket 组件中抓取数据
  75. /// </summary>
  76. /// <param name="connId"></param>
  77. /// <param name="pBuffer"></param>
  78. /// <param name="size"></param>
  79. /// <returns></returns>
  80. public FetchResult Peek(IntPtr pBuffer, int size)
  81. {
  82. return Sdk.HP_TcpPullClient_Peek(pClient, pBuffer, size);
  83. }
  84. new Sdk.OnPullReceive _OnReceive = null;
  85. /// <summary>
  86. /// 设置回调函数
  87. /// </summary>
  88. protected override void SetCallback()
  89. {
  90. _OnReceive = new Sdk.OnPullReceive(SDK_OnReceive);
  91. Sdk.HP_Set_FN_Client_OnPullReceive(pListener, _OnReceive);
  92. base.SetCallback();
  93. }
  94. protected HandleResult SDK_OnReceive(IntPtr pSender, IntPtr pClient, int length)
  95. {
  96. if (OnReceive != null)
  97. {
  98. return OnReceive(this, length);
  99. }
  100. return HandleResult.Ignore;
  101. }
  102. /// <summary>
  103. /// 终止服务并释放资源
  104. /// </summary>
  105. public override void Destroy()
  106. {
  107. Stop();
  108. if (pClient != IntPtr.Zero)
  109. {
  110. SSLSdk.Destroy_HP_SSLPullClient(pClient);
  111. pClient = IntPtr.Zero;
  112. }
  113. if (pListener != IntPtr.Zero)
  114. {
  115. Sdk.Destroy_HP_TcpPullClientListener(pListener);
  116. pListener = IntPtr.Zero;
  117. }
  118. IsCreate = false;
  119. }
  120. }
  121. }