TcpPackClient.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class TcpPackClient<T> : TcpPackClient
  8. {
  9. public T GetExtra()
  10. {
  11. return base.GetExtra<T>();
  12. }
  13. public bool SetExtra(T obj)
  14. {
  15. return base.SetExtra(obj);
  16. }
  17. }
  18. public class TcpPackClient : TcpClient
  19. {
  20. /// <summary>
  21. /// 创建socket监听&服务组件
  22. /// </summary>
  23. /// <returns></returns>
  24. protected override bool CreateListener()
  25. {
  26. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  27. {
  28. return false;
  29. }
  30. pListener = Sdk.Create_HP_TcpPackClientListener();
  31. if (pListener == IntPtr.Zero)
  32. {
  33. return false;
  34. }
  35. pClient = Sdk.Create_HP_TcpPackClient (pListener);
  36. if (pClient == IntPtr.Zero)
  37. {
  38. return false;
  39. }
  40. IsCreate = true;
  41. return true;
  42. }
  43. /// <summary>
  44. /// 终止服务并释放资源
  45. /// </summary>
  46. public override void Destroy()
  47. {
  48. Stop();
  49. if (pClient != IntPtr.Zero)
  50. {
  51. Sdk.Destroy_HP_TcpPackClient(pClient);
  52. pClient = IntPtr.Zero;
  53. }
  54. if (pListener != IntPtr.Zero)
  55. {
  56. Sdk.Destroy_HP_TcpPackClientListener(pListener);
  57. pListener = IntPtr.Zero;
  58. }
  59. IsCreate = false;
  60. }
  61. /// <summary>
  62. /// 读取或设置数据包最大长度
  63. /// 有效数据包最大长度不能超过 524287/0x7FFFF 字节,默认:262144/0x40000
  64. /// </summary>
  65. public uint MaxPackSize
  66. {
  67. get
  68. {
  69. return Sdk.HP_TcpPackClient_GetMaxPackSize(pClient);
  70. }
  71. set
  72. {
  73. Sdk.HP_TcpPackClient_SetMaxPackSize(pClient, value);
  74. }
  75. }
  76. /// <summary>
  77. /// 读取或设置包头标识
  78. /// 有效包头标识取值范围 0 ~ 8191/0x1FFF,当包头标识为 0 时不校验包头,默认:0
  79. /// </summary>
  80. public ushort PackHeaderFlag
  81. {
  82. get
  83. {
  84. return Sdk.HP_TcpPackClient_GetPackHeaderFlag(pClient);
  85. }
  86. set
  87. {
  88. Sdk.HP_TcpPackClient_SetPackHeaderFlag(pClient, value);
  89. }
  90. }
  91. }
  92. }