void Run() { if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) { // Disable inheritance of server-socket, so child processes (like ADB) won't keep // a valid handle to that socket open until the child-process itself dies. // // This is needed due to the unfortunate combination of Process.Start enabling // handle-inheritance without providing an escape-hatch, and BCL deciding to // create all handles with inheritance enabled, again without any opt-out option. // // If this fails, there's little useful we can do. if (SocketWin32.SetHandleInformation(_listener.Server.Handle, SocketWin32.HANDLE_FLAGS.INHERIT, SocketWin32.HANDLE_FLAGS.None) == false) { Debug.Write("Failed to disable handle-inheritance for socket!"); } } _listener.Start(); var thread = new Thread(RunInternal) { Name = "Listen for connections on " + _listener.LocalEndpoint, IsBackground = true }; thread.Start(_cancelSource.Token); }
public void Host(int port) { var address = IPAddress.Loopback; var endpoint = new IPEndPoint(address, port); _listener = new TcpListener(endpoint); if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) { // Disable inheritance of server-socket, so child processes (like ADB) won't keep // a valid handle to that socket open until the child-process itself dies. // // This is needed due to the unfortunate combination of Process.Start enabling // handle-inheritance without providing an escape-hatch, and BCL deciding to // create all handles with inheritance enabled, again without any opt-out option. // // If this fails, there's little useful we can do. if (SocketWin32.SetHandleInformation(_listener.Server.Handle, SocketWin32.HANDLE_FLAGS.INHERIT, SocketWin32.HANDLE_FLAGS.None) == false) { Debug.Write("Failed to disable handle-inheritance for socket!"); } } try { _listener.Start(20); _continueListening = true; _listenerThread.Start(_listener); } catch (Exception) { Close(); throw; } }