UdpCast.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace HPSocketCS
  5. {
  6. public class UdpCast : UdpClient
  7. {
  8. /// <summary>
  9. /// 创建socket监听&服务组件
  10. /// </summary>
  11. /// <returns></returns>
  12. protected override bool CreateListener()
  13. {
  14. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  15. {
  16. return false;
  17. }
  18. pListener = Sdk.Create_HP_UdpCastListener();
  19. if (pListener == IntPtr.Zero)
  20. {
  21. return false;
  22. }
  23. pClient = Sdk.Create_HP_UdpCast(pListener);
  24. if (pClient == IntPtr.Zero)
  25. {
  26. return false;
  27. }
  28. IsCreate = true;
  29. return true;
  30. }
  31. /// <summary>
  32. /// 终止服务并释放资源
  33. /// </summary>
  34. public override void Destroy()
  35. {
  36. Stop();
  37. if (pClient != IntPtr.Zero)
  38. {
  39. Sdk.Destroy_HP_UdpCastListener(pClient);
  40. pClient = IntPtr.Zero;
  41. }
  42. if (pListener != IntPtr.Zero)
  43. {
  44. Sdk.Destroy_HP_UdpCast(pListener);
  45. pListener = IntPtr.Zero;
  46. }
  47. IsCreate = false;
  48. }
  49. /// <summary>
  50. /// 获取或设置数据报文最大长度(建议在局域网环境下不超过 1472 字节,在广域网环境下不超过 548 字节)
  51. /// </summary>
  52. public uint MaxDatagramSiz
  53. {
  54. get
  55. {
  56. return Sdk.HP_UdpCast_GetMaxDatagramSize(pClient );
  57. }
  58. set
  59. {
  60. Sdk.HP_UdpCast_SetMaxDatagramSize(pClient, value);
  61. }
  62. }
  63. /// <summary>
  64. /// 获取或设置是否启用地址重用机制(默认:不启用)
  65. /// </summary>
  66. public bool ReuseAddress
  67. {
  68. get
  69. {
  70. return Sdk.HP_UdpCast_IsReuseAddress(pClient);
  71. }
  72. set
  73. {
  74. Sdk.HP_UdpCast_SetReuseAddress(pClient, value);
  75. }
  76. }
  77. /// <summary>
  78. /// 获取或设置传播模式(组播或广播)
  79. /// </summary>
  80. public CastMode CastMode
  81. {
  82. get
  83. {
  84. return Sdk.HP_UdpCast_GetCastMode(pClient);
  85. }
  86. set
  87. {
  88. Sdk.HP_UdpCast_SetCastMode(pClient, value);
  89. }
  90. }
  91. /// <summary>
  92. /// 获取或设置组播报文的 TTL(0 - 255)
  93. /// </summary>
  94. public int MultiCastTtl
  95. {
  96. get
  97. {
  98. return Sdk.HP_UdpCast_GetMultiCastTtl(pClient);
  99. }
  100. set
  101. {
  102. if (value < 0 || value > 255)
  103. {
  104. throw new InvalidOperationException("MultiCastTtl取值范围只能是0-255");
  105. }
  106. Sdk.HP_UdpCast_SetMultiCastTtl(pClient, value);
  107. }
  108. }
  109. /// <summary>
  110. /// 获取或设置是否启用组播环路(TRUE or FALSE)
  111. /// </summary>
  112. public bool MultiCastLoop
  113. {
  114. get
  115. {
  116. return Sdk.HP_UdpCast_IsMultiCastLoop(pClient);
  117. }
  118. set
  119. {
  120. Sdk.HP_UdpCast_SetMultiCastLoop(pClient, value);
  121. }
  122. }
  123. /// <summary>
  124. /// 获取当前数据报的远程地址信息(通常在 OnReceive 事件中调用)
  125. /// </summary>
  126. /// <returns></returns>
  127. public bool GetRemoteAddress(IntPtr connId, ref string ip, ref ushort port)
  128. {
  129. int ipLength = 40;
  130. StringBuilder sb = new StringBuilder(ipLength);
  131. bool ret = Sdk.HP_UdpCast_GetRemoteAddress(pClient, sb, ref ipLength, ref port);
  132. if (ret == true)
  133. {
  134. ip = sb.ToString();
  135. }
  136. return ret;
  137. }
  138. }
  139. }