SSLPullAgent.cs 4.1 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 SSLPullAgent : SSLAgent
  8. {
  9. protected new TcpPullAgentEvent.OnReceiveEventHandler OnReceive;
  10. public SSLPullAgent()
  11. : base()
  12. {
  13. }
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="verifyModel">验证模式</param>
  18. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  19. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  20. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  21. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  22. public SSLPullAgent(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  23. : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath)
  24. {
  25. }
  26. /// <summary>
  27. /// 创建socket监听&服务组件
  28. /// </summary>
  29. /// <returns></returns>
  30. protected override bool CreateListener()
  31. {
  32. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  33. {
  34. return false;
  35. }
  36. pListener = Sdk.Create_HP_TcpPullAgentListener();
  37. if (pListener == IntPtr.Zero)
  38. {
  39. return false;
  40. }
  41. pAgent = SSLSdk.Create_HP_SSLPullAgent(pListener);
  42. if (pAgent == IntPtr.Zero)
  43. {
  44. return false;
  45. }
  46. IsCreate = true;
  47. return true;
  48. }
  49. /// <summary>
  50. /// 抓取数据
  51. /// 用户通过该方法从 Socket 组件中抓取数据
  52. /// </summary>
  53. /// <param name="connId"></param>
  54. /// <param name="pBuffer"></param>
  55. /// <param name="size"></param>
  56. /// <returns></returns>
  57. public FetchResult Fetch(IntPtr connId, IntPtr pBuffer, int size)
  58. {
  59. return Sdk.HP_TcpPullAgent_Fetch(pAgent, connId, pBuffer, size);
  60. }
  61. /// <summary>
  62. /// 抓取数据
  63. /// 用户通过该方法从 Socket 组件中抓取数据
  64. /// </summary>
  65. /// <param name="connId"></param>
  66. /// <param name="pBuffer"></param>
  67. /// <param name="size"></param>
  68. /// <returns></returns>
  69. public FetchResult Peek(IntPtr connId, IntPtr pBuffer, int size)
  70. {
  71. return Sdk.HP_TcpPullAgent_Peek(pAgent, connId, pBuffer, size);
  72. }
  73. new Sdk.OnPullReceive _OnReceive = null;
  74. /// <summary>
  75. /// 设置回调函数
  76. /// </summary>
  77. /// <param name="prepareConnect"></param>
  78. /// <param name="connect"></param>
  79. /// <param name="send"></param>
  80. /// <param name="recv"></param>
  81. /// <param name="close"></param>
  82. /// <param name="error"></param>
  83. /// <param name="agentShutdown"></param>
  84. protected override void SetCallback()
  85. {
  86. _OnReceive = new Sdk.OnPullReceive(SDK_OnReceive);
  87. // 设置 Socket 监听器回调函数
  88. Sdk.HP_Set_FN_Agent_OnPullReceive(pListener, _OnReceive);
  89. base.SetCallback();
  90. }
  91. protected virtual HandleResult SDK_OnReceive(IntPtr pSender, IntPtr connId, int length)
  92. {
  93. if (OnReceive != null)
  94. {
  95. return OnReceive(connId, length);
  96. }
  97. return HandleResult.Ignore;
  98. }
  99. /// <summary>
  100. /// 终止服务并释放资源
  101. /// </summary>
  102. public override void Destroy()
  103. {
  104. Stop();
  105. if (pAgent != IntPtr.Zero)
  106. {
  107. SSLSdk.Destroy_HP_SSLPullAgent(pAgent);
  108. pAgent = IntPtr.Zero;
  109. }
  110. if (pListener != IntPtr.Zero)
  111. {
  112. Sdk.Destroy_HP_TcpPullAgentListener(pListener);
  113. pListener = IntPtr.Zero;
  114. }
  115. IsCreate = false;
  116. }
  117. }
  118. }