public IpcPort GetConnection(string portName, bool secure, TokenImpersonationLevel level, int timeout) { PortConnection connection = null; lock (_connections) { bool flag = true; if (secure) { try { WindowsIdentity current = WindowsIdentity.GetCurrent(true); if (current != null) { flag = false; current.Dispose(); } } catch (Exception) { flag = false; } } if (flag) { connection = (PortConnection)_connections[portName]; } if ((connection == null) || connection.Port.IsDisposed) { connection = new PortConnection(IpcPort.Connect(portName, secure, level, timeout)) { Port = { Cacheable = flag } }; } else { _connections.Remove(portName); } } return(connection.Port); }
} // TimeoutConnections // The key is expected to of the form portName public IpcPort GetConnection(String portName, bool secure, TokenImpersonationLevel level, int timeout) { PortConnection connection = null; lock (_connections) { bool cacheable = true; if (secure) { try { WindowsIdentity currentId = WindowsIdentity.GetCurrent(true /*ifImpersonating*/); if (currentId != null) { cacheable = false; currentId.Dispose(); } } catch (Exception) { cacheable = false; } } if (cacheable) { connection = (PortConnection)_connections[portName]; } if (connection == null || connection.Port.IsDisposed) { connection = new PortConnection(IpcPort.Connect(portName, secure, level, timeout)); connection.Port.Cacheable = cacheable; } else { // Remove the connection from the cache _connections.Remove(portName); } } return(connection.Port); } // GetSocket