暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UdpSocket.cs 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. using System;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. namespace Microsoft.Unity.VisualStudio.Editor.Messaging
  9. {
  10. internal class UdpSocket : Socket
  11. {
  12. // Maximum UDP payload is 65507 bytes.
  13. // TCP mode will be used when the payload is bigger than this BufferSize
  14. public const int BufferSize = 1024 * 8;
  15. internal UdpSocket()
  16. : base(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
  17. {
  18. SetIOControl();
  19. }
  20. public void Bind(IPAddress address, int port = 0)
  21. {
  22. Bind(new IPEndPoint(address ?? IPAddress.Any, port));
  23. }
  24. private void SetIOControl()
  25. {
  26. if (!VisualStudioEditor.IsWindows)
  27. return;
  28. try
  29. {
  30. const int SIO_UDP_CONNRESET = -1744830452;
  31. IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, new byte[0]);
  32. }
  33. catch
  34. {
  35. // fallback
  36. }
  37. }
  38. public static byte[] BufferFor(IAsyncResult result)
  39. {
  40. return (byte[])result.AsyncState;
  41. }
  42. public static EndPoint Any()
  43. {
  44. return new IPEndPoint(IPAddress.Any, 0);
  45. }
  46. }
  47. }