SocketExtensions.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Net;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11. using pb = Google.Protobuf;
  12. /// <summary>
  13. /// socket 辅助类
  14. /// </summary>
  15. public static class SocketUtils
  16. {
  17. #region Socket
  18. /// <summary>
  19. /// socket辅助方法
  20. /// </summary>
  21. /// <param name="sock"></param>
  22. /// <param name="interval_ms"></param>
  23. public static void SetKeepAlive(this Socket sock, int interval_ms)
  24. {
  25. uint dummy = 0;
  26. byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
  27. BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
  28. BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
  29. BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
  30. sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
  31. }
  32. public static bool IsWriteAble(this Socket socket)
  33. {
  34. try
  35. {
  36. if (socket.Poll(-1, SelectMode.SelectWrite))
  37. {
  38. return true;
  39. }
  40. }
  41. catch (SocketException se)
  42. {
  43. System.Diagnostics.Debug.WriteLine(se.Message);
  44. return false;
  45. }
  46. return false;
  47. }
  48. public static bool IsReadAble(this Socket socket)
  49. {
  50. try
  51. {
  52. if (socket.Poll(-1, SelectMode.SelectRead))
  53. {
  54. return true;
  55. }
  56. }
  57. catch (SocketException se)
  58. {
  59. System.Diagnostics.Debug.WriteLine(se.Message);
  60. return false;
  61. }
  62. return false;
  63. }
  64. #endregion
  65. /// <summary>
  66. /// Task辅助方法
  67. /// </summary>
  68. /// <param name="task"></param>
  69. /// <param name="timeout"></param>
  70. /// <returns></returns>
  71. public static async Task WaitAsync(this Task task, TimeSpan timeout)
  72. {
  73. using (var timeoutCancellationTokenSource = new CancellationTokenSource())
  74. {
  75. var delayTask = Task.Delay(timeout, timeoutCancellationTokenSource.Token);
  76. if (await Task.WhenAny(task, delayTask) == task)
  77. {
  78. timeoutCancellationTokenSource.Cancel();
  79. return;
  80. }
  81. throw new TimeoutException("The operation has timed out.");
  82. }
  83. }
  84. /// <summary>
  85. /// ProtoBuf序列化
  86. /// </summary>
  87. /// <param name="data"></param>
  88. /// <returns></returns>
  89. public static byte[] ToBytes(this pb::IMessage data)
  90. {
  91. using var ms = new MemoryStream();
  92. using var os = new pb::CodedOutputStream(ms);
  93. data.WriteTo(os);
  94. os.Flush();
  95. ms.Seek(0, SeekOrigin.Begin);
  96. return ms.ToArray();
  97. }
  98. }