No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UdpSocket.cs 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 UNITY_EDITOR_WIN
  27. try
  28. {
  29. const int SIO_UDP_CONNRESET = -1744830452;
  30. IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, new byte[0]);
  31. }
  32. catch
  33. {
  34. // fallback
  35. }
  36. #endif
  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. }