SSLPullServer.cs 4.1 KB

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