SSLPackServer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class SSLPackServer : SSLServer
  8. {
  9. public SSLPackServer()
  10. : base()
  11. {
  12. }
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="verifyModel">验证模式</param>
  17. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  18. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  19. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  20. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  21. /// <param name="sniServerNameCallback">SNI 回调函数指针(可选)</param>
  22. public SSLPackServer(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath, SSLSdk.SNIServerNameCallback sniServerNameCallback)
  23. : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath, sniServerNameCallback)
  24. {
  25. }
  26. /// <summary>
  27. /// 创建socket监听&服务组件
  28. /// </summary>
  29. /// <returns></returns>
  30. protected override bool CreateListener()
  31. {
  32. if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
  33. {
  34. return false;
  35. }
  36. pListener = Sdk.Create_HP_TcpPackServerListener();
  37. if (pListener == IntPtr.Zero)
  38. {
  39. return false;
  40. }
  41. pServer = SSLSdk.Create_HP_SSLPackServer(pListener);
  42. if (pServer == IntPtr.Zero)
  43. {
  44. return false;
  45. }
  46. IsCreate = true;
  47. return true;
  48. }
  49. /// <summary>
  50. /// 终止服务并释放资源
  51. /// </summary>
  52. public override void Destroy()
  53. {
  54. Stop();
  55. if (pServer != IntPtr.Zero)
  56. {
  57. SSLSdk.Destroy_HP_SSLPackServer(pServer);
  58. pServer = IntPtr.Zero;
  59. }
  60. if (pListener != IntPtr.Zero)
  61. {
  62. Sdk.Destroy_HP_TcpPackServerListener(pListener);
  63. pListener = IntPtr.Zero;
  64. }
  65. IsCreate = false;
  66. }
  67. /// <summary>
  68. /// 读取或设置数据包最大长度
  69. /// 有效数据包最大长度不能超过 4194303/0x3FFFFF 字节,默认:262144/0x40000
  70. /// </summary>
  71. public uint MaxPackSize
  72. {
  73. get
  74. {
  75. return Sdk.HP_TcpPackServer_GetMaxPackSize(pServer);
  76. }
  77. set
  78. {
  79. Sdk.HP_TcpPackServer_SetMaxPackSize(pServer, value );
  80. }
  81. }
  82. /// <summary>
  83. /// 读取或设置包头标识
  84. /// 有效包头标识取值范围 0 ~ 1023/0x3FF,当包头标识为 0 时不校验包头,默认:0
  85. /// </summary>
  86. public ushort PackHeaderFlag
  87. {
  88. get
  89. {
  90. return Sdk.HP_TcpPackServer_GetPackHeaderFlag(pServer);
  91. }
  92. set
  93. {
  94. Sdk.HP_TcpPackServer_SetPackHeaderFlag(pServer, value);
  95. }
  96. }
  97. }
  98. }